forked from umijs/father
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.test.ts
108 lines (89 loc) · 2.67 KB
/
utils.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { logger as umiLogger } from '@umijs/utils';
import {
getDepPkgName,
isBuiltInModule,
isFilePath,
logger,
} from '../src/utils';
jest.mock('@umijs/utils', () => {
const originalModule = jest.requireActual('@umijs/utils');
return {
...originalModule,
logger: {
...originalModule.logger,
info: jest.fn(),
},
};
});
afterAll(() => {
jest.unmock('@umijs/utils');
});
describe('logger', () => {
test('normal', () => {
logger.info('normal');
expect(umiLogger.info).toBeCalledWith('normal');
});
test('quiet only', () => {
logger.setQuiet(false);
logger.quietOnly.info('quiet only');
expect(umiLogger.info).not.toBeCalledWith('quiet only');
logger.setQuiet(true);
logger.quietOnly.info('quiet only');
expect(umiLogger.info).toBeCalledWith('quiet only');
});
test('quiet expect', () => {
logger.setQuiet(false);
logger.quietExpect.info('quiet expect');
expect(umiLogger.info).toBeCalledWith('quiet expect');
logger.setQuiet(true);
logger.quietExpect.info('quiet expect');
expect(umiLogger.info).toBeCalledWith('quiet expect');
});
test('prefix time', () => {
const originalEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'development';
logger.info('prefix time');
expect(umiLogger.info).toBeCalledWith(
expect.stringMatching(/(\d{2}:){2}\d{2}/),
'prefix time',
);
process.env.NODE_ENV = originalEnv;
});
describe(isFilePath.name, () => {
test('absolute', () => {
expect(isFilePath('/path/to/name')).toBe(true);
});
test('relative', () => {
expect(isFilePath('./name/test')).toBe(true);
});
test('module', () => {
expect(isFilePath('name')).toBe(false);
expect(isFilePath('@scope/name')).toBe(false);
});
});
describe(getDepPkgName.name, () => {
test('normal module', () => {
expect(getDepPkgName('name', { name: 'test' })).toBe('name');
expect(getDepPkgName('name/test', { name: 'test' })).toBe('name');
});
test('scope module', () => {
expect(getDepPkgName('@scope/name', { name: 'test' })).toBe(
'@scope/name',
);
expect(getDepPkgName('@scope/name/test', { name: 'test' })).toBe(
'@scope/name',
);
});
test('absolute', () => {
expect(getDepPkgName('/path/to/name', { name: 'test' })).toBe('test');
});
test('relative', () => {
expect(getDepPkgName('./name/test', { name: 'test' })).toBe('test');
});
});
test(isBuiltInModule.name, () => {
expect(isBuiltInModule('node:path')).toBeTruthy();
expect(isBuiltInModule('path')).toBeTruthy();
expect(isBuiltInModule('minimatch')).toBeFalsy();
});
});