UNPKG

1.81 kBJavaScriptView Raw
1const calculateAliasConfig = require('../packages/utils/calculateAliasConfig');
2
3//https://jestjs.io/docs/zh-Hans/mock-functions
4jest.mock('../packages/utils/calculateAliasConfig');
5
6//https://stackoverflow.com/questions/55906920/how-to-mock-the-process-cwd-function-with-jest
7const process = require('process');
8const spy = jest.spyOn(process, 'cwd');
9spy.mockReturnValue('/Users/blue/work/anu/packages/cli/packages/demo');
10
11describe('calculate using components path', () => {
12
13 let sourcePath = '/Users/blue/work/anu/packages/cli/packages/demo/source/pages/syntax/await/index.js';
14
15 //using components path 测试
16 //import Cat from '@componenet/Cat/index';
17 test('import Cat from \'@components/Cat/index\';', ()=>{
18 const calculateComponentsPath = require('../packages/utils/calculateComponentsPath');
19 calculateAliasConfig.mockReturnValue({
20 '@components': '/Users/blue/work/anu/packages/cli/packages/demo/source/components'
21 });
22 expect(
23 calculateComponentsPath({
24 sourcePath: sourcePath,
25 source: '@components/Cat/index'
26 })
27 ).toBe('/components/Cat/index');
28 });
29
30 //import Dog from '@syntaxComponents/Dog/index';
31 test('import Dog from \'@syntaxComponents/Dog/index\';', ()=>{
32 const calculateComponentsPath = require('../packages/utils/calculateComponentsPath');
33 calculateAliasConfig.mockReturnValue({
34 '@syntaxComponents': '/Users/blue/work/anu/packages/cli/packages/demo/source/pages/syntax/components'
35 });
36 expect(
37 calculateComponentsPath({
38 sourcePath: sourcePath,
39 source: '@syntaxComponents/Dog/index'
40 })
41 ).toBe('/pages/syntax/components/Dog/index');
42
43 });
44
45});
\No newline at end of file