Skip to content

Commit

Permalink
test(markdown): mock error logs
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Feb 27, 2023
1 parent b722a62 commit 8e6560c
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion packages/markdown/tests/plugins/importCodePlugin.spec.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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 = [
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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)
})
})

Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand All @@ -251,6 +279,7 @@ foo

expect(rendered).toEqual(expected)
expect(env.importedFiles).toEqual(['/path/to/foo.js'])
expect(mockConsoleError).toHaveBeenCalledTimes(1)
})
})

Expand Down

0 comments on commit 8e6560c

Please sign in to comment.