forked from jestjs/jest
/
transform.test.ts
240 lines (202 loc) · 7.42 KB
/
transform.test.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
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
231
232
233
234
235
236
237
238
239
240
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {tmpdir} from 'os';
import * as path from 'path';
import {wrap} from 'jest-snapshot-serializer-raw';
import {
cleanup,
copyDir,
createEmptyPackage,
extractSummary,
linkJestPackage,
runYarnInstall,
} from '../Utils';
import runJest, {json as runWithJson} from '../runJest';
describe('babel-jest', () => {
const dir = path.resolve(__dirname, '..', 'transform/babel-jest');
beforeEach(() => {
runYarnInstall(dir);
});
it('runs transpiled code', () => {
// --no-cache because babel can cache stuff and result in false green
const {json} = runWithJson(dir, ['--no-cache']);
expect(json.success).toBe(true);
expect(json.numTotalTests).toBeGreaterThanOrEqual(2);
});
it('instruments only specific files and collects coverage', () => {
const {stdout} = runJest(dir, ['--coverage', '--no-cache'], {
stripAnsi: true,
});
expect(stdout).toMatch('covered.js');
expect(stdout).not.toMatch('notCovered.js');
expect(stdout).not.toMatch('excludedFromCoverage.js');
// coverage result should not change
expect(wrap(stdout)).toMatchSnapshot();
});
});
describe('babel-jest ignored', () => {
const dir = path.resolve(__dirname, '..', 'transform/babel-jest-ignored');
it('tells user to match ignored files', () => {
// --no-cache because babel can cache stuff and result in false green
const {exitCode, stderr} = runJest(dir, ['--no-cache']);
expect(exitCode).toBe(1);
expect(wrap(extractSummary(stderr).rest)).toMatchSnapshot();
});
});
describe('babel-jest with manual transformer', () => {
const dir = path.resolve(__dirname, '..', 'transform/babel-jest-manual');
beforeEach(() => {
runYarnInstall(dir);
});
it('runs transpiled code', () => {
// --no-cache because babel can cache stuff and result in false green
const {json} = runWithJson(dir, ['--no-cache']);
expect(json.success).toBe(true);
expect(json.numTotalTests).toBeGreaterThanOrEqual(1);
});
});
// babel-jest is automatically linked at the root because it is a workspace now
// a way to test this in isolation is to move the test suite into a temp folder
describe('no babel-jest', () => {
const dir = path.resolve(__dirname, '..', 'transform/no-babel-jest');
// doing test in a temp directory because we don't want jest node_modules affect it
const tempDir = path.resolve(tmpdir(), 'transform-no-babel-jest');
beforeEach(() => {
cleanup(tempDir);
createEmptyPackage(tempDir);
copyDir(dir, tempDir);
linkJestPackage('babel-jest', tempDir);
});
test('fails with syntax error on flow types', () => {
const {stderr} = runJest(tempDir, ['--no-cache', '--no-watchman']);
expect(stderr).toMatch(/FAIL.*failsWithSyntaxError/);
expect(stderr).toMatch('Unexpected token');
});
test('instrumentation with no babel-jest', () => {
const {stdout} = runJest(
tempDir,
['--no-cache', '--coverage', '--no-watchman'],
{stripAnsi: true},
);
expect(stdout).toMatch('covered.js');
expect(stdout).not.toMatch('excludedFromCoverage.js');
// coverage result should not change
expect(wrap(stdout)).toMatchSnapshot();
});
});
describe('custom transformer', () => {
const dir = path.resolve(
__dirname,
'..',
'transform/custom-instrumenting-preprocessor',
);
it('proprocesses files', () => {
const {json, stderr} = runWithJson(dir, ['--no-cache']);
expect(stderr).toMatch(/FAIL/);
expect(stderr).toMatch(/instruments by setting.*global\.__INSTRUMENTED__/);
expect(json.numTotalTests).toBe(2);
expect(json.numPassedTests).toBe(1);
expect(json.numFailedTests).toBe(1);
});
it('instruments files', () => {
const {stdout, exitCode} = runJest(dir, ['--no-cache', '--coverage'], {
stripAnsi: true,
});
// coverage should be empty because there's no real instrumentation
expect(wrap(stdout)).toMatchSnapshot();
expect(exitCode).toBe(0);
});
});
describe('multiple-transformers', () => {
const dir = path.resolve(__dirname, '..', 'transform/multiple-transformers');
beforeEach(() => {
runYarnInstall(dir);
});
it('transforms dependencies using specific transformers', () => {
const {json, stderr} = runWithJson(dir, ['--no-cache']);
expect(stderr).toMatch(/PASS/);
expect(json.numTotalTests).toBe(1);
expect(json.numPassedTests).toBe(1);
});
});
describe('ecmascript-modules-support', () => {
const dir = path.resolve(
__dirname,
'..',
'transform/ecmascript-modules-support',
);
it('runs transpiled code', () => {
// --no-cache because babel can cache stuff and result in false green
const {json} = runWithJson(dir, ['--no-cache']);
expect(json.success).toBe(true);
expect(json.numTotalTests).toBeGreaterThanOrEqual(1);
});
});
describe('transformer-config', () => {
const dir = path.resolve(__dirname, '..', 'transform/transformer-config');
beforeEach(() => {
runYarnInstall(dir);
});
it('runs transpiled code', () => {
// --no-cache because babel can cache stuff and result in false green
const {json} = runWithJson(dir, ['--no-cache']);
expect(json.success).toBe(true);
expect(json.numTotalTests).toBeGreaterThanOrEqual(1);
});
it('instruments only specific files and collects coverage', () => {
const {stdout} = runJest(dir, ['--coverage', '--no-cache'], {
stripAnsi: true,
});
expect(stdout).toMatch('Covered.js');
expect(stdout).not.toMatch('NotCovered.js');
expect(stdout).not.toMatch('ExcludedFromCoverage.js');
// coverage result should not change
expect(wrap(stdout)).toMatchSnapshot();
});
});
describe('transformer caching', () => {
const dir = path.resolve(__dirname, '../transform/cache');
const transformedFile = path.resolve(dir, './common-file.js');
it('does not rerun transform within worker', () => {
// --no-cache because babel can cache stuff and result in false green
const {stdout} = runJest(dir, ['--no-cache', '-w=2']);
const loggedFiles = stdout.split('\n');
// Verify any lines logged are _just_ the file we care about
loggedFiles.forEach(line => {
expect(line).toBe(transformedFile);
});
// We run with 2 workers, so the file should be transformed twice
expect(loggedFiles).toHaveLength(2);
});
});
describe('transform-environment', () => {
const dir = path.resolve(__dirname, '../transform/transform-environment');
it('should transform the environment', () => {
const {json, stderr} = runWithJson(dir, ['--no-cache']);
expect(stderr).toMatch(/PASS/);
expect(json.success).toBe(true);
expect(json.numPassedTests).toBe(1);
});
});
describe('transform-runner', () => {
const dir = path.resolve(__dirname, '../transform/transform-runner');
it('should transform runner', () => {
const {json, stderr} = runWithJson(dir, ['--no-cache']);
expect(stderr).toMatch(/PASS/);
expect(json.success).toBe(true);
expect(json.numPassedTests).toBe(1);
});
});
describe('transform-testrunner', () => {
const dir = path.resolve(__dirname, '../transform/transform-testrunner');
it('should transform testRunner', () => {
const {json, stderr} = runWithJson(dir, ['--no-cache']);
expect(stderr).toMatch(/PASS/);
expect(json.success).toBe(true);
expect(json.numPassedTests).toBe(1);
});
});