Skip to content

Commit

Permalink
test(utils): add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
liangskyli committed May 9, 2023
1 parent c6e4053 commit 118fa78
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 13 deletions.
2 changes: 2 additions & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
11 changes: 5 additions & 6 deletions packages/utils/src/config.ts
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 1 addition & 7 deletions packages/utils/src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions packages/utils/test/config.test.ts
Original file line number Diff line number Diff line change
@@ -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/);
});
});
4 changes: 4 additions & 0 deletions packages/utils/test/example/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const config = {
a: 1,
};
export default config;
19 changes: 19 additions & 0 deletions packages/utils/test/register.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
81 changes: 81 additions & 0 deletions packages/utils/test/tools.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
10 changes: 10 additions & 0 deletions packages/utils/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": "../",
"moduleResolution": "node",
},
"include": [
"../test/**/*.ts",
]
}
8 changes: 8 additions & 0 deletions packages/utils/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
include: ['./**/*.test.{ts,js}'],
environment: 'node',
testTimeout: 1000 * 30,
},
});

0 comments on commit 118fa78

Please sign in to comment.