This repository was archived by the owner on Nov 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpreAnalyze.test.ts
57 lines (53 loc) · 1.97 KB
/
preAnalyze.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
import { expect, it, describe } from 'vitest';
import * as path from 'path';
import { fileURLToPath } from 'url';
import { analyzeImports, getImportPath, resolveId, type Alias } from '../src/service/analyze';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
describe('resolveId', () => {
it('alias: { ice: \'/.ice/runApp\'}; id: ice', () => {
const alias = { ice: '/.ice/runApp' };
const id = resolveId('ice', alias);
expect(id).toBe('/.ice/runApp');
});
it('alias: { ice: \'/.ice/runApp\'}; id: ice/test', () => {
const alias = { ice: '/.ice/runApp' };
const id = resolveId('ice/test', alias);
expect(id).toBe('/.ice/runApp/test');
});
it('alias: { ice$: \'/.ice/runApp\'}; id: ice/test', () => {
const alias = { 'ice$': '/.ice/runApp' };
const id = resolveId('ice/test', alias);
expect(id).toBe('ice/test');
});
it('alias: { ice: false}; id: false', () => {
const alias = { ice: false } as Alias;
const id = resolveId('ice', alias);
expect(id).toBe(false);
});
});
describe('getImportPath', () => {
it('import from relative path', () => {
const filePath = getImportPath('./page.js', '/rootDir/test.ts', {});
expect(filePath).toBe('/rootDir/page.js');
});
it('import from alias', () => {
const filePath = getImportPath('ice', '/rootDir/test.ts', { ice: '/rootDir/component.tsx'});
expect(filePath).toBe('/rootDir/component.tsx');
});
it('import node_module dependency', () => {
const filePath = getImportPath('ice', '/rootDir/test.ts', {});
expect(filePath).toBe('');
});
});
describe('analyzeImports', () => {
it('basic usage', async () => {
const entryFile = path.join(__dirname, './fixtures/preAnalyze/app.ts');
const analyzeSet = await analyzeImports([entryFile], {
analyzeRelativeImport: true,
alias: {
'@': path.join(__dirname, './fixtures/preAnalyze'),
},
});
expect([...(analyzeSet || [])]).toStrictEqual(['runApp', 'request', 'store']);
})
});