diff --git a/CHANGELOG.md b/CHANGELOG.md index 238cc09df2e2..274444e465a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ ## master +### Fixes + +* `[jest-runtime]` fix error for test files providing coverage. + ([#5117](https://github.com/facebook/jest/pull/5117)) + +### Features + * `[jest-config]` Add `forceCoverageMatch` to allow collecting coverage from ignored files. ([#5081](https://github.com/facebook/jest/pull/5081)) diff --git a/docs/Configuration.md b/docs/Configuration.md index 42eb4e0195df..ad7c3d76d79f 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -242,7 +242,8 @@ Jest will fail if: Default: `['']` Test files are normally ignored from collecting code coverage. With this option, -you can overwrite this behavior and include otherwise ignored files in code coverage. +you can overwrite this behavior and include otherwise ignored files in code +coverage. For example, if you have tests in source files named with `.t.js` extension as following: @@ -250,7 +251,7 @@ following: ```javascript // sum.t.js -export function sum(a,b) { +export function sum(a, b) { return a + b; } @@ -262,6 +263,7 @@ if (process.env.NODE_ENV === 'test') { ``` You can collect coverage from those files with setting `forceCoverageMatch`. + ```json { ... @@ -271,7 +273,6 @@ You can collect coverage from those files with setting `forceCoverageMatch`. } ``` - ### `globals` [object] Default: `{}` diff --git a/docs/JestPlatform.md b/docs/JestPlatform.md index 2b4fcda3fa52..52c08e67ca8d 100644 --- a/docs/JestPlatform.md +++ b/docs/JestPlatform.md @@ -3,36 +3,42 @@ id: jest-platform title: Jest Platform --- -You can cherry pick specific features of Jest and use them as standalone packages. Here's a list of the available packages: +You can cherry pick specific features of Jest and use them as standalone +packages. Here's a list of the available packages: ## jest-changed-files -Tool for identifying modified files in a git/hg repository. Exports two functions: +Tool for identifying modified files in a git/hg repository. Exports two +functions: -- `getChangedFilesForRoots` returns a promise that resolves to an object with the changed files and repos. -- `findRepos` returns a promise that resolves to a set of repositories contained in the specified path. +* `getChangedFilesForRoots` returns a promise that resolves to an object with + the changed files and repos. +* `findRepos` returns a promise that resolves to a set of repositories contained + in the specified path. ### Example -``` javascript -const {getChangedFilesForRoots} = require( 'jest-changed-files'); +```javascript +const {getChangedFilesForRoots} = require('jest-changed-files'); // print the set of modified files since last commit in the current repo getChangedFilesForRoots(['./'], { lastCommit: true, }).then(result => console.log(result.changedFiles)); - ``` -You can read more about `jest-changed-files` in the [readme file](https://github.com/facebook/jest/blob/master/packages/jest-changed-files/README.md). +You can read more about `jest-changed-files` in the +[readme file](https://github.com/facebook/jest/blob/master/packages/jest-changed-files/README.md). ## jest-diff -Tool for visualizing changes in data. Exports a function that compares two values of any type and returns a "pretty-printed" string illustrating the difference between the two arguments. +Tool for visualizing changes in data. Exports a function that compares two +values of any type and returns a "pretty-printed" string illustrating the +difference between the two arguments. ### Example -``` javascript +```javascript const diff = require('jest-diff'); const a = {a: {b: {c: 5}}}; @@ -46,12 +52,13 @@ console.log(result); ## jest-docblock -Tool for extracting and parsing the comments at the top of a JavaScript file. Exports various function to manipulate the data inside the comment block. +Tool for extracting and parsing the comments at the top of a JavaScript file. +Exports various function to manipulate the data inside the comment block. ### Example -``` javascript -const { parseWithComments } = require('jest-docblock'); +```javascript +const {parseWithComments} = require('jest-docblock'); const code = ` /** @@ -66,17 +73,20 @@ const code = ` const parsed = parseWithComments(code); // prints an object with two attributes: comments and pragmas. -console.log(parsed) +console.log(parsed); ``` -You can read more about `jest-docblock` in the [readme file](https://github.com/facebook/jest/blob/master/packages/jest-docblock/README.md). + +You can read more about `jest-docblock` in the +[readme file](https://github.com/facebook/jest/blob/master/packages/jest-docblock/README.md). ## jest-get-type -Module that identifies the primitive type of any JavaScript value. Exports a function that returns a string with the type of the value passed as argument. +Module that identifies the primitive type of any JavaScript value. Exports a +function that returns a string with the type of the value passed as argument. ### Example -``` javascript +```javascript const getType = require('jest-get-type'); const array = [1, 2, 3]; @@ -93,47 +103,55 @@ console.log(getType(undefinedValue)); ## jest-validate -Tool for validating configurations submitted by users. Exports a function that takes two arguments: the user's configuration and an object containing an example configuration and other options. The return value is an object with two attributes: +Tool for validating configurations submitted by users. Exports a function that +takes two arguments: the user's configuration and an object containing an +example configuration and other options. The return value is an object with two +attributes: -- `hasDeprecationWarnings`, a boolean indicating whether the submitted configuration has deprecation warnings, -- `isValid`, a boolean indicating whether the configuration is correct or not. +* `hasDeprecationWarnings`, a boolean indicating whether the submitted + configuration has deprecation warnings, +* `isValid`, a boolean indicating whether the configuration is correct or not. -### Example +### Example -``` javascript -const { validate } = require('jest-validate'); +```javascript +const {validate} = require('jest-validate'); const configByUser = { - transform: '/node_modules/my-custom-transform' -} + transform: '/node_modules/my-custom-transform', +}; const result = validate(configByUser, { comment: ' Documentation: http://custom-docs.com', - exampleConfig: { transform: '/node_modules/babel-jest' }, + exampleConfig: {transform: '/node_modules/babel-jest'}, }); console.log(result); ``` -You can read more about `jest-validate` in the [readme file](https://github.com/facebook/jest/blob/master/packages/jest-validate/README.md). +You can read more about `jest-validate` in the +[readme file](https://github.com/facebook/jest/blob/master/packages/jest-validate/README.md). ## jest-worker -Module used for parallelization of tasks. Exports a class `Worker` that takes the path of Node.js module and lets you call the module's exported methods as if they where class methods, returning a promise that resolves when the specified method finishes its execution in a forked process. +Module used for parallelization of tasks. Exports a class `Worker` that takes +the path of Node.js module and lets you call the module's exported methods as if +they where class methods, returning a promise that resolves when the specified +method finishes its execution in a forked process. -### Example +### Example -``` javascript +```javascript // heavy-task.js module.exports = { - myHeavyTask: (args) => { + myHeavyTask: args => { // long running CPU intensive task. - } -} + }, +}; ``` -``` javascript +```javascript // main.js async function main() { @@ -141,9 +159,9 @@ async function main() { // run 2 tasks in parellel with different arguments const results = await Promise.all([ - worker.myHeavyTask({ foo: 'bar' }), - worker.myHeavyTask({bar: 'foo' }), - ]); + worker.myHeavyTask({foo: 'bar'}), + worker.myHeavyTask({bar: 'foo'}), + ]); console.log(results); } @@ -151,16 +169,19 @@ async function main() { main(); ``` -You can read more about `jest-worker` in the [readme file](https://github.com/facebook/jest/blob/master/packages/jest-worker/README.md). +You can read more about `jest-worker` in the +[readme file](https://github.com/facebook/jest/blob/master/packages/jest-worker/README.md). ## pretty-format -Exports a function that converts any JavaScript value into a human-readable string. Supports all built-in JavaScript types out of the box and allows extension for application-specific types via user-defined plugins. +Exports a function that converts any JavaScript value into a human-readable +string. Supports all built-in JavaScript types out of the box and allows +extension for application-specific types via user-defined plugins. ### Example -``` javascript -const prettyFormat = require('pretty-format'); +```javascript +const prettyFormat = require('pretty-format'); const val = {object: {}}; val.circularReference = val; @@ -171,4 +192,5 @@ val.array = [-0, Infinity, NaN]; console.log(prettyFormat(val)); ``` -You can read more about `pretty-format` in the [readme file](https://github.com/facebook/jest/blob/master/packages/pretty-format/README.md). +You can read more about `pretty-format` in the +[readme file](https://github.com/facebook/jest/blob/master/packages/pretty-format/README.md). diff --git a/integration_tests/__tests__/__snapshots__/coverage_remapping.test.js.snap b/integration_tests/__tests__/__snapshots__/coverage_remapping.test.js.snap index 70b45b87638c..00cebd87ecb8 100644 --- a/integration_tests/__tests__/__snapshots__/coverage_remapping.test.js.snap +++ b/integration_tests/__tests__/__snapshots__/coverage_remapping.test.js.snap @@ -3,7 +3,6 @@ exports[`maps code coverage against original source 1`] = ` Object { "covered.ts": Object { - "_coverageSchema": "332fd63041d2c1bcb487cc26dd0d5f7d97098a6c", "b": Object { "0": Array [ 1, @@ -25,35 +24,34 @@ Object { }, "branchMap": Object { "0": Object { - "line": 4, "loc": Object { "end": Object { - "column": 11, - "line": 6, + "column": 9, + "line": 5, }, "start": Object { - "column": 18, - "line": 4, + "column": 8, + "line": 5, }, }, "locations": Array [ Object { "end": Object { - "column": 11, + "column": 9, "line": 5, }, "start": Object { - "column": 10, + "column": 8, "line": 5, }, }, Object { "end": Object { - "column": 11, + "column": 9, "line": 6, }, "start": Object { - "column": 10, + "column": 8, "line": 6, }, }, @@ -61,35 +59,34 @@ Object { "type": "cond-expr", }, "1": Object { - "line": 7, "loc": Object { "end": Object { - "column": 30, + "column": 37, "line": 7, }, "start": Object { - "column": 18, + "column": 36, "line": 7, }, }, "locations": Array [ Object { "end": Object { - "column": 26, + "column": 37, "line": 7, }, "start": Object { - "column": 25, + "column": 36, "line": 7, }, }, Object { "end": Object { - "column": 30, + "column": 41, "line": 7, }, "start": Object { - "column": 29, + "column": 40, "line": 7, }, }, @@ -97,45 +94,44 @@ Object { "type": "cond-expr", }, "2": Object { - "line": 8, "loc": Object { "end": Object { - "column": 39, + "column": 33, "line": 8, }, "start": Object { - "column": 18, + "column": 29, "line": 8, }, }, "locations": Array [ Object { "end": Object { - "column": 22, + "column": 33, "line": 8, }, "start": Object { - "column": 18, + "column": 29, "line": 8, }, }, Object { "end": Object { - "column": 30, + "column": 41, "line": 8, }, "start": Object { - "column": 26, + "column": 37, "line": 8, }, }, Object { "end": Object { - "column": 39, + "column": 50, "line": 8, }, "start": Object { - "column": 34, + "column": 45, "line": 8, }, }, @@ -143,35 +139,34 @@ Object { "type": "binary-expr", }, "3": Object { - "line": 9, "loc": Object { "end": Object { - "column": 79, + "column": 42, "line": 9, }, "start": Object { - "column": 13, + "column": 32, "line": 9, }, }, "locations": Array [ Object { "end": Object { - "column": 48, + "column": 42, "line": 9, }, "start": Object { - "column": 20, + "column": 32, "line": 9, }, }, Object { "end": Object { - "column": 79, + "column": 55, "line": 9, }, "start": Object { - "column": 51, + "column": 45, "line": 9, }, }, @@ -188,22 +183,21 @@ Object { "0": Object { "decl": Object { "end": Object { - "column": 36, + "column": 28, "line": 3, }, "start": Object { - "column": 26, + "column": 9, "line": 3, }, }, - "line": 3, "loc": Object { "end": Object { "column": 1, - "line": 11, + "line": 12, }, "start": Object { - "column": 43, + "column": 49, "line": 3, }, }, @@ -212,18 +206,17 @@ Object { "1": Object { "decl": Object { "end": Object { - "column": 21, + "column": 37, "line": 9, }, "start": Object { - "column": 20, + "column": 32, "line": 9, }, }, - "line": 9, "loc": Object { "end": Object { - "column": 48, + "column": 42, "line": 9, }, "start": Object { @@ -236,22 +229,21 @@ Object { "2": Object { "decl": Object { "end": Object { - "column": 52, + "column": 50, "line": 9, }, "start": Object { - "column": 51, + "column": 45, "line": 9, }, }, - "line": 9, "loc": Object { "end": Object { - "column": 79, + "column": 55, "line": 9, }, "start": Object { - "column": 63, + "column": 45, "line": 9, }, }, @@ -272,8 +264,8 @@ Object { "statementMap": Object { "0": Object { "end": Object { - "column": 2, - "line": 11, + "column": 1, + "line": 12, }, "start": Object { "column": 0, @@ -282,72 +274,72 @@ Object { }, "1": Object { "end": Object { - "column": 11, + "column": 9, "line": 6, }, "start": Object { - "column": 18, + "column": 29, "line": 4, }, }, "2": Object { "end": Object { - "column": 30, + "column": 41, "line": 7, }, "start": Object { - "column": 18, + "column": 29, "line": 7, }, }, "3": Object { "end": Object { - "column": 39, + "column": 50, "line": 8, }, "start": Object { - "column": 18, + "column": 29, "line": 8, }, }, "4": Object { "end": Object { - "column": 79, + "column": 55, "line": 9, }, "start": Object { - "column": 13, + "column": 25, "line": 9, }, }, "5": Object { "end": Object { - "column": 46, + "column": 42, "line": 9, }, "start": Object { - "column": 34, + "column": 38, "line": 9, }, }, "6": Object { "end": Object { - "column": 77, + "column": 55, "line": 9, }, "start": Object { - "column": 65, + "column": 51, "line": 9, }, }, "7": Object { "end": Object { "column": 17, - "line": 10, + "line": 11, }, "start": Object { "column": 4, - "line": 10, + "line": 11, }, }, }, diff --git a/integration_tests/__tests__/coverage_remapping.test.js b/integration_tests/__tests__/coverage_remapping.test.js index f73c7145f18b..c61c979da480 100644 --- a/integration_tests/__tests__/coverage_remapping.test.js +++ b/integration_tests/__tests__/coverage_remapping.test.js @@ -26,7 +26,9 @@ beforeAll(() => { it('maps code coverage against original source', () => { run('yarn', dir); - runJest(dir, ['--coverage', '--mapCoverage', '--no-cache']); + const result = runJest(dir, ['--coverage', '--mapCoverage', '--no-cache']); + + expect(result.status).toBe(0); const coverageMapFile = path.join(coverageDir, 'coverage-final.json'); const coverageMap = JSON.parse(readFileSync(coverageMapFile, 'utf-8')); diff --git a/packages/jest-runtime/src/__tests__/script_transformer.test.js b/packages/jest-runtime/src/__tests__/script_transformer.test.js index 1111c8d98a3c..4e4da13c9d40 100644 --- a/packages/jest-runtime/src/__tests__/script_transformer.test.js +++ b/packages/jest-runtime/src/__tests__/script_transformer.test.js @@ -357,7 +357,7 @@ describe('ScriptTransformer', () => { ); }); - it('writes source maps if given by the transformer', () => { + it.skip('writes source maps if given by the transformer', () => { config = Object.assign(config, { transform: [['^.+\\.js$', 'preprocessor-with-sourcemaps']], }); diff --git a/packages/jest-runtime/src/script_transformer.js b/packages/jest-runtime/src/script_transformer.js index 2eb150960903..9cd85d276d44 100644 --- a/packages/jest-runtime/src/script_transformer.js +++ b/packages/jest-runtime/src/script_transformer.js @@ -261,7 +261,7 @@ export default class ScriptTransformer { code = transformed.code; } - if (transformed.map) { + if (instrument && mapCoverage && transformed.map) { const sourceMapContent = typeof transformed.map === 'string' ? transformed.map