From 997c40c1404fcb1bfe17973434994dbd43dc8f3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Fri, 8 Mar 2019 18:41:56 +0100 Subject: [PATCH 1/4] fix: serialize changedFiles passed to workers --- packages/jest-reporters/src/coverage_reporter.ts | 7 ++++++- packages/jest-reporters/src/coverage_worker.ts | 6 +++--- packages/jest-reporters/src/types.ts | 4 ++++ packages/jest-runner/src/index.ts | 7 ++++++- packages/jest-runner/src/testWorker.ts | 9 ++++++--- packages/jest-runner/src/types.ts | 4 ++++ 6 files changed, 29 insertions(+), 8 deletions(-) diff --git a/packages/jest-reporters/src/coverage_reporter.ts b/packages/jest-reporters/src/coverage_reporter.ts index 698abc17e4ff..55f98b34f3f9 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..6f7046470835 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 && { + ...config, + 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; From 43e2e3c05a291455f5638ea3af165600f0d2df6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Fri, 8 Mar 2019 18:47:22 +0100 Subject: [PATCH 2/4] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9983aabc1980..517f53d69851 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,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-reporters/jest-runner]` Serialize `changedFiles` passed to workers ([#8090](https://github.com/facebook/jest/pull/8090)) ### Chore & Maintenance From 2bac0e9e92a0a90bcc54377058ddc6a1ddaf8c9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Fri, 8 Mar 2019 18:59:42 +0100 Subject: [PATCH 3/4] fix typo --- packages/jest-runner/src/testWorker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-runner/src/testWorker.ts b/packages/jest-runner/src/testWorker.ts index 6f7046470835..6bb94790b204 100644 --- a/packages/jest-runner/src/testWorker.ts +++ b/packages/jest-runner/src/testWorker.ts @@ -87,7 +87,7 @@ export async function worker({ config, getResolver(config, moduleMap), context && { - ...config, + ...context, changedFiles: context.changedFiles && new Set(context.changedFiles), }, ); From dff08798fdcff379c9ed031a5882d5e3ecc56658 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 8 Mar 2019 19:33:03 +0100 Subject: [PATCH 4/4] add test --- e2e/__tests__/onlyChanged.test.ts | 30 ++++++++++++++++++++++++++++-- packages/test-utils/package.json | 2 +- 2 files changed, 29 insertions(+), 3 deletions(-) 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/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" }