diff --git a/CHANGELOG.md b/CHANGELOG.md index 388500960c05..9cb6f47f709c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - `[expect]` Compare DOM nodes even if there are multiple Node classes ([#8064](https://github.com/facebook/jest/pull/8064)) - `[jest-worker]` `worker.getStdout()` can return `null` ([#8083](https://github.com/facebook/jest/pull/8083)) - `[jest-worker]` Re-attach stdout and stderr from new processes/threads created after retries ([#8087](https://github.com/facebook/jest/pull/8087)) +- `[jest-reporters/jest-runner]` Serialize `changedFiles` passed to workers ([#8090](https://github.com/facebook/jest/pull/8090)) ### Chore & Maintenance diff --git a/e2e/__tests__/onlyChanged.test.ts b/e2e/__tests__/onlyChanged.test.ts index 1fdcfadd4374..75ded6c7b0ae 100644 --- a/e2e/__tests__/onlyChanged.test.ts +++ b/e2e/__tests__/onlyChanged.test.ts @@ -153,12 +153,38 @@ test('do not pickup non-tested files when reporting coverage on only changed fil 'package.json': JSON.stringify({name: 'new name'}), }); - const {stderr, stdout} = runJest(DIR, ['-o', '--coverage']); - + const {stderr, stdout, status} = runJest(DIR, ['-o', '--coverage']); expect(stderr).toEqual( expect.not.stringContaining('Failed to collect coverage from'), ); expect(stdout).toEqual(expect.not.stringContaining('package.json')); + expect(status).toBe(0); +}); + +test('collect test coverage when using onlyChanged', () => { + writeFiles(DIR, { + 'a.js': 'module.exports = {}', + 'b.test.js': 'module.exports = {}', + 'package.json': JSON.stringify({ + jest: {collectCoverageFrom: ['a.js']}, + name: 'original name', + }), + }); + + run(`${GIT} init`, DIR); + run(`${GIT} add .`, DIR); + run(`${GIT} commit --no-gpg-sign -m "first"`, DIR); + run(`${GIT} checkout -b new-branch`, DIR); + + writeFiles(DIR, { + 'b.test.js': 'it("passes", () => {expect(1).toBe(1)})', + }); + + const {stderr, status} = runJest(DIR, ['-o', '--coverage']); + expect(stderr).toEqual( + expect.not.stringContaining('Failed to collect coverage from'), + ); + expect(status).toBe(0); }); test('onlyChanged in config is overwritten by --all or testPathPattern', () => { diff --git a/packages/jest-reporters/src/coverage_reporter.ts b/packages/jest-reporters/src/coverage_reporter.ts index 2799cfd04a31..bb761944b417 100644 --- a/packages/jest-reporters/src/coverage_reporter.ts +++ b/packages/jest-reporters/src/coverage_reporter.ts @@ -170,7 +170,12 @@ export default class CoverageReporter extends BaseReporter { const result = await worker.worker({ config, globalConfig, - options: this._options, + options: { + ...this._options, + changedFiles: + this._options.changedFiles && + Array.from(this._options.changedFiles), + }, path: filename, }); diff --git a/packages/jest-reporters/src/coverage_worker.ts b/packages/jest-reporters/src/coverage_worker.ts index 6461a7973fde..6bc5e6731d27 100644 --- a/packages/jest-reporters/src/coverage_worker.ts +++ b/packages/jest-reporters/src/coverage_worker.ts @@ -8,7 +8,7 @@ import fs from 'fs'; import {Config} from '@jest/types'; import exit from 'exit'; -import {CoverageReporterOptions} from './types'; +import {CoverageReporterSerializedOptions} from './types'; import generateEmptyCoverage, { CoverageWorkerResult, @@ -18,7 +18,7 @@ export type CoverageWorkerData = { globalConfig: Config.GlobalConfig; config: Config.ProjectConfig; path: Config.Path; - options?: CoverageReporterOptions; + options?: CoverageReporterSerializedOptions; }; export {CoverageWorkerResult}; @@ -40,6 +40,6 @@ export function worker({ path, globalConfig, config, - options && options.changedFiles, + options && options.changedFiles && new Set(options.changedFiles), ); } diff --git a/packages/jest-reporters/src/types.ts b/packages/jest-reporters/src/types.ts index 9c0270b1c745..a5029660ef29 100644 --- a/packages/jest-reporters/src/types.ts +++ b/packages/jest-reporters/src/types.ts @@ -41,6 +41,10 @@ export type CoverageReporterOptions = { changedFiles?: Set; }; +export type CoverageReporterSerializedOptions = { + changedFiles?: Array; +}; + export type OnTestStart = (test: Test) => Promise; export type OnTestFailure = ( test: Test, diff --git a/packages/jest-runner/src/index.ts b/packages/jest-runner/src/index.ts index c18063a3a590..94a379592501 100644 --- a/packages/jest-runner/src/index.ts +++ b/packages/jest-runner/src/index.ts @@ -127,7 +127,12 @@ class TestRunner { return worker.worker({ config: test.context.config, - context: this._context, + context: { + ...this._context, + changedFiles: + this._context.changedFiles && + Array.from(this._context.changedFiles), + }, globalConfig: this._globalConfig, path: test.path, serializableModuleMap: watcher.isWatchMode() diff --git a/packages/jest-runner/src/testWorker.ts b/packages/jest-runner/src/testWorker.ts index 3cd4309fb6e7..6bb94790b204 100644 --- a/packages/jest-runner/src/testWorker.ts +++ b/packages/jest-runner/src/testWorker.ts @@ -12,7 +12,7 @@ import HasteMap, {SerializableModuleMap, ModuleMap} from 'jest-haste-map'; import exit from 'exit'; import {separateMessageFromStack} from 'jest-message-util'; import Runtime from 'jest-runtime'; -import {ErrorWithCode, TestRunnerContext} from './types'; +import {ErrorWithCode, TestRunnerSerializedContext} from './types'; import runTest from './runTest'; type WorkerData = { @@ -20,7 +20,7 @@ type WorkerData = { globalConfig: Config.GlobalConfig; path: Config.Path; serializableModuleMap: SerializableModuleMap | null; - context?: TestRunnerContext; + context?: TestRunnerSerializedContext; }; // Make sure uncaught errors are logged before we exit. @@ -86,7 +86,10 @@ export async function worker({ globalConfig, config, getResolver(config, moduleMap), - context, + context && { + ...context, + changedFiles: context.changedFiles && new Set(context.changedFiles), + }, ); } catch (error) { throw formatError(error); diff --git a/packages/jest-runner/src/types.ts b/packages/jest-runner/src/types.ts index 7f3aa4a626c8..57e54a2b0166 100644 --- a/packages/jest-runner/src/types.ts +++ b/packages/jest-runner/src/types.ts @@ -53,6 +53,10 @@ export type TestRunnerContext = { changedFiles?: Set; }; +export type TestRunnerSerializedContext = { + changedFiles?: Array; +}; + // TODO: Should live in `@jest/core` or `jest-watcher` export type WatcherState = { interrupted: boolean; diff --git a/packages/test-utils/package.json b/packages/test-utils/package.json index f2573bb024d1..cd25632fb2bb 100644 --- a/packages/test-utils/package.json +++ b/packages/test-utils/package.json @@ -4,5 +4,5 @@ "private": true, "license": "MIT", "main": "build/index.js", - "types": "src/index.d.ts" + "types": "build/index.d.ts" }