1
1
import { fs , path } from '@vuepress/utils'
2
2
import MarkdownIt from 'markdown-it'
3
- import { describe , expect , it } from 'vitest'
3
+ import {
4
+ afterAll ,
5
+ afterEach ,
6
+ beforeAll ,
7
+ describe ,
8
+ expect ,
9
+ it ,
10
+ vi ,
11
+ } from 'vitest'
4
12
import { codePlugin , importCodePlugin } from '../../src/index.js'
5
13
import type { MarkdownEnv } from '../../src/index.js'
6
14
@@ -11,6 +19,21 @@ const mdFixturePath = path.resolve(__dirname, mdFixturePathRelative)
11
19
const jsFixtureContent = fs . readFileSync ( jsFixturePath ) . toString ( )
12
20
const mdFixtureContent = fs . readFileSync ( mdFixturePath ) . toString ( )
13
21
22
+ const consoleError = console . error
23
+ const mockConsoleError = vi . fn ( )
24
+
25
+ beforeAll ( ( ) => {
26
+ console . error = mockConsoleError
27
+ } )
28
+
29
+ afterEach ( ( ) => {
30
+ mockConsoleError . mockClear ( )
31
+ } )
32
+
33
+ afterAll ( ( ) => {
34
+ console . error = consoleError
35
+ } )
36
+
14
37
describe ( '@vuepress/markdown > plugins > importCodePlugin' , ( ) => {
15
38
it ( 'should not be parsed as import code syntax' , ( ) => {
16
39
const source = [
@@ -119,6 +142,7 @@ ${mdFixtureContent.split('\n').slice(0, 5).join('\n').replace(/\n?$/, '\n')}\
119
142
120
143
expect ( rendered ) . toEqual ( expected )
121
144
expect ( env . importedFiles ) . toEqual ( [ '/foo.js' , '/bar.md' ] )
145
+ expect ( mockConsoleError ) . toHaveBeenCalledTimes ( 2 )
122
146
} )
123
147
124
148
it ( 'should use file ext as fallback language' , ( ) => {
@@ -139,6 +163,7 @@ ${mdFixtureContent.split('\n').slice(0, 5).join('\n').replace(/\n?$/, '\n')}\
139
163
140
164
expect ( rendered ) . toEqual ( expected )
141
165
expect ( env . importedFiles ) . toEqual ( [ '/foo.js' , '/bar.md' ] )
166
+ expect ( mockConsoleError ) . toHaveBeenCalledTimes ( 2 )
142
167
} )
143
168
} )
144
169
@@ -164,6 +189,7 @@ ${mdFixtureContent.split('\n').slice(0, 5).join('\n').replace(/\n?$/, '\n')}\
164
189
'/foo.js' ,
165
190
path . resolve ( __dirname , './bar.js' ) ,
166
191
] )
192
+ expect ( mockConsoleError ) . toHaveBeenCalledTimes ( 2 )
167
193
} )
168
194
169
195
it ( 'should not resolve relative path if filePath is not provided' , ( ) => {
@@ -184,6 +210,7 @@ ${mdFixtureContent.split('\n').slice(0, 5).join('\n').replace(/\n?$/, '\n')}\
184
210
185
211
expect ( rendered ) . toEqual ( expected )
186
212
expect ( env . importedFiles ) . toEqual ( [ '/foo.js' ] )
213
+ expect ( mockConsoleError ) . toHaveBeenCalledTimes ( 2 )
187
214
} )
188
215
189
216
it ( 'should handle import path correctly' , ( ) => {
229
256
230
257
expect ( rendered ) . toEqual ( expected )
231
258
expect ( env . importedFiles ) . toEqual ( [ '/path/to/foo.js' ] )
259
+ expect ( mockConsoleError ) . toHaveBeenCalledTimes ( 1 )
232
260
} )
233
261
234
262
it ( 'should terminate blockquote' , ( ) => {
251
279
252
280
expect ( rendered ) . toEqual ( expected )
253
281
expect ( env . importedFiles ) . toEqual ( [ '/path/to/foo.js' ] )
282
+ expect ( mockConsoleError ) . toHaveBeenCalledTimes ( 1 )
254
283
} )
255
284
} )
256
285
0 commit comments