This repository was archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.spec.ts
163 lines (151 loc) · 4.95 KB
/
main.spec.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import * as fs from 'fs';
import sortImports from 'import-sort';
import * as path from 'path';
const parser = path.resolve(__dirname, '../node_modules/import-sort-parser-typescript/lib/index.js');
const style = path.resolve(__dirname, '../src/index.ts');
const options = { prefixes: ['@lib', '@app'] };
describe('Within blocks', () => {
describe('of side effect imports', () => {
describe('with absolute paths to third-party modules', () => {
it('are not sorted', () => {
const unsortedCode = `
import 'c';
import 'a';
import 'b';
`;
const sortedCode = executeSort(unsortedCode);
exectImportsToBeEqual(sortedCode, unsortedCode);
});
});
describe('with absolute paths to local modules', () => {
it('are not sorted', () => {
const unsortedCode = `
import '@lib/b2';
import '@app/a1';
import '@lib/b1';
`;
const sortedCode = executeSort(unsortedCode);
exectImportsToBeEqual(sortedCode, unsortedCode);
});
});
describe('with relative paths', () => {
it('are not sorted', () => {
const unsortedCode = `
import './c';
import './a';
import './b';
`;
const sortedCode = executeSort(unsortedCode);
exectImportsToBeEqual(sortedCode, unsortedCode);
});
});
});
describe('of regular imports', () => {
describe('with absolute paths to native Node.js modules', () => {
it('are sorted alphabetically', () => {
const unsortedCode = `
import * as path from 'path';
import * as fs from 'fs';
import * as http from 'http';
`;
const sortedCode = executeSort(unsortedCode);
exectImportsToBeEqual(sortedCode, `
import * as fs from 'fs';
import * as http from 'http';
import * as path from 'path';
`);
});
});
describe('with absolute paths to third-party modules', () => {
it('are sorted alphabetically', () => {
const unsortedCode = `
import cc from 'cc';
import aa from 'aa';
import bb from 'bb';
`;
const sortedCode = executeSort(unsortedCode);
exectImportsToBeEqual(sortedCode, `
import aa from 'aa';
import bb from 'bb';
import cc from 'cc';
`);
});
});
describe('with absolute paths to local modules', () => {
it('are sorted by prefix order and then alphabetically', () => {
const unsortedCode = `
import { a3 } from '@app/a3';
import { a1 } from '@app/a1';
import { b3 } from '@lib/b3';
import { b1 } from '@lib/b1';
import { b2 } from '@lib/b2';
import { a2 } from '@app/a2';
`;
const sortedCode = executeSort(unsortedCode);
exectImportsToBeEqual(sortedCode, `
import { b1 } from '@lib/b1';
import { b2 } from '@lib/b2';
import { b3 } from '@lib/b3';
import { a1 } from '@app/a1';
import { a2 } from '@app/a2';
import { a3 } from '@app/a3';
`);
});
});
describe('with relative paths', () => {
it('are sorted by path and then alphabetically', () => {
const unsortedCode = `
import bbb from '../../bbb';
import bbbb from '../bbbb';
import aaaaa from './aaaaa';
import bbbbb from './bbbbb';
import aaaa from '../aaaa';
import aaa from '../../aaa';
`;
const sortedCode = executeSort(unsortedCode);
exectImportsToBeEqual(sortedCode, `
import aaa from '../../aaa';
import bbb from '../../bbb';
import aaaa from '../aaaa';
import bbbb from '../bbbb';
import aaaaa from './aaaaa';
import bbbbb from './bbbbb';
`);
});
});
});
});
describe('Between blocks', () => {
it('the correct order is applied', () => {
const unsortedCode = `
import z from 'z';
import a from '@app/a';
import * as fs from 'fs';
import './a';
import a from 'a';
import '@app/a';
import a from './a';
import 'a';
`;
const sortedCode = executeSort(unsortedCode);
exectImportsToBeEqual(sortedCode, `
import 'a';
import '@app/a';
import './a';
import a from 'a';
import z from 'z';
import * as fs from 'fs';
import a from '@app/a';
import a from './a';
`);
});
});
function executeSort(unsortedCode: string): string {
return sortImports(unsortedCode, parser, style, undefined, options).code;
}
function exectImportsToBeEqual(code1: string, code2: string) {
const indentRegex = /\n */g;
const trimmedCode1 = code1.replace(indentRegex, '\n').trim();
const trimmedCode2 = code2.replace(indentRegex, '\n').trim();
expect(trimmedCode1).toBe(trimmedCode2);
}