From 8e6560cacbac476846e2c3fc6a908029bb8d6392 Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Mon, 27 Feb 2023 20:36:09 +0800 Subject: [PATCH] test(markdown): mock error logs --- .../tests/plugins/importCodePlugin.spec.ts | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/markdown/tests/plugins/importCodePlugin.spec.ts b/packages/markdown/tests/plugins/importCodePlugin.spec.ts index 1a4dacde27..e433b2b111 100644 --- a/packages/markdown/tests/plugins/importCodePlugin.spec.ts +++ b/packages/markdown/tests/plugins/importCodePlugin.spec.ts @@ -1,6 +1,14 @@ import { fs, path } from '@vuepress/utils' import MarkdownIt from 'markdown-it' -import { describe, expect, it } from 'vitest' +import { + afterAll, + afterEach, + beforeAll, + describe, + expect, + it, + vi, +} from 'vitest' import { codePlugin, importCodePlugin } from '../../src/index.js' import type { MarkdownEnv } from '../../src/index.js' @@ -11,6 +19,21 @@ const mdFixturePath = path.resolve(__dirname, mdFixturePathRelative) const jsFixtureContent = fs.readFileSync(jsFixturePath).toString() const mdFixtureContent = fs.readFileSync(mdFixturePath).toString() +const consoleError = console.error +const mockConsoleError = vi.fn() + +beforeAll(() => { + console.error = mockConsoleError +}) + +afterEach(() => { + mockConsoleError.mockClear() +}) + +afterAll(() => { + console.error = consoleError +}) + describe('@vuepress/markdown > plugins > importCodePlugin', () => { it('should not be parsed as import code syntax', () => { const source = [ @@ -119,6 +142,7 @@ ${mdFixtureContent.split('\n').slice(0, 5).join('\n').replace(/\n?$/, '\n')}\ expect(rendered).toEqual(expected) expect(env.importedFiles).toEqual(['/foo.js', '/bar.md']) + expect(mockConsoleError).toHaveBeenCalledTimes(2) }) it('should use file ext as fallback language', () => { @@ -139,6 +163,7 @@ ${mdFixtureContent.split('\n').slice(0, 5).join('\n').replace(/\n?$/, '\n')}\ expect(rendered).toEqual(expected) expect(env.importedFiles).toEqual(['/foo.js', '/bar.md']) + expect(mockConsoleError).toHaveBeenCalledTimes(2) }) }) @@ -164,6 +189,7 @@ ${mdFixtureContent.split('\n').slice(0, 5).join('\n').replace(/\n?$/, '\n')}\ '/foo.js', path.resolve(__dirname, './bar.js'), ]) + expect(mockConsoleError).toHaveBeenCalledTimes(2) }) 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')}\ expect(rendered).toEqual(expected) expect(env.importedFiles).toEqual(['/foo.js']) + expect(mockConsoleError).toHaveBeenCalledTimes(2) }) it('should handle import path correctly', () => { @@ -229,6 +256,7 @@ foo expect(rendered).toEqual(expected) expect(env.importedFiles).toEqual(['/path/to/foo.js']) + expect(mockConsoleError).toHaveBeenCalledTimes(1) }) it('should terminate blockquote', () => { @@ -251,6 +279,7 @@ foo expect(rendered).toEqual(expected) expect(env.importedFiles).toEqual(['/path/to/foo.js']) + expect(mockConsoleError).toHaveBeenCalledTimes(1) }) })