Skip to content

Commit 39a1222

Browse files
committed
test: add dynamic import code test for transpile-module
1 parent 5a21aca commit 39a1222

File tree

1 file changed

+127
-45
lines changed

1 file changed

+127
-45
lines changed

src/transpilers/typescript/transpile-module.spec.ts

Lines changed: 127 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -57,39 +57,62 @@ describe('transpileModules', () => {
5757
const mtsFilePath = path.join(workspaceRoot, 'foo.mts')
5858
const tsFilePathInCjsModernNode = path.join(workspaceRoot, 'cjs-node-modern', 'foo.ts')
5959
const ctsFilePath = path.join(workspaceRoot, 'foo.cts')
60-
vol.fromJSON(
61-
{
62-
'./esm-node-modern/package.json': JSON.stringify({
63-
name: 'test-package-1',
64-
type: 'module',
65-
} as PackageJson),
66-
'./esm-node-modern/foo.ts': `
60+
beforeEach(() => {
61+
vol.reset()
62+
vol.fromJSON(
63+
{
64+
'./esm-node-modern/package.json': JSON.stringify({
65+
name: 'test-package-1',
66+
type: 'module',
67+
} as PackageJson),
68+
'./esm-node-modern/foo.ts': `
6769
import { foo } from 'foo';
6870
6971
console.log(foo);
72+
const loadFooAsync = async () => {
73+
const fooDefault = await import('foo');
74+
console.log(fooDefault);
75+
}
76+
console.log(loadFooAsync());
7077
`,
71-
'./foo.mts': `
78+
'./foo.mts': `
7279
import { foo } from 'foo';
7380
7481
console.log(foo);
82+
const loadFooAsync = async () => {
83+
const fooDefault = await import('foo');
84+
console.log(fooDefault);
85+
}
86+
console.log(loadFooAsync());
7587
`,
76-
'./cjs-node-modern/package.json': JSON.stringify({
77-
name: 'test-package-1',
78-
type: 'commonjs',
79-
} as PackageJson),
80-
'./cjs-node-modern/foo.ts': `
88+
'./cjs-node-modern/package.json': JSON.stringify({
89+
name: 'test-package-1',
90+
type: 'commonjs',
91+
} as PackageJson),
92+
'./cjs-node-modern/foo.ts': `
8193
import { foo } from 'foo';
8294
8395
console.log(foo);
96+
const loadFooAsync = async () => {
97+
const fooDefault = await import('foo');
98+
console.log(fooDefault);
99+
}
100+
console.log(loadFooAsync());
84101
`,
85-
'./foo.cts': `
102+
'./foo.cts': `
86103
import { foo } from 'foo';
87104
88105
console.log(foo);
106+
const loadFooAsync = async () => {
107+
const fooDefault = await import('foo');
108+
console.log(fooDefault);
109+
}
110+
console.log(loadFooAsync());
89111
`,
90-
},
91-
workspaceRoot,
92-
)
112+
},
113+
workspaceRoot,
114+
)
115+
})
93116

94117
it.each([
95118
{
@@ -110,6 +133,12 @@ describe('transpileModules', () => {
110133

111134
expect(omitLeadingWhitespace(result.outputText)).toContain(dedent`
112135
const foo_1 = require("foo");
136+
console.log(foo_1.foo);
137+
const loadFooAsync = async () => {
138+
const fooDefault = await import('foo');
139+
console.log(fooDefault);
140+
};
141+
console.log(loadFooAsync());
113142
`)
114143
})
115144

@@ -131,32 +160,50 @@ describe('transpileModules', () => {
131160

132161
expect(omitLeadingWhitespace(result.outputText)).toContain(dedent`
133162
import { foo } from 'foo';
163+
console.log(foo);
164+
const loadFooAsync = async () => {
165+
const fooDefault = await import('foo');
166+
console.log(fooDefault);
167+
};
168+
console.log(loadFooAsync());
134169
`)
135170
})
136171

137172
it.each([
138-
{
139-
module: ts.ModuleKind.CommonJS,
140-
expectedResult: dedent`
141-
const foo_1 = require("foo");
142-
`,
143-
},
144173
{
145174
module: ts.ModuleKind.Node16,
146175
expectedResult: dedent`
147176
import { foo } from 'foo';
177+
console.log(foo);
178+
const loadFooAsync = async () => {
179+
const fooDefault = await import('foo');
180+
console.log(fooDefault);
181+
};
182+
console.log(loadFooAsync());
148183
`,
149184
},
150185
{
151186
module: ts.ModuleKind.ES2020,
152187
expectedResult: dedent`
153188
import { foo } from 'foo';
189+
console.log(foo);
190+
const loadFooAsync = async () => {
191+
const fooDefault = await import('foo');
192+
console.log(fooDefault);
193+
};
194+
console.log(loadFooAsync());
154195
`,
155196
},
156197
{
157198
module: undefined,
158199
expectedResult: dedent`
159200
import { foo } from 'foo';
201+
console.log(foo);
202+
const loadFooAsync = async () => {
203+
const fooDefault = await import('foo');
204+
console.log(fooDefault);
205+
};
206+
console.log(loadFooAsync());
160207
`,
161208
},
162209
])('should emit code with ".mts" extension respecting module option', ({ module, expectedResult }) => {
@@ -172,28 +219,40 @@ describe('transpileModules', () => {
172219
})
173220

174221
it.each([
175-
{
176-
module: ts.ModuleKind.CommonJS,
177-
expectedResult: dedent`
178-
const foo_1 = require("foo");
179-
`,
180-
},
181222
{
182223
module: ts.ModuleKind.Node16,
183224
expectedResult: dedent`
184225
const foo_1 = require("foo");
226+
console.log(foo_1.foo);
227+
const loadFooAsync = async () => {
228+
const fooDefault = await import('foo');
229+
console.log(fooDefault);
230+
};
231+
console.log(loadFooAsync());
185232
`,
186233
},
187234
{
188235
module: ts.ModuleKind.ES2020,
189236
expectedResult: dedent`
190237
import { foo } from 'foo';
238+
console.log(foo);
239+
const loadFooAsync = async () => {
240+
const fooDefault = await import('foo');
241+
console.log(fooDefault);
242+
};
243+
console.log(loadFooAsync());
191244
`,
192245
},
193246
{
194247
module: undefined,
195248
expectedResult: dedent`
196249
import { foo } from 'foo';
250+
console.log(foo);
251+
const loadFooAsync = async () => {
252+
const fooDefault = await import('foo');
253+
console.log(fooDefault);
254+
};
255+
console.log(loadFooAsync());
197256
`,
198257
},
199258
])('should emit code with ".cts" extension respecting module option', ({ module, expectedResult }) => {
@@ -210,19 +269,28 @@ describe('transpileModules', () => {
210269
})
211270

212271
describe('with classic CommonJS module and ES module kind', () => {
213-
vol.fromJSON(
214-
{
215-
'./foo.ts': `
272+
const filePath = path.join(workspaceRoot, 'bar.ts')
273+
274+
beforeEach(() => {
275+
vol.reset()
276+
vol.fromJSON(
277+
{
278+
'./bar.ts': `
216279
import { foo } from 'foo';
217280
218281
console.log(foo);
282+
const loadFooAsync = async () => {
283+
const fooDefault = await import('foo');
284+
console.log(fooDefault);
285+
}
286+
console.log(loadFooAsync());
219287
`,
220-
},
221-
workspaceRoot,
222-
)
288+
},
289+
workspaceRoot,
290+
)
291+
})
223292

224293
it('should emit CJS code with module kind set to CommonJS', () => {
225-
const filePath = path.join(workspaceRoot, 'foo.ts')
226294
const result = tsTranspileModule(vol.readFileSync(filePath, 'utf-8').toString(), {
227295
fileName: filePath,
228296
compilerOptions: {
@@ -232,12 +300,17 @@ describe('transpileModules', () => {
232300
})
233301

234302
expect(omitLeadingWhitespace(result.outputText)).toContain(dedent`
235-
const foo_1 = require("foo");
303+
const foo_1 = require("foo");
304+
console.log(foo_1.foo);
305+
const loadFooAsync = async () => {
306+
const fooDefault = await Promise.resolve().then(() => require('foo'));
307+
console.log(fooDefault);
308+
};
309+
console.log(loadFooAsync());
236310
`)
237311
})
238312

239313
it('should emit ESM code with module kind set to one of ES module value', () => {
240-
const filePath = path.join(workspaceRoot, 'foo.ts')
241314
const result = tsTranspileModule(vol.readFileSync(filePath, 'utf-8').toString(), {
242315
fileName: filePath,
243316
compilerOptions: {
@@ -247,23 +320,32 @@ describe('transpileModules', () => {
247320
})
248321

249322
expect(omitLeadingWhitespace(result.outputText)).toContain(dedent`
250-
import { foo } from 'foo';
323+
import { foo } from 'foo';
324+
console.log(foo);
325+
const loadFooAsync = async () => {
326+
const fooDefault = await import('foo');
327+
console.log(fooDefault);
328+
};
329+
console.log(loadFooAsync());
251330
`)
252331
})
253332
})
254333

255334
describe('with diagnostics', () => {
256335
const testFilePath = path.join(workspaceRoot, 'foo.ts')
257-
vol.fromJSON(
258-
{
259-
'./foo.ts': `
336+
beforeEach(() => {
337+
vol.reset()
338+
vol.fromJSON(
339+
{
340+
'./foo.ts': `
260341
import { foo } from 'foo';
261342
262343
console.log(foo);
263344
`,
264-
},
265-
workspaceRoot,
266-
)
345+
},
346+
workspaceRoot,
347+
)
348+
})
267349

268350
it('should return diagnostics for invalid combination of compiler options', () => {
269351
const result = tsTranspileModule(vol.readFileSync(testFilePath, 'utf-8').toString(), {

0 commit comments

Comments
 (0)