From 118fa78fb68bba3e049b01c078a919c5f7003053 Mon Sep 17 00:00:00 2001 From: liangsky <640634387@qq.com> Date: Tue, 9 May 2023 22:13:08 +0800 Subject: [PATCH] test(utils): add unit test --- packages/utils/package.json | 2 + packages/utils/src/config.ts | 11 ++-- packages/utils/src/tools.ts | 8 +-- packages/utils/test/config.test.ts | 12 +++++ packages/utils/test/example/a.ts | 4 ++ packages/utils/test/register.test.ts | 19 +++++++ packages/utils/test/tools.test.ts | 81 ++++++++++++++++++++++++++++ packages/utils/test/tsconfig.json | 10 ++++ packages/utils/vite.config.ts | 8 +++ 9 files changed, 142 insertions(+), 13 deletions(-) create mode 100644 packages/utils/test/config.test.ts create mode 100644 packages/utils/test/example/a.ts create mode 100644 packages/utils/test/register.test.ts create mode 100644 packages/utils/test/tools.test.ts create mode 100644 packages/utils/test/tsconfig.json create mode 100644 packages/utils/vite.config.ts diff --git a/packages/utils/package.json b/packages/utils/package.json index 24d0d67..53f35a5 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -25,6 +25,8 @@ "package.json" ], "scripts": { + "test": "vitest run", + "coverage": "vitest run --coverage", "build": "rollup --bundleConfigAsCjs --config=./rollup.config.js", "update:deps": "pnpm update --interactive --latest" }, diff --git a/packages/utils/src/config.ts b/packages/utils/src/config.ts index 64b71e4..5745ea3 100644 --- a/packages/utils/src/config.ts +++ b/packages/utils/src/config.ts @@ -1,20 +1,19 @@ -import colors from 'colors'; import { register } from './register'; const getConfig = (configFile: string) => { let config: any = null; const { unregister } = register({ key: 'utils-getConfig' }); + let errInfo: any = null; try { config = require(configFile).default; } catch (err: any) { - if ((err.details || err.message).indexOf('Cannot find') > -1) { - console.info(colors.red('配置文件找不到,请检查:'), configFile); - } else { - throw err; - } + errInfo = err; } finally { unregister(); } + if (errInfo !== null) { + throw errInfo; + } return config; }; export default getConfig; diff --git a/packages/utils/src/tools.ts b/packages/utils/src/tools.ts index 82baf6a..52aea9d 100644 --- a/packages/utils/src/tools.ts +++ b/packages/utils/src/tools.ts @@ -19,13 +19,7 @@ export const prettierData = ( fileContent: string, options?: prettier.Options, ) => { - let configFile; - try { - configFile = prettier.resolveConfigFile.sync(); - } catch { - configFile = null; - } - + const configFile = prettier.resolveConfigFile.sync(); let configFileOptions: prettier.Options | null = null; if (configFile !== null) { configFileOptions = prettier.resolveConfig.sync(configFile); diff --git a/packages/utils/test/config.test.ts b/packages/utils/test/config.test.ts new file mode 100644 index 0000000..03d0d84 --- /dev/null +++ b/packages/utils/test/config.test.ts @@ -0,0 +1,12 @@ +import { describe, expect, test } from 'vitest'; +import { getAbsolutePath } from '../src'; +import getConfig from '../src/config'; + +describe('config', () => { + test('getConfig', () => { + expect(getConfig(getAbsolutePath('./test/example/a.ts'))).toEqual({ a: 1 }); + expect(() => + getConfig(getAbsolutePath('./test/example/not-exist.ts')), + ).toThrow(/Cannot find module/); + }); +}); diff --git a/packages/utils/test/example/a.ts b/packages/utils/test/example/a.ts new file mode 100644 index 0000000..951a517 --- /dev/null +++ b/packages/utils/test/example/a.ts @@ -0,0 +1,4 @@ +const config = { + a: 1, +}; +export default config; diff --git a/packages/utils/test/register.test.ts b/packages/utils/test/register.test.ts new file mode 100644 index 0000000..f7b8d89 --- /dev/null +++ b/packages/utils/test/register.test.ts @@ -0,0 +1,19 @@ +import { describe, expect, test } from 'vitest'; +import { register } from '../src'; + +describe('register', () => { + test('register', () => { + register.register({ key: '1' }); + register.register({ key: '2' }); + expect(() => register.register({ key: '2' })).toThrow( + 'register key have exist!', + ); + register.unregister('2'); + register.register({ key: '2' }); + register.restore(); + const register1 = register.register({ key: '1' }); + register.register({ key: '2' }); + register1.unregister(); + register.restore(); + }); +}); diff --git a/packages/utils/test/tools.test.ts b/packages/utils/test/tools.test.ts new file mode 100644 index 0000000..1bc81e5 --- /dev/null +++ b/packages/utils/test/tools.test.ts @@ -0,0 +1,81 @@ +import fs from 'fs-extra'; +import path from 'path'; +import { describe, expect, test } from 'vitest'; +import { + copyOptions, + getAbsolutePath, + getRelativePath, + prettierData, + removeFilesSync, + winPath, +} from '../src'; + +describe('utils', () => { + test('getAbsolutePath', () => { + expect(getAbsolutePath('/a/b')).toBe('/a/b'); + expect(winPath(getAbsolutePath('./a/b'))).toContain('/a/b'); + }); + test('getRelativePath', () => { + expect(getRelativePath('/a/b', '/a/b/c')).toBe('c'); + expect(getRelativePath('/a/b', '/a')).toBe('..'); + expect(winPath(getRelativePath('/a/b/c/d', '/a/b/e'))).toBe('../../e'); + }); + test('winPath', () => { + expect(winPath('\\a\\b')).toEqual('/a/b'); + expect(winPath('\\你好\\欢迎')).toEqual('/你好/欢迎'); + expect(winPath('\\$\\%')).toEqual('/$/%'); + // not convert extended-length paths + const path = '\\\\?\\c:\\aaaa\\bbbb'; + expect(winPath(path)).toEqual(path); + }); + test('prettierData', () => { + expect(prettierData('const a=1')).toEqual('const a = 1;\n'); + expect(prettierData('const a=\'1\'', { singleQuote: false })).toEqual( + 'const a = "1";\n', + ); + }); + test('copyOptions', () => { + expect(copyOptions(undefined)).toEqual(undefined); + const obj1 = { a: 1 }; + const obj2 = copyOptions(obj1); + expect(obj1).toEqual({ a: 1 }); + expect(obj1).toEqual({ a: 1 }); + obj2.a = 2; + expect(obj1).toEqual({ a: 1 }); + expect(obj2).toEqual({ a: 2 }); + }); + test('removeFilesSync, not dir and custom-data dir', () => { + const tempDir = './test/temp1'; + expect(fs.existsSync(tempDir)).toBeFalsy(); + removeFilesSync(tempDir); + expect(fs.existsSync(tempDir)).toBeTruthy(); + fs.removeSync(tempDir); + }); + test('removeFilesSync, have dir, not custom-data dir', () => { + const tempDir = './test/temp1'; + const otherDir = path.join(tempDir, 'other'); + const otherFile = path.join(tempDir, 'a.txt'); + fs.mkdirsSync(tempDir); + fs.mkdirsSync(otherDir); + fs.writeFileSync(otherFile, 'otherFile'); + removeFilesSync(tempDir); + expect(fs.existsSync(otherDir)).toBeFalsy(); + expect(fs.existsSync(otherFile)).toBeFalsy(); + fs.removeSync(tempDir); + }); + test('removeFilesSync, have custom-data dir', () => { + const tempDir = './test/temp2'; + const customDataDir = path.join(tempDir, 'custom-data'); + const otherDir = path.join(tempDir, 'other'); + const otherFile = path.join(tempDir, 'a.txt'); + fs.mkdirsSync(tempDir); + fs.mkdirsSync(customDataDir); + fs.mkdirsSync(otherDir); + fs.writeFileSync(otherFile, 'otherFile'); + removeFilesSync(tempDir); + expect(fs.existsSync(tempDir)).toBeTruthy(); + expect(fs.existsSync(customDataDir)).toBeTruthy(); + expect(fs.existsSync(otherFile)).toBeFalsy(); + fs.removeSync(tempDir); + }); +}); diff --git a/packages/utils/test/tsconfig.json b/packages/utils/test/tsconfig.json new file mode 100644 index 0000000..dc4cd63 --- /dev/null +++ b/packages/utils/test/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "rootDir": "../", + "moduleResolution": "node", + }, + "include": [ + "../test/**/*.ts", + ] +} diff --git a/packages/utils/vite.config.ts b/packages/utils/vite.config.ts new file mode 100644 index 0000000..7d4ddc4 --- /dev/null +++ b/packages/utils/vite.config.ts @@ -0,0 +1,8 @@ +import { defineConfig } from 'vitest/config'; +export default defineConfig({ + test: { + include: ['./**/*.test.{ts,js}'], + environment: 'node', + testTimeout: 1000 * 30, + }, +});