Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix (jest-runtime): correct wrapperLength value for ESM modules #11893

Merged
merged 7 commits into from Sep 25, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,8 @@

### Fixes

- `[jest-runtime]` Correct `wrapperLength` value for ESM modules. ([#11893](https://github.com/facebook/jest/pull/11893))

### Chore & Maintenance

### Performance
Expand Down
72 changes: 72 additions & 0 deletions e2e/__tests__/__snapshots__/coverageProviderV8.test.ts-.snap
@@ -0,0 +1,72 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
mrazauskas marked this conversation as resolved.
Show resolved Hide resolved

exports[`prints correct coverage report, if a CJS module is put under test without transformation 1`] = `
console.log
this will print

at covered (module.js:11:11)

--------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------|---------|----------|---------|---------|-------------------
All files | 59.38 | 60 | 50 | 59.38 |
module.js | 79.17 | 75 | 66.67 | 79.17 | 14-16,19-20
uncovered.js | 0 | 0 | 0 | 0 | 1-8
--------------|---------|----------|---------|---------|-------------------
`;

exports[`prints correct coverage report, if a TS module is transpiled by Babel to CJS and put under test 1`] = `
console.log
this will print

at log (module.ts:13:11)

--------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------|---------|----------|---------|---------|-------------------
All files | 50 | 25 | 25 | 50 |
module.ts | 80.77 | 50 | 50 | 80.77 | 16-18,21-22
types.ts | 0 | 0 | 0 | 0 | 1-8
uncovered.ts | 0 | 0 | 0 | 0 | 1-8
--------------|---------|----------|---------|---------|-------------------
`;

exports[`prints correct coverage report, if a TS module is transpiled by custom transformer to ESM put under test 1`] = `
console.log
this will print

at covered (module.ts:13:11)

--------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------|---------|----------|---------|---------|-------------------
All files | 50 | 25 | 25 | 50 |
module.ts | 80.77 | 50 | 50 | 80.77 | 16-18,21-22
types.ts | 0 | 0 | 0 | 0 | 1-8
uncovered.ts | 0 | 0 | 0 | 0 | 1-8
--------------|---------|----------|---------|---------|-------------------
`;

exports[`prints correct coverage report, if an ESM module is put under test without transformation 1`] = `
console.log
this will print

at covered (module.js:11:11)

--------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------|---------|----------|---------|---------|-------------------
All files | 59.38 | 50 | 33.33 | 59.38 |
module.js | 79.17 | 66.67 | 50 | 79.17 | 14-16,19-20
uncovered.js | 0 | 0 | 0 | 0 | 1-8
--------------|---------|----------|---------|---------|-------------------
`;

exports[`prints coverage with empty sourcemaps 1`] = `
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
types.ts | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
`;
87 changes: 87 additions & 0 deletions e2e/__tests__/__snapshots__/coverageProviderV8.test.ts.snap
@@ -0,0 +1,87 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`prints correct coverage report, if a CJS module is put under test without transformation 1`] = `
console.log
this will print

at covered (module.js:11:11)

--------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------|---------|----------|---------|---------|-------------------
All files | 59.38 | 60 | 50 | 59.38 |
module.js | 79.17 | 75 | 66.67 | 79.17 | 14-16,19-20
uncovered.js | 0 | 0 | 0 | 0 | 1-8
--------------|---------|----------|---------|---------|-------------------
`;

exports[`prints correct coverage report, if a TS module is transpiled by Babel to CJS and put under test 1`] = `
console.log
this will print

at log (module.ts:13:11)

--------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------|---------|----------|---------|---------|-------------------
All files | 50 | 25 | 25 | 50 |
module.ts | 80.77 | 50 | 50 | 80.77 | 16-18,21-22
types.ts | 0 | 0 | 0 | 0 | 1-8
uncovered.ts | 0 | 0 | 0 | 0 | 1-8
--------------|---------|----------|---------|---------|-------------------
`;

exports[`prints correct coverage report, if a TS module is transpiled by custom transformer to ESM put under test 1`] = `
console.log
this will print

at covered (module.ts:13:11)

--------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------|---------|----------|---------|---------|-------------------
All files | 50 | 25 | 25 | 50 |
module.ts | 80.77 | 50 | 50 | 80.77 | 16-18,21-22
types.ts | 0 | 0 | 0 | 0 | 1-8
uncovered.ts | 0 | 0 | 0 | 0 | 1-8
--------------|---------|----------|---------|---------|-------------------
`;

exports[`prints correct coverage report, if an ESM module is put under test without transformation 1`] = `
console.log
this will print

at covered (module.js:11:11)

--------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------|---------|----------|---------|---------|-------------------
All files | 59.38 | 50 | 33.33 | 59.38 |
module.js | 79.17 | 66.67 | 50 | 79.17 | 14-16,19-20
uncovered.js | 0 | 0 | 0 | 0 | 1-8
--------------|---------|----------|---------|---------|-------------------
`;

exports[`prints coverage with empty sourcemaps 1`] = `
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
types.ts | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
`;

exports[`prints coverage with missing sourcemaps 1`] = `
console.log
42

at Object.log (__tests__/Thing.test.js:10:9)

----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
Thing.js | 100 | 100 | 100 | 100 |
x.css | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
`;
25 changes: 0 additions & 25 deletions e2e/__tests__/__snapshots__/v8Coverage.test.ts.snap

This file was deleted.

96 changes: 96 additions & 0 deletions e2e/__tests__/coverageProviderV8.test.ts
@@ -0,0 +1,96 @@
/**
* 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 * as path from 'path';
import wrap from 'jest-snapshot-serializer-raw';
import runJest from '../runJest';

const DIR = path.resolve(__dirname, '../coverage-provider-v8');

test('prints coverage with missing sourcemaps', () => {
const sourcemapDir = path.join(DIR, 'no-sourcemap');

const {stdout, exitCode} = runJest(
sourcemapDir,
['--coverage', '--coverage-provider', 'v8'],
{stripAnsi: true},
);

expect(exitCode).toBe(0);
expect(wrap(stdout)).toMatchSnapshot();
});

test('prints coverage with empty sourcemaps', () => {
const sourcemapDir = path.join(DIR, 'empty-sourcemap');

const {stdout, exitCode} = runJest(
sourcemapDir,
['--coverage', '--coverage-provider', 'v8'],
{stripAnsi: true},
);

expect(exitCode).toBe(0);
expect(wrap(stdout)).toMatchSnapshot();
});

test('prints correct coverage report, if a CJS module is put under test without transformation', () => {
const sourcemapDir = path.join(DIR, 'cjs-native-without-sourcemap');

const {stdout, exitCode} = runJest(
sourcemapDir,
['--coverage', '--coverage-provider', 'v8', '--no-cache'],
{stripAnsi: true},
);

expect(exitCode).toBe(0);
expect(wrap(stdout)).toMatchSnapshot();
});

test('prints correct coverage report, if a TS module is transpiled by Babel to CJS and put under test', () => {
const sourcemapDir = path.join(DIR, 'cjs-with-babel-transformer');

const {stdout, exitCode} = runJest(
sourcemapDir,
['--coverage', '--coverage-provider', 'v8', '--no-cache'],
{stripAnsi: true},
);

expect(exitCode).toBe(0);
expect(wrap(stdout)).toMatchSnapshot();
});

test('prints correct coverage report, if an ESM module is put under test without transformation', () => {
const sourcemapDir = path.join(DIR, 'esm-native-without-sourcemap');

const {stdout, exitCode} = runJest(
sourcemapDir,
['--coverage', '--coverage-provider', 'v8', '--no-cache'],
{
nodeOptions: '--experimental-vm-modules --no-warnings',
stripAnsi: true,
},
);

expect(exitCode).toBe(0);
expect(wrap(stdout)).toMatchSnapshot();
});

test('prints correct coverage report, if a TS module is transpiled by custom transformer to ESM put under test', () => {
const sourcemapDir = path.join(DIR, 'esm-with-custom-transformer');

const {stdout, exitCode} = runJest(
sourcemapDir,
['--coverage', '--coverage-provider', 'v8', '--no-cache'],
{
nodeOptions: '--experimental-vm-modules --no-warnings',
stripAnsi: true,
},
);

expect(exitCode).toBe(0);
expect(wrap(stdout)).toMatchSnapshot();
});
38 changes: 0 additions & 38 deletions e2e/__tests__/v8Coverage.test.ts

This file was deleted.

@@ -0,0 +1,12 @@
/**
* 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.
*/

const {value} = require('../module');

test('dummy', () => {
expect(value).toBe('abc');
});
24 changes: 24 additions & 0 deletions e2e/coverage-provider-v8/cjs-native-without-sourcemap/module.js
@@ -0,0 +1,24 @@
/**
* 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.
*/

const value = 'abc';

function covered() {
console.log('this will print');
}

function uncovered() {
console.log('this will not');
}

if (value !== 'abc') {
uncovered();
}

covered();

module.exports = {value};
@@ -0,0 +1,9 @@
{
"jest": {
"collectCoverageFrom": [
"<rootDir>/*.js"
],
"transform": {
}
}
}
@@ -0,0 +1,8 @@
/**
* 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.
*/

module.exports = {};