-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Expand file tree
/
Copy pathjest.config.ts.test.ts
More file actions
230 lines (204 loc) · 7.6 KB
/
jest.config.ts.test.ts
File metadata and controls
230 lines (204 loc) · 7.6 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import * as path from 'path';
import * as fs from 'graceful-fs';
import {onNodeVersions} from '@jest/test-utils';
import {cleanup, extractSummary, writeFiles} from '../Utils';
import runJest from '../runJest';
const DIR = path.resolve(__dirname, '../jest-config-ts');
beforeEach(() => cleanup(DIR));
afterAll(() => cleanup(DIR));
test('works with jest.config.ts', () => {
writeFiles(DIR, {
'__tests__/a-giraffe.js': "test('giraffe', () => expect(1).toBe(1));",
'jest.config.ts':
"export default {testEnvironment: 'jest-environment-node', testRegex: '.*-giraffe.js'};",
'package.json': '{}',
});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false'], {
nodeOptions: '--no-warnings',
});
const {rest, summary} = extractSummary(stderr);
expect(exitCode).toBe(0);
expect(rest).toMatchSnapshot();
expect(summary).toMatchSnapshot();
});
test('falls back to a loader if we encounter a ESM TS config file in a CommonJs project', () => {
writeFiles(DIR, {
'__tests__/a-giraffe.js': "test('giraffe', () => expect(1).toBe(1));",
'jest.config.ts':
"export default {testEnvironment: 'jest-environment-node', testRegex: '.*-giraffe.js'};",
'package.json': '{"type":"commonjs"}',
});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false'], {
nodeOptions: '--no-warnings',
});
const {rest, summary} = extractSummary(stderr);
expect(exitCode).toBe(0);
expect(rest).toMatchSnapshot();
expect(summary).toMatchSnapshot();
});
test('works with tsconfig.json', () => {
writeFiles(DIR, {
'__tests__/a-giraffe.js': "test('giraffe', () => expect(1).toBe(1));",
'jest.config.ts':
"export default {testEnvironment: 'jest-environment-node', testRegex: '.*-giraffe.js'};",
'package.json': '{}',
'tsconfig.json': '{ "compilerOptions": { "module": "esnext" } }',
});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false'], {
nodeOptions: '--no-warnings',
});
const {rest, summary} = extractSummary(stderr);
expect(exitCode).toBe(0);
expect(rest).toMatchSnapshot();
expect(summary).toMatchSnapshot();
});
test('traverses directory tree up until it finds jest.config', () => {
writeFiles(DIR, {
'__tests__/a-giraffe.js': `
const slash = p => p.replaceAll('\\\\', '/');
test('giraffe', () => expect(1).toBe(1));
test('abc', () => console.log(slash(process.cwd())));
`,
'jest.config.ts':
"export default {testEnvironment: 'jest-environment-node', testRegex: '.*-giraffe.js'};",
'package.json': '{}',
'some/nested/directory/file.js': '// nothing special',
});
const {stderr, exitCode, stdout} = runJest(
path.join(DIR, 'some', 'nested', 'directory'),
['-w=1', '--ci=false'],
{nodeOptions: '--no-warnings', skipPkgJsonCheck: true},
);
// Snapshot the console.logged `process.cwd()` and make sure it stays the same
expect(
stdout
.replaceAll(/^\W+(.*)e2e/gm, '<<REPLACED>>')
// slightly different log in node versions >= 23
.replace('at Object.log', 'at Object.<anonymous>'),
).toMatchSnapshot();
const {rest, summary} = extractSummary(stderr);
expect(exitCode).toBe(0);
expect(rest).toMatchSnapshot();
expect(summary).toMatchSnapshot();
});
onNodeVersions('<23.6', () => {
const jestPath = require.resolve('jest');
const jestTypesPath = jestPath.replace(/\.js$/, '.d.ts');
const jestTypesExists = fs.existsSync(jestTypesPath);
(jestTypesExists ? test : test.skip).each([true, false])(
'check the config disabled (skip type check: %p)',
skipTypeCheck => {
writeFiles(DIR, {
'__tests__/a-giraffe.js': "test('giraffe', () => expect(1).toBe(1));",
'jest.config.ts': `
/**@jest-config-loader-options {"transpileOnly":${skipTypeCheck}}*/
import {Config} from 'jest';
const config: Config = { testTimeout: "10000" };
export default config;
`,
'package.json': '{}',
'tsconfig.json': '{}',
});
const typeErrorString =
"TS2322: Type 'string' is not assignable to type 'number'.";
const runtimeErrorString = 'Option "testTimeout" must be of type:';
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false']);
if (skipTypeCheck) {
expect(stderr).not.toMatch(typeErrorString);
expect(stderr).toMatch(runtimeErrorString);
} else {
expect(stderr).toMatch(typeErrorString);
expect(stderr).not.toMatch(runtimeErrorString);
}
expect(exitCode).toBe(1);
},
);
test('invalid JS in jest.config.ts', () => {
writeFiles(DIR, {
'__tests__/a-giraffe.js': "test('giraffe', () => expect(1).toBe(1));",
'jest.config.ts': "export default i'll break this file yo",
'package.json': '{}',
});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false']);
expect(stderr).toMatch('TSError: ⨯ Unable to compile TypeScript:');
expect(exitCode).toBe(1);
});
});
onNodeVersions('^23.6', () => {
test('invalid JS in jest.config.ts (node with native TS support)', () => {
writeFiles(DIR, {
'__tests__/a-giraffe.js': "test('giraffe', () => expect(1).toBe(1));",
'jest.config.ts': "export default i'll break this file yo",
'package.json': '{}',
});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false'], {
nodeOptions: '--no-warnings',
});
expect(
stderr
// Remove the stack trace from the error message
.slice(0, Math.max(0, stderr.indexOf('at readConfigFileAndSetRootDir')))
.trim()
// Replace the path to the config file with a placeholder
.replace(
/(Error: Jest: Failed to parse the TypeScript config file).*$/m,
'$1 <<REPLACED>>',
),
).toMatchSnapshot();
expect(exitCode).toBe(1);
});
test('load typed jest.config.ts with TS loader specified in docblock pragma', () => {
writeFiles(DIR, {
'__tests__/a-giraffe.js': "test('giraffe', () => expect(1).toBe(1));",
'foo.ts': 'export const a = () => {};',
'jest.config.ts': `
/** @jest-config-loader ts-node */
import { a } from './foo'
a();
import type {Config} from 'jest';
const config: Config = { testTimeout: 10000 };
export default config;
`,
'package.json': '{}',
});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false'], {
nodeOptions: '--no-warnings',
});
const {rest, summary} = extractSummary(stderr);
expect(exitCode).toBe(0);
expect(rest).toMatchSnapshot();
expect(summary).toMatchSnapshot();
});
});
onNodeVersions('>=24', () => {
// todo fixme
// eslint-disable-next-line jest/no-identical-title
test('invalid JS in jest.config.ts (node with native TS support)', () => {
writeFiles(DIR, {
'__tests__/a-giraffe.js': "test('giraffe', () => expect(1).toBe(1));",
'jest.config.ts': "export default i'll break this file yo",
'package.json': '{}',
});
const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false'], {
nodeOptions: '--no-warnings',
});
expect(
stderr
// Remove the stack trace from the error message
.slice(0, Math.max(0, stderr.indexOf('at readConfigFileAndSetRootDir')))
.trim()
// Replace the path to the config file with a placeholder
.replace(
/(Error: Jest: Failed to parse the TypeScript config file).*$/m,
'$1 <<REPLACED>>',
),
).toMatchSnapshot();
expect(exitCode).toBe(1);
});
});