-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconvertFile.test.ts
More file actions
83 lines (68 loc) · 3.05 KB
/
convertFile.test.ts
File metadata and controls
83 lines (68 loc) · 3.05 KB
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
import path from 'node:path';
import {ProjectUtil} from '../util/ProjectUtil.js';
import {convertFile} from './convertFile.js';
describe('convertFile', () => {
function testFileConversion(directoryName: string, fileName = 'main', extension = 'ts') {
const fixtures = path.join(process.cwd(), 'src', 'test', 'fixtures');
const projectDir = path.join(fixtures, directoryName);
const projectConfig = path.join(projectDir, 'tsconfig.json');
const project = ProjectUtil.getProject(projectConfig);
const snapshot = path.join(projectDir, 'src', `${fileName}.snap.${extension}`);
const sourceFileName = `${fileName}.${extension}`;
const sourceFile = project.getSourceFile(sourceFileName);
if (!sourceFile) {
throw new Error(`File "${sourceFileName}" not found.`);
}
const modifiedFile = convertFile(sourceFile);
if (!modifiedFile) {
throw new Error(`File "${sourceFileName}" was not converted.`);
}
return expect(modifiedFile.getFullText()).toMatchFileSnapshot(snapshot);
}
describe('imports', () => {
it('converts imports from index files', async () => {
await testFileConversion('index-import');
});
it('converts imports when tsconfig has an "include" property', async () => {
await testFileConversion('tsconfig-include', 'consumer');
});
it('converts CJS require statements into ESM imports', async () => {
await testFileConversion('require-import');
});
it('converts dynamic imports', async () => {
await testFileConversion('dynamic-imports');
});
it('handles index files referenced with a trailing slash', async () => {
await testFileConversion('trailing-slash');
});
it('handles files with a Shebang (#!) at the beginning', async () => {
await testFileConversion('cjs-shebang');
});
it('handles named imports from require statements', async () => {
await testFileConversion('cjs-destructuring');
});
});
describe('exports', () => {
describe('CJS (module.exports) to ESM', () => {
it('converts default and named exports', async () => {
await testFileConversion('module-exports');
});
it('converts multiple named exports', async () => {
await testFileConversion('module-exports', 'multiple-named-exports');
});
it('converts object literal exports', async () => {
await testFileConversion('module-exports', 'object-literal-export');
});
it('converts object literal exports with aliases', async () => {
await testFileConversion('module-exports', 'object-literal-aliased');
});
it('converts object literal exports with mixed properties', async () => {
await testFileConversion('module-exports', 'object-literal-mixed');
});
it('handles functions exported as default from plain JavaScript files', async () => {
await testFileConversion('module-exports-function-js', 'build-example-index', 'js');
await testFileConversion('module-exports-function-js', 'build-example-index-markdown', 'js');
});
});
});
});