Skip to content

Commit d4fff6a

Browse files
authored
fix (jest-runtime): correct wrapperLength value for ESM modules (#11893)
1 parent da7c4a4 commit d4fff6a

File tree

34 files changed

+458
-64
lines changed

34 files changed

+458
-64
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Fixes
66

7+
- `[jest-runtime]` Correct `wrapperLength` value for ESM modules. ([#11893](https://github.com/facebook/jest/pull/11893))
8+
79
### Chore & Maintenance
810

911
### Performance
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`on node >=12.16.0 prints correct coverage report, if a TS module is transpiled by custom transformer to ESM put under test 1`] = `
4+
console.log
5+
this will print
6+
7+
at covered (module.ts:13:11)
8+
9+
--------------|---------|----------|---------|---------|-------------------
10+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
11+
--------------|---------|----------|---------|---------|-------------------
12+
All files | 50 | 25 | 25 | 50 |
13+
module.ts | 80.77 | 50 | 50 | 80.77 | 16-18,21-22
14+
types.ts | 0 | 0 | 0 | 0 | 1-8
15+
uncovered.ts | 0 | 0 | 0 | 0 | 1-8
16+
--------------|---------|----------|---------|---------|-------------------
17+
`;
18+
19+
exports[`on node >=12.16.0 prints correct coverage report, if an ESM module is put under test without transformation 1`] = `
20+
console.log
21+
this will print
22+
23+
at covered (module.js:11:11)
24+
25+
--------------|---------|----------|---------|---------|-------------------
26+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
27+
--------------|---------|----------|---------|---------|-------------------
28+
All files | 59.38 | 50 | 33.33 | 59.38 |
29+
module.js | 79.17 | 66.67 | 50 | 79.17 | 14-16,19-20
30+
uncovered.js | 0 | 0 | 0 | 0 | 1-8
31+
--------------|---------|----------|---------|---------|-------------------
32+
`;
33+
34+
exports[`prints correct coverage report, if a CJS module is put under test without transformation 1`] = `
35+
console.log
36+
this will print
37+
38+
at covered (module.js:11:11)
39+
40+
--------------|---------|----------|---------|---------|-------------------
41+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
42+
--------------|---------|----------|---------|---------|-------------------
43+
All files | 59.38 | 60 | 50 | 59.38 |
44+
module.js | 79.17 | 75 | 66.67 | 79.17 | 14-16,19-20
45+
uncovered.js | 0 | 0 | 0 | 0 | 1-8
46+
--------------|---------|----------|---------|---------|-------------------
47+
`;
48+
49+
exports[`prints correct coverage report, if a TS module is transpiled by Babel to CJS and put under test 1`] = `
50+
console.log
51+
this will print
52+
53+
at log (module.ts:13:11)
54+
55+
--------------|---------|----------|---------|---------|-------------------
56+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
57+
--------------|---------|----------|---------|---------|-------------------
58+
All files | 50 | 25 | 25 | 50 |
59+
module.ts | 80.77 | 50 | 50 | 80.77 | 16-18,21-22
60+
types.ts | 0 | 0 | 0 | 0 | 1-8
61+
uncovered.ts | 0 | 0 | 0 | 0 | 1-8
62+
--------------|---------|----------|---------|---------|-------------------
63+
`;
64+
65+
exports[`prints coverage with empty sourcemaps 1`] = `
66+
----------|---------|----------|---------|---------|-------------------
67+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
68+
----------|---------|----------|---------|---------|-------------------
69+
All files | 100 | 100 | 100 | 100 |
70+
types.ts | 100 | 100 | 100 | 100 |
71+
----------|---------|----------|---------|---------|-------------------
72+
`;
73+
74+
exports[`prints coverage with missing sourcemaps 1`] = `
75+
console.log
76+
42
77+
78+
at Object.log (__tests__/Thing.test.js:10:9)
79+
80+
----------|---------|----------|---------|---------|-------------------
81+
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
82+
----------|---------|----------|---------|---------|-------------------
83+
All files | 100 | 100 | 100 | 100 |
84+
Thing.js | 100 | 100 | 100 | 100 |
85+
x.css | 100 | 100 | 100 | 100 |
86+
----------|---------|----------|---------|---------|-------------------
87+
`;

e2e/__tests__/__snapshots__/v8Coverage.test.ts.snap

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import * as path from 'path';
9+
import wrap from 'jest-snapshot-serializer-raw';
10+
import {onNodeVersions} from '@jest/test-utils';
11+
import runJest from '../runJest';
12+
13+
const DIR = path.resolve(__dirname, '../coverage-provider-v8');
14+
15+
test('prints coverage with missing sourcemaps', () => {
16+
const sourcemapDir = path.join(DIR, 'no-sourcemap');
17+
18+
const {stdout, exitCode} = runJest(
19+
sourcemapDir,
20+
['--coverage', '--coverage-provider', 'v8'],
21+
{stripAnsi: true},
22+
);
23+
24+
expect(exitCode).toBe(0);
25+
expect(wrap(stdout)).toMatchSnapshot();
26+
});
27+
28+
test('prints coverage with empty sourcemaps', () => {
29+
const sourcemapDir = path.join(DIR, 'empty-sourcemap');
30+
31+
const {stdout, exitCode} = runJest(
32+
sourcemapDir,
33+
['--coverage', '--coverage-provider', 'v8'],
34+
{stripAnsi: true},
35+
);
36+
37+
expect(exitCode).toBe(0);
38+
expect(wrap(stdout)).toMatchSnapshot();
39+
});
40+
41+
test('prints correct coverage report, if a CJS module is put under test without transformation', () => {
42+
const sourcemapDir = path.join(DIR, 'cjs-native-without-sourcemap');
43+
44+
const {stdout, exitCode} = runJest(
45+
sourcemapDir,
46+
['--coverage', '--coverage-provider', 'v8', '--no-cache'],
47+
{stripAnsi: true},
48+
);
49+
50+
expect(exitCode).toBe(0);
51+
expect(wrap(stdout)).toMatchSnapshot();
52+
});
53+
54+
test('prints correct coverage report, if a TS module is transpiled by Babel to CJS and put under test', () => {
55+
const sourcemapDir = path.join(DIR, 'cjs-with-babel-transformer');
56+
57+
const {stdout, exitCode} = runJest(
58+
sourcemapDir,
59+
['--coverage', '--coverage-provider', 'v8', '--no-cache'],
60+
{stripAnsi: true},
61+
);
62+
63+
expect(exitCode).toBe(0);
64+
expect(wrap(stdout)).toMatchSnapshot();
65+
});
66+
67+
// The versions where vm.Module exists and commonjs with "exports" is not broken
68+
onNodeVersions('>=12.16.0', () => {
69+
test('prints correct coverage report, if an ESM module is put under test without transformation', () => {
70+
const sourcemapDir = path.join(DIR, 'esm-native-without-sourcemap');
71+
72+
const {stdout, exitCode} = runJest(
73+
sourcemapDir,
74+
['--coverage', '--coverage-provider', 'v8', '--no-cache'],
75+
{
76+
nodeOptions: '--experimental-vm-modules --no-warnings',
77+
stripAnsi: true,
78+
},
79+
);
80+
81+
expect(exitCode).toBe(0);
82+
expect(wrap(stdout)).toMatchSnapshot();
83+
});
84+
85+
test('prints correct coverage report, if a TS module is transpiled by custom transformer to ESM put under test', () => {
86+
const sourcemapDir = path.join(DIR, 'esm-with-custom-transformer');
87+
88+
const {stdout, exitCode} = runJest(
89+
sourcemapDir,
90+
['--coverage', '--coverage-provider', 'v8', '--no-cache'],
91+
{
92+
nodeOptions: '--experimental-vm-modules --no-warnings',
93+
stripAnsi: true,
94+
},
95+
);
96+
97+
expect(exitCode).toBe(0);
98+
expect(wrap(stdout)).toMatchSnapshot();
99+
});
100+
});

e2e/__tests__/v8Coverage.test.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
const {value} = require('../module');
9+
10+
test('dummy', () => {
11+
expect(value).toBe('abc');
12+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
const value = 'abc';
9+
10+
function covered() {
11+
console.log('this will print');
12+
}
13+
14+
function uncovered() {
15+
console.log('this will not');
16+
}
17+
18+
if (value !== 'abc') {
19+
uncovered();
20+
}
21+
22+
covered();
23+
24+
module.exports = {value};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"jest": {
3+
"collectCoverageFrom": [
4+
"<rootDir>/*.js"
5+
],
6+
"transform": {
7+
}
8+
}
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
module.exports = {};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import {value} from '../module';
9+
10+
test('dummy', () => {
11+
expect(value).toBe('abc');
12+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import type {A} from './types';
9+
10+
const value: A = 'abc';
11+
12+
function covered() {
13+
console.log('this will print');
14+
}
15+
16+
function uncovered() {
17+
console.log('this will not');
18+
}
19+
20+
if (value !== 'abc') {
21+
uncovered();
22+
}
23+
24+
covered();
25+
26+
export {value};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"babel": {
3+
"presets": [
4+
["@babel/preset-env", {"targets": {"node": "current"}}],
5+
"@babel/preset-typescript"
6+
]
7+
},
8+
"jest": {
9+
"collectCoverageFrom": [
10+
"<rootDir>/*.ts"
11+
]
12+
}
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export type A = string;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export {};

0 commit comments

Comments
 (0)