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: serialize changedFiles passed to workers #8090

Merged
merged 5 commits into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 28 additions & 2 deletions e2e/__tests__/onlyChanged.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
thymikee marked this conversation as resolved.
Show resolved Hide resolved
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', () => {
Expand Down
7 changes: 6 additions & 1 deletion packages/jest-reporters/src/coverage_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand Down
6 changes: 3 additions & 3 deletions packages/jest-reporters/src/coverage_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -18,7 +18,7 @@ export type CoverageWorkerData = {
globalConfig: Config.GlobalConfig;
config: Config.ProjectConfig;
path: Config.Path;
options?: CoverageReporterOptions;
options?: CoverageReporterSerializedOptions;
};

export {CoverageWorkerResult};
Expand All @@ -40,6 +40,6 @@ export function worker({
path,
globalConfig,
config,
options && options.changedFiles,
options && options.changedFiles && new Set(options.changedFiles),
);
}
4 changes: 4 additions & 0 deletions packages/jest-reporters/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export type CoverageReporterOptions = {
changedFiles?: Set<Config.Path>;
};

export type CoverageReporterSerializedOptions = {
changedFiles?: Array<Config.Path>;
};

export type OnTestStart = (test: Test) => Promise<void>;
export type OnTestFailure = (
test: Test,
Expand Down
7 changes: 6 additions & 1 deletion packages/jest-runner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 6 additions & 3 deletions packages/jest-runner/src/testWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ 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 = {
config: Config.ProjectConfig;
globalConfig: Config.GlobalConfig;
path: Config.Path;
serializableModuleMap: SerializableModuleMap | null;
context?: TestRunnerContext;
context?: TestRunnerSerializedContext;
};

// Make sure uncaught errors are logged before we exit.
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-runner/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export type TestRunnerContext = {
changedFiles?: Set<Config.Path>;
};

export type TestRunnerSerializedContext = {
changedFiles?: Array<Config.Path>;
};

// TODO: Should live in `@jest/core` or `jest-watcher`
export type WatcherState = {
interrupted: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"private": true,
"license": "MIT",
"main": "build/index.js",
"types": "src/index.d.ts"
"types": "build/index.d.ts"
}