Skip to content

Commit eeaefd6

Browse files
committed
test: separate tsconfig tests
1 parent 9cf2115 commit eeaefd6

File tree

5 files changed

+215
-193
lines changed

5 files changed

+215
-193
lines changed

tests/fixtures.ts

Lines changed: 34 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const React = {
66
createElement: (...args) => Array.from(args),
77
};
88
`;
9-
const jsxCheck = '<><div>JSX</div></>';
9+
10+
export const jsxCheck = '<><div>JSX</div></>';
1011

1112
const preserveName = `
1213
assert(
@@ -83,7 +84,39 @@ const sourcemap = {
8384
},
8485
};
8586

87+
export const expectErrors = {
88+
'expect-errors.js': `
89+
export const expectErrors = async (...assertions) => {
90+
let errors = await Promise.all(
91+
assertions.map(async ([fn, expectedError]) => {
92+
let thrown;
93+
try {
94+
await fn();
95+
} catch (error) {
96+
thrown = error;
97+
}
98+
99+
if (!thrown) {
100+
return new Error('No error thrown');
101+
} else if (!thrown.message.includes(expectedError)) {
102+
return new Error(\`Message \${JSON.stringify(expectedError)} not found in \${JSON.stringify(thrown.message)}\n\${thrown.stack}\`);
103+
}
104+
}),
105+
);
106+
107+
errors = errors.filter(Boolean);
108+
109+
if (errors.length > 0) {
110+
console.error(errors);
111+
process.exitCode = 1;
112+
}
113+
};
114+
`,
115+
};
116+
86117
export const files = {
118+
...expectErrors,
119+
87120
'js/index.js': `
88121
import assert from 'assert';
89122
${syntaxLowering}
@@ -188,44 +221,8 @@ export const files = {
188221
${sourcemap.test('cts')}
189222
`,
190223

191-
'expect-errors.js': `
192-
export const expectErrors = async (...assertions) => {
193-
let errors = await Promise.all(
194-
assertions.map(async ([fn, expectedError]) => {
195-
let thrown;
196-
try {
197-
await fn();
198-
} catch (error) {
199-
thrown = error;
200-
}
201-
202-
if (!thrown) {
203-
return new Error('No error thrown');
204-
} else if (!thrown.message.includes(expectedError)) {
205-
return new Error(\`Message \${JSON.stringify(expectedError)} not found in \${JSON.stringify(thrown.message)}\n\${thrown.stack}\`);
206-
}
207-
}),
208-
);
209-
210-
errors = errors.filter(Boolean);
211-
212-
if (errors.length > 0) {
213-
console.error(errors);
214-
process.exitCode = 1;
215-
}
216-
};
217-
`,
218-
219224
'file.txt': 'hello',
220225

221-
'import-typescript-parent.js': sourcemap.tag`
222-
import './import-typescript-child.js';
223-
`,
224-
225-
'import-typescript-child.ts': sourcemap.tag`
226-
console.log('imported');
227-
`,
228-
229226
'broken-syntax.ts': 'if',
230227

231228
node_modules: {
@@ -244,87 +241,4 @@ export const files = {
244241
'empty-export/index.js': 'export {}',
245242
},
246243
},
247-
248-
tsconfig: {
249-
'file.ts': '',
250-
251-
'jsx.jsx': `
252-
// tsconfig not applied to jsx because allowJs is not set
253-
import { expectErrors } from '../expect-errors';
254-
expectErrors(
255-
[() => ${jsxCheck}, 'React is not defined'],
256-
257-
// These should throw unless allowJs is set
258-
// [() => import ('prefix/file'), "Cannot find package 'prefix'"],
259-
// [() => import ('paths-exact-match'), "Cannot find package 'paths-exact-match'"],
260-
// [() => import ('file'), "Cannot find package 'file'"],
261-
);
262-
`,
263-
264-
'node_modules/tsconfig-should-not-apply': {
265-
'package.json': JSON.stringify({
266-
exports: {
267-
import: './index.mjs',
268-
default: './index.cjs',
269-
},
270-
}),
271-
'index.mjs': `
272-
import { expectErrors } from '../../../expect-errors';
273-
expectErrors(
274-
[() => import ('prefix/file'), "Cannot find package 'prefix'"],
275-
[() => import ('paths-exact-match'), "Cannot find package 'paths-exact-match'"],
276-
[() => import ('file'), "Cannot find package 'file'"],
277-
);
278-
`,
279-
'index.cjs': `
280-
const { expectErrors } = require('../../../expect-errors');
281-
expectErrors(
282-
[() => require('prefix/file'), "Cannot find module"],
283-
[() => require('paths-exact-match'), "Cannot find module"],
284-
[() => require('file'), "Cannot find module"],
285-
);
286-
`,
287-
},
288-
289-
'index.tsx': `
290-
${jsxCheck};
291-
292-
import './jsx';
293-
294-
// Resolves relative to baseUrl
295-
import 'file';
296-
297-
// Resolves paths - exact match
298-
import 'paths-exact-match';
299-
300-
// Resolves paths - prefix match
301-
import 'prefix/file';
302-
303-
// Resolves paths - suffix match
304-
import 'file/suffix';
305-
306-
// tsconfig should not apply to dependency
307-
import "tsconfig-should-not-apply";
308-
`,
309-
310-
'tsconfig.json': JSON.stringify({
311-
compilerOptions: {
312-
jsxFactory: 'Array',
313-
jsxFragmentFactory: 'null',
314-
baseUrl: '.',
315-
paths: {
316-
'paths-exact-match': ['file'],
317-
'prefix/*': ['*'],
318-
'*/suffix': ['*'],
319-
},
320-
},
321-
}),
322-
323-
'tsconfig-allowJs.json': JSON.stringify({
324-
extends: './tsconfig.json',
325-
compilerOptions: {
326-
allowJs: true,
327-
},
328-
}),
329-
},
330244
};

tests/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { nodeVersions } from './utils/node-versions';
1515
await runTestSuite(import('./specs/watch'), node);
1616
await runTestSuite(import('./specs/loaders'), node);
1717
await runTestSuite(import('./specs/smoke'), node);
18+
await runTestSuite(import('./specs/tsconfig'), node);
1819
});
1920
}
2021
});

tests/specs/smoke.ts

Lines changed: 32 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,19 @@ import type { NodeApis } from '../utils/tsx.js';
77
import { hasCoverageSourcesContent } from '../utils/coverage-sources-content.js';
88
import { isWindows } from '../utils/is-windows.js';
99
import { files } from '../fixtures.js';
10+
import { packageTypes } from '../utils/package-types.js';
1011

1112
const wasmPath = path.resolve('tests/fixtures/test.wasm');
1213
const wasmPathUrl = pathToFileURL(wasmPath).toString();
1314

14-
const packageTypes = [
15-
'module',
16-
'commonjs',
17-
] as const;
18-
1915
export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
2016
describe('Smoke', ({ describe }) => {
2117
for (const packageType of packageTypes) {
2218
const isCommonJs = packageType === 'commonjs';
2319

24-
describe(packageType, ({ test, describe }) => {
25-
test('from .js', async ({ onTestFinish, onTestFail }) => {
26-
const fixture = await createFixture({
20+
describe(packageType, ({ test }) => {
21+
test('from .js', async ({ onTestFail }) => {
22+
await using fixture = await createFixture({
2723
'package.json': JSON.stringify({ type: packageType }),
2824
'import-from-js.js': outdent`
2925
import assert from 'assert';
@@ -133,7 +129,6 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
133129
`,
134130
...files,
135131
});
136-
onTestFinish(async () => await fixture.rm());
137132

138133
const p = await tsx(['import-from-js.js'], fixture.path);
139134
onTestFail((error) => {
@@ -158,8 +153,8 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
158153
expect(p.stderr).toBe('');
159154
});
160155

161-
describe('from .ts', async ({ test, onFinish }) => {
162-
const fixture = await createFixture({
156+
test('from .ts', async ({ onTestFail }) => {
157+
await using fixture = await createFixture({
163158
'package.json': JSON.stringify({ type: packageType }),
164159

165160
'import-from-ts.ts': ({ fixturePath }) => outdent`
@@ -333,73 +328,37 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
333328
`,
334329
...files,
335330
});
336-
onFinish(async () => await fixture.rm());
337-
338-
test('import all', async ({ onTestFail }) => {
339-
const p = await tsx(['import-from-ts.ts'], {
340-
cwd: fixture.path,
341-
env: {
342-
NODE_V8_COVERAGE: 'coverage',
343-
},
344-
});
345-
onTestFail((error) => {
346-
console.error(error);
347-
console.log(p);
348-
});
349-
expect(p.failed).toBe(false);
350-
expect(p.stdout).toMatch(`"import.meta.url":"${pathToFileURL(fixture.getPath('import-from-ts.ts'))}"`);
351-
expect(p.stdout).toMatch(`"js":{"cjsContext":${isCommonJs},"default":1,"named":2}`);
352-
expect(p.stdout).toMatch('"json":{"default":{"loaded":"json"},"loaded":"json"}');
353-
expect(p.stdout).toMatch('"cjs":{"default":{"named":"named"},"named":"named"}');
354-
expect(p.stdout).toMatch(`"jsx":{"cjsContext":${isCommonJs},"jsx":[null,null,["div",null,"JSX"]]}`);
355-
expect(p.stdout).toMatch('"pkgModule":{"default":1,"named":2}');
356-
if (isCommonJs) {
357-
expect(p.stdout).toMatch('"pkgCommonjs":{"default":1,"named":2}');
358-
} else {
359-
expect(p.stdout).toMatch('"pkgCommonjs":{"default":{"default":1,"named":2}}');
360-
}
361331

362-
// By "require()"ing an ESM file, it forces it to be compiled in a CJS context
363-
expect(p.stdout).toMatch(`"mjs":{"mjsHasCjsContext":${isCommonJs}}`);
364-
expect(p.stderr).toBe('');
365-
366-
const coverageDirectory = fixture.getPath('coverage');
367-
const coverageSourceMapCache = await hasCoverageSourcesContent(coverageDirectory);
368-
expect(coverageSourceMapCache).toBe(true);
332+
const p = await tsx(['import-from-ts.ts'], {
333+
cwd: fixture.path,
334+
env: {
335+
NODE_V8_COVERAGE: 'coverage',
336+
},
369337
});
370-
371-
test('tsconfig', async ({ onTestFail }) => {
372-
const pTsconfig = await tsx(['index.tsx'], fixture.getPath('tsconfig'));
373-
onTestFail((error) => {
374-
console.error(error);
375-
console.log(pTsconfig);
376-
});
377-
expect(pTsconfig.failed).toBe(false);
378-
expect(pTsconfig.stderr).toBe('');
379-
expect(pTsconfig.stdout).toBe('');
338+
onTestFail((error) => {
339+
console.error(error);
340+
console.log(p);
380341
});
342+
expect(p.failed).toBe(false);
343+
expect(p.stdout).toMatch(`"import.meta.url":"${pathToFileURL(fixture.getPath('import-from-ts.ts'))}"`);
344+
expect(p.stdout).toMatch(`"js":{"cjsContext":${isCommonJs},"default":1,"named":2}`);
345+
expect(p.stdout).toMatch('"json":{"default":{"loaded":"json"},"loaded":"json"}');
346+
expect(p.stdout).toMatch('"cjs":{"default":{"named":"named"},"named":"named"}');
347+
expect(p.stdout).toMatch(`"jsx":{"cjsContext":${isCommonJs},"jsx":[null,null,["div",null,"JSX"]]}`);
348+
expect(p.stdout).toMatch('"pkgModule":{"default":1,"named":2}');
349+
if (isCommonJs) {
350+
expect(p.stdout).toMatch('"pkgCommonjs":{"default":1,"named":2}');
351+
} else {
352+
expect(p.stdout).toMatch('"pkgCommonjs":{"default":{"default":1,"named":2}}');
353+
}
381354

382-
test('custom tsconfig', async ({ onTestFail }) => {
383-
const pTsconfigAllowJs = await tsx(['--tsconfig', 'tsconfig-allowJs.json', 'jsx.jsx'], fixture.getPath('tsconfig'));
384-
onTestFail((error) => {
385-
console.error(error);
386-
console.log(pTsconfigAllowJs);
387-
});
388-
expect(pTsconfigAllowJs.failed).toBe(true);
389-
expect(pTsconfigAllowJs.stderr).toMatch('Error: No error thrown');
390-
expect(pTsconfigAllowJs.stdout).toBe('');
391-
});
355+
// By "require()"ing an ESM file, it forces it to be compiled in a CJS context
356+
expect(p.stdout).toMatch(`"mjs":{"mjsHasCjsContext":${isCommonJs}}`);
357+
expect(p.stderr).toBe('');
392358

393-
test('allowJs in tsconfig.json', async ({ onTestFail }) => {
394-
const pTsconfigAllowJs = await tsx(['--tsconfig', 'tsconfig/tsconfig-allowJs.json', 'import-typescript-parent.js'], fixture.path);
395-
onTestFail((error) => {
396-
console.error(error);
397-
console.log(pTsconfigAllowJs);
398-
});
399-
expect(pTsconfigAllowJs.failed).toBe(false);
400-
expect(pTsconfigAllowJs.stderr).toBe('');
401-
expect(pTsconfigAllowJs.stdout).toBe('imported');
402-
});
359+
const coverageDirectory = fixture.getPath('coverage');
360+
const coverageSourceMapCache = await hasCoverageSourcesContent(coverageDirectory);
361+
expect(coverageSourceMapCache).toBe(true);
403362
});
404363
});
405364
}

0 commit comments

Comments
 (0)