11import { fs , path } from '@vuepress/utils'
22import 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'
412import { codePlugin , importCodePlugin } from '../../src/index.js'
513import type { MarkdownEnv } from '../../src/index.js'
614
@@ -11,6 +19,21 @@ const mdFixturePath = path.resolve(__dirname, mdFixturePathRelative)
1119const jsFixtureContent = fs . readFileSync ( jsFixturePath ) . toString ( )
1220const mdFixtureContent = fs . readFileSync ( mdFixturePath ) . toString ( )
1321
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+
1437describe ( '@vuepress/markdown > plugins > importCodePlugin' , ( ) => {
1538 it ( 'should not be parsed as import code syntax' , ( ) => {
1639 const source = [
@@ -119,6 +142,7 @@ ${mdFixtureContent.split('\n').slice(0, 5).join('\n').replace(/\n?$/, '\n')}\
119142
120143 expect ( rendered ) . toEqual ( expected )
121144 expect ( env . importedFiles ) . toEqual ( [ '/foo.js' , '/bar.md' ] )
145+ expect ( mockConsoleError ) . toHaveBeenCalledTimes ( 2 )
122146 } )
123147
124148 it ( 'should use file ext as fallback language' , ( ) => {
@@ -139,6 +163,7 @@ ${mdFixtureContent.split('\n').slice(0, 5).join('\n').replace(/\n?$/, '\n')}\
139163
140164 expect ( rendered ) . toEqual ( expected )
141165 expect ( env . importedFiles ) . toEqual ( [ '/foo.js' , '/bar.md' ] )
166+ expect ( mockConsoleError ) . toHaveBeenCalledTimes ( 2 )
142167 } )
143168 } )
144169
@@ -164,6 +189,7 @@ ${mdFixtureContent.split('\n').slice(0, 5).join('\n').replace(/\n?$/, '\n')}\
164189 '/foo.js' ,
165190 path . resolve ( __dirname , './bar.js' ) ,
166191 ] )
192+ expect ( mockConsoleError ) . toHaveBeenCalledTimes ( 2 )
167193 } )
168194
169195 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')}\
184210
185211 expect ( rendered ) . toEqual ( expected )
186212 expect ( env . importedFiles ) . toEqual ( [ '/foo.js' ] )
213+ expect ( mockConsoleError ) . toHaveBeenCalledTimes ( 2 )
187214 } )
188215
189216 it ( 'should handle import path correctly' , ( ) => {
229256
230257 expect ( rendered ) . toEqual ( expected )
231258 expect ( env . importedFiles ) . toEqual ( [ '/path/to/foo.js' ] )
259+ expect ( mockConsoleError ) . toHaveBeenCalledTimes ( 1 )
232260 } )
233261
234262 it ( 'should terminate blockquote' , ( ) => {
251279
252280 expect ( rendered ) . toEqual ( expected )
253281 expect ( env . importedFiles ) . toEqual ( [ '/path/to/foo.js' ] )
282+ expect ( mockConsoleError ) . toHaveBeenCalledTimes ( 1 )
254283 } )
255284 } )
256285
0 commit comments