From eefa59de8ae8e439daab6238455f6b238ed323a3 Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Fri, 29 Dec 2023 14:14:07 +0800 Subject: [PATCH] test(utils): add more unit tests --- packages/utils/tests/console/formatMs.spec.ts | 18 +++++ packages/utils/tests/console/logger.spec.ts | 35 ++++------ .../utils/tests/console/withSpinner.spec.ts | 68 +++++++++++++++++++ .../utils/tests/module/isChildPath.spec.ts | 12 ++-- packages/utils/tests/ssr/renderHead.spec.ts | 12 ++-- .../utils/tests/ssr/renderHeadAttrs.spec.ts | 12 ++-- .../utils/tests/ssr/templateRenderer.spec.ts | 60 ++++++++++++++++ 7 files changed, 173 insertions(+), 44 deletions(-) create mode 100644 packages/utils/tests/console/formatMs.spec.ts create mode 100644 packages/utils/tests/console/withSpinner.spec.ts create mode 100644 packages/utils/tests/ssr/templateRenderer.spec.ts diff --git a/packages/utils/tests/console/formatMs.spec.ts b/packages/utils/tests/console/formatMs.spec.ts new file mode 100644 index 0000000000..ca8484d8bd --- /dev/null +++ b/packages/utils/tests/console/formatMs.spec.ts @@ -0,0 +1,18 @@ +import { expect, it } from 'vitest' +import { formatMs } from '../../src/index.js' + +const testCases: [source: number, expected: string][] = [ + [0, '0ms'], + [999, '999ms'], + [1000, '1.00s'], + [1001, '1.00s'], + [1999, '2.00s'], + [2000, '2.00s'], + [2001, '2.00s'], +] + +testCases.forEach(([source, expected]) => { + it(`${source} -> ${expected}`, () => { + expect(formatMs(source)).toBe(expected) + }) +}) diff --git a/packages/utils/tests/console/logger.spec.ts b/packages/utils/tests/console/logger.spec.ts index f5bcaab326..4b34ca4408 100644 --- a/packages/utils/tests/console/logger.spec.ts +++ b/packages/utils/tests/console/logger.spec.ts @@ -1,4 +1,4 @@ -import { describe, expect, it, vi } from 'vitest' +import { expect, it, vi } from 'vitest' import { logger } from '../../src/index.js' const methods = [ @@ -9,29 +9,24 @@ const methods = [ ['error', 'error'], ] -describe('utils > logger', () => { - methods.forEach(([method, innerMethod]) => { - it(method, () => { - const stored = console[innerMethod] - console[innerMethod] = vi.fn() +methods.forEach(([method, innerMethod]) => { + it(method, () => { + const stored = console[innerMethod] + console[innerMethod] = vi.fn() - logger[method]('foo') - expect(console[innerMethod]).toHaveBeenCalledWith( - expect.any(String), - 'foo', - ) + logger[method]('foo') + expect(console[innerMethod]).toHaveBeenCalledWith(expect.any(String), 'foo') - console[innerMethod] = stored - }) + console[innerMethod] = stored }) +}) - it('creteError', () => { - const stored = console.error - console.error = vi.fn() +it('creteError', () => { + const stored = console.error + console.error = vi.fn() - expect(logger.createError()).toBeInstanceOf(Error) - expect(console.error).toHaveBeenCalled() + expect(logger.createError()).toBeInstanceOf(Error) + expect(console.error).toHaveBeenCalled() - console.error = stored - }) + console.error = stored }) diff --git a/packages/utils/tests/console/withSpinner.spec.ts b/packages/utils/tests/console/withSpinner.spec.ts new file mode 100644 index 0000000000..9038516435 --- /dev/null +++ b/packages/utils/tests/console/withSpinner.spec.ts @@ -0,0 +1,68 @@ +import { beforeEach, expect, it, vi } from 'vitest' +import { withSpinner } from '../../src/index.js' + +const mocks = vi.hoisted(() => { + const start = vi.fn() + const succeed = vi.fn() + const fail = vi.fn() + const ora = vi.fn().mockReturnValue({ + start, + succeed, + fail, + }) + + return { + ora, + start, + succeed, + fail, + } +}) + +vi.mock('ora', () => ({ + default: mocks.ora, +})) + +beforeEach(() => { + Object.values(mocks).forEach((mock) => mock.mockClear()) +}) + +it('should skip spinner if DEBUG env is set', async () => { + process.env.DEBUG = 'msg' + + const target = vi.fn().mockResolvedValue('result') + + const result = await withSpinner('msg')(target) + + expect(result).toBe('result') + expect(target).toHaveBeenCalledTimes(1) + expect(mocks.ora).toHaveBeenCalledTimes(0) + + delete process.env.DEBUG +}) + +it('should call target with spinner.succeed', async () => { + const target = vi.fn().mockResolvedValue('result') + + const result = await withSpinner('msg')(target) + + expect(result).toBe('result') + expect(target).toHaveBeenCalledTimes(1) + expect(mocks.ora).toHaveBeenCalledTimes(1) + expect(mocks.start).toHaveBeenCalledWith('msg') + expect(mocks.succeed).toHaveBeenCalledTimes(1) + expect(mocks.fail).toHaveBeenCalledTimes(0) +}) + +it('should call target with spinner.fail', async () => { + const error = new Error('mock error') + const target = vi.fn().mockRejectedValue(error) + + await expect(() => withSpinner('msg')(target)).rejects.toThrowError(error) + + expect(target).toHaveBeenCalledTimes(1) + expect(mocks.ora).toHaveBeenCalledTimes(1) + expect(mocks.start).toHaveBeenCalledWith('msg') + expect(mocks.succeed).toHaveBeenCalledTimes(0) + expect(mocks.fail).toHaveBeenCalledTimes(1) +}) diff --git a/packages/utils/tests/module/isChildPath.spec.ts b/packages/utils/tests/module/isChildPath.spec.ts index 508793f48a..d77d2e9d65 100644 --- a/packages/utils/tests/module/isChildPath.spec.ts +++ b/packages/utils/tests/module/isChildPath.spec.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest' +import { expect, it } from 'vitest' import { isChildPath } from '../../src/index.js' const testCases: [[string, string], boolean][] = [ @@ -22,12 +22,8 @@ const testCases: [[string, string], boolean][] = [ [['foo-bar', 'foo'], false], ] -describe('utils > isChildPath', () => { - describe('should check child path correctly', () => { - testCases.forEach(([source, expected]) => { - it(JSON.stringify(source), () => { - expect(isChildPath(...source)).toBe(expected) - }) - }) +testCases.forEach(([source, expected]) => { + it(`${JSON.stringify(source)} -> ${expected}`, () => { + expect(isChildPath(...source)).toBe(expected) }) }) diff --git a/packages/utils/tests/ssr/renderHead.spec.ts b/packages/utils/tests/ssr/renderHead.spec.ts index 5d9a7dd3b4..035785ba72 100644 --- a/packages/utils/tests/ssr/renderHead.spec.ts +++ b/packages/utils/tests/ssr/renderHead.spec.ts @@ -1,5 +1,5 @@ import type { HeadConfig } from '@vuepress/shared' -import { describe, expect, it } from 'vitest' +import { expect, it } from 'vitest' import { renderHead } from '../../src/index.js' const testCases: [HeadConfig, string][] = [ @@ -70,12 +70,8 @@ const testCases: [HeadConfig, string][] = [ ], ] -describe('utils > renderHead', () => { - describe('should render head config correctly', () => { - testCases.forEach(([source, expected]) => { - it(expected, () => { - expect(renderHead(source)).toBe(expected) - }) - }) +testCases.forEach(([source, expected]) => { + it(expected, () => { + expect(renderHead(source)).toBe(expected) }) }) diff --git a/packages/utils/tests/ssr/renderHeadAttrs.spec.ts b/packages/utils/tests/ssr/renderHeadAttrs.spec.ts index aa268a2ec4..49df3547bc 100644 --- a/packages/utils/tests/ssr/renderHeadAttrs.spec.ts +++ b/packages/utils/tests/ssr/renderHeadAttrs.spec.ts @@ -1,5 +1,5 @@ import type { HeadAttrsConfig } from '@vuepress/shared' -import { describe, expect, it } from 'vitest' +import { expect, it } from 'vitest' import { renderHeadAttrs } from '../../src/index.js' const testCases: [HeadAttrsConfig, string][] = [ @@ -18,12 +18,8 @@ const testCases: [HeadAttrsConfig, string][] = [ ], ] -describe('utils > renderHeadAttrs', () => { - describe('should render head attrs config correctly', () => { - testCases.forEach(([source, expected]) => { - it(JSON.stringify(source), () => { - expect(renderHeadAttrs(source)).toBe(expected) - }) - }) +testCases.forEach(([source, expected]) => { + it(JSON.stringify(source), () => { + expect(renderHeadAttrs(source)).toBe(expected) }) }) diff --git a/packages/utils/tests/ssr/templateRenderer.spec.ts b/packages/utils/tests/ssr/templateRenderer.spec.ts new file mode 100644 index 0000000000..7795491144 --- /dev/null +++ b/packages/utils/tests/ssr/templateRenderer.spec.ts @@ -0,0 +1,60 @@ +import { expect, it } from 'vitest' +import type { TemplateRendererContext } from '../../src/index.js' +import { templateRenderer } from '../../src/index.js' + +const template = `\ + + + + + + + + + + + + +
+ + + + +` + +const context: TemplateRendererContext = { + content: '#content#', + head: '#head#', + lang: '#lang#', + prefetch: '#prefetch#', + preload: '#preload#', + scripts: '#scripts#', + styles: '#styles#', + version: '#version#', +} + +const expected = `\ + + + + + + + #head# + #styles# + #preload# + #prefetch# + + +
#content#
+ #scripts# + + + +` + +it('should fill template outlets correctly', () => { + const result = templateRenderer(template, context) + + expect(result).toBe(expected) +})