Skip to content

Commit 17ffcc9

Browse files
authored
fix: serialize changedFiles passed to workers (#8090)
1 parent 95535e0 commit 17ffcc9

File tree

9 files changed

+59
-11
lines changed

9 files changed

+59
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- `[expect]` Compare DOM nodes even if there are multiple Node classes ([#8064](https://github.com/facebook/jest/pull/8064))
88
- `[jest-worker]` `worker.getStdout()` can return `null` ([#8083](https://github.com/facebook/jest/pull/8083))
99
- `[jest-worker]` Re-attach stdout and stderr from new processes/threads created after retries ([#8087](https://github.com/facebook/jest/pull/8087))
10+
- `[jest-reporters/jest-runner]` Serialize `changedFiles` passed to workers ([#8090](https://github.com/facebook/jest/pull/8090))
1011

1112
### Chore & Maintenance
1213

e2e/__tests__/onlyChanged.test.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,38 @@ test('do not pickup non-tested files when reporting coverage on only changed fil
153153
'package.json': JSON.stringify({name: 'new name'}),
154154
});
155155

156-
const {stderr, stdout} = runJest(DIR, ['-o', '--coverage']);
157-
156+
const {stderr, stdout, status} = runJest(DIR, ['-o', '--coverage']);
158157
expect(stderr).toEqual(
159158
expect.not.stringContaining('Failed to collect coverage from'),
160159
);
161160
expect(stdout).toEqual(expect.not.stringContaining('package.json'));
161+
expect(status).toBe(0);
162+
});
163+
164+
test('collect test coverage when using onlyChanged', () => {
165+
writeFiles(DIR, {
166+
'a.js': 'module.exports = {}',
167+
'b.test.js': 'module.exports = {}',
168+
'package.json': JSON.stringify({
169+
jest: {collectCoverageFrom: ['a.js']},
170+
name: 'original name',
171+
}),
172+
});
173+
174+
run(`${GIT} init`, DIR);
175+
run(`${GIT} add .`, DIR);
176+
run(`${GIT} commit --no-gpg-sign -m "first"`, DIR);
177+
run(`${GIT} checkout -b new-branch`, DIR);
178+
179+
writeFiles(DIR, {
180+
'b.test.js': 'it("passes", () => {expect(1).toBe(1)})',
181+
});
182+
183+
const {stderr, status} = runJest(DIR, ['-o', '--coverage']);
184+
expect(stderr).toEqual(
185+
expect.not.stringContaining('Failed to collect coverage from'),
186+
);
187+
expect(status).toBe(0);
162188
});
163189

164190
test('onlyChanged in config is overwritten by --all or testPathPattern', () => {

packages/jest-reporters/src/coverage_reporter.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,12 @@ export default class CoverageReporter extends BaseReporter {
170170
const result = await worker.worker({
171171
config,
172172
globalConfig,
173-
options: this._options,
173+
options: {
174+
...this._options,
175+
changedFiles:
176+
this._options.changedFiles &&
177+
Array.from(this._options.changedFiles),
178+
},
174179
path: filename,
175180
});
176181

packages/jest-reporters/src/coverage_worker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import fs from 'fs';
99
import {Config} from '@jest/types';
1010
import exit from 'exit';
11-
import {CoverageReporterOptions} from './types';
11+
import {CoverageReporterSerializedOptions} from './types';
1212

1313
import generateEmptyCoverage, {
1414
CoverageWorkerResult,
@@ -18,7 +18,7 @@ export type CoverageWorkerData = {
1818
globalConfig: Config.GlobalConfig;
1919
config: Config.ProjectConfig;
2020
path: Config.Path;
21-
options?: CoverageReporterOptions;
21+
options?: CoverageReporterSerializedOptions;
2222
};
2323

2424
export {CoverageWorkerResult};
@@ -40,6 +40,6 @@ export function worker({
4040
path,
4141
globalConfig,
4242
config,
43-
options && options.changedFiles,
43+
options && options.changedFiles && new Set(options.changedFiles),
4444
);
4545
}

packages/jest-reporters/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ export type CoverageReporterOptions = {
4141
changedFiles?: Set<Config.Path>;
4242
};
4343

44+
export type CoverageReporterSerializedOptions = {
45+
changedFiles?: Array<Config.Path>;
46+
};
47+
4448
export type OnTestStart = (test: Test) => Promise<void>;
4549
export type OnTestFailure = (
4650
test: Test,

packages/jest-runner/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,12 @@ class TestRunner {
127127

128128
return worker.worker({
129129
config: test.context.config,
130-
context: this._context,
130+
context: {
131+
...this._context,
132+
changedFiles:
133+
this._context.changedFiles &&
134+
Array.from(this._context.changedFiles),
135+
},
131136
globalConfig: this._globalConfig,
132137
path: test.path,
133138
serializableModuleMap: watcher.isWatchMode()

packages/jest-runner/src/testWorker.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ import HasteMap, {SerializableModuleMap, ModuleMap} from 'jest-haste-map';
1212
import exit from 'exit';
1313
import {separateMessageFromStack} from 'jest-message-util';
1414
import Runtime from 'jest-runtime';
15-
import {ErrorWithCode, TestRunnerContext} from './types';
15+
import {ErrorWithCode, TestRunnerSerializedContext} from './types';
1616
import runTest from './runTest';
1717

1818
type WorkerData = {
1919
config: Config.ProjectConfig;
2020
globalConfig: Config.GlobalConfig;
2121
path: Config.Path;
2222
serializableModuleMap: SerializableModuleMap | null;
23-
context?: TestRunnerContext;
23+
context?: TestRunnerSerializedContext;
2424
};
2525

2626
// Make sure uncaught errors are logged before we exit.
@@ -86,7 +86,10 @@ export async function worker({
8686
globalConfig,
8787
config,
8888
getResolver(config, moduleMap),
89-
context,
89+
context && {
90+
...context,
91+
changedFiles: context.changedFiles && new Set(context.changedFiles),
92+
},
9093
);
9194
} catch (error) {
9295
throw formatError(error);

packages/jest-runner/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ export type TestRunnerContext = {
5353
changedFiles?: Set<Config.Path>;
5454
};
5555

56+
export type TestRunnerSerializedContext = {
57+
changedFiles?: Array<Config.Path>;
58+
};
59+
5660
// TODO: Should live in `@jest/core` or `jest-watcher`
5761
export type WatcherState = {
5862
interrupted: boolean;

packages/test-utils/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
"private": true,
55
"license": "MIT",
66
"main": "build/index.js",
7-
"types": "src/index.d.ts"
7+
"types": "build/index.d.ts"
88
}

0 commit comments

Comments
 (0)