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: make sure generated name in config is stable across runs of Jest #7746

Merged
merged 7 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -11,6 +11,7 @@
- `[jest-cli]` Do not execute any `globalSetup` or `globalTeardown` if there are no tests to execute ([#7745](https://github.com/facebook/jest/pull/7745))
- `[jest-runtime]` Lock down version of `write-file-atomic` ([#7725](https://github.com/facebook/jest/pull/7725))
- `[jest-cli]` Print log entries when logging happens after test environment is torn down ([#7731](https://github.com/facebook/jest/pull/7731))
- `[jest-config]` Do not use a uuid as `name` since that breaks caching ([#7746](https://github.com/facebook/jest/pull/7746))

### Chore & Maintenance

Expand Down
19 changes: 19 additions & 0 deletions e2e/__tests__/multiProjectRunner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,25 @@ test("doesn't bleed module file extensions resolution with multiple workers", ()
};`,
});

const {stdout: configOutput} = runJest(DIR, [
'--show-config',
'--projects',
'project1',
'project2',
]);

const {configs} = JSON.parse(configOutput);

expect(configs).toHaveLength(2);

const [{name: name1}, {name: name2}] = configs;

expect(name1).toBeTruthy();
expect(name2).toBeTruthy();
expect(name1).toHaveLength(32);
expect(name2).toHaveLength(32);
expect(name1).not.toEqual(name2);

const {stderr} = runJest(DIR, [
'--no-watchman',
'-w=2',
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"jest-validate": "^24.0.0",
"micromatch": "^3.1.10",
"pretty-format": "^24.0.0",
"realpath-native": "^1.0.2",
"uuid": "^3.3.2"
"realpath-native": "^1.0.2"
},
"engines": {
"node": ">= 6"
Expand Down
54 changes: 41 additions & 13 deletions packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
*
*/

import crypto from 'crypto';
import path from 'path';
import {escapeStrForRegex} from 'jest-regex-util';
import normalize from '../normalize';

jest.mock('jest-resolve');
jest.mock('path', () => jest.requireActual('path').posix);
import {DEFAULT_JS_PATTERN} from '../constants';

const path = require('path');
const DEFAULT_JS_PATTERN = require('../constants').DEFAULT_JS_PATTERN;
const DEFAULT_CSS_PATTERN = '^.+\\.(css)$';

jest.mock('jest-resolve').mock('path', () => jest.requireActual('path').posix);

let root;
let expectedPathFooBar;
let expectedPathFooQux;
Expand Down Expand Up @@ -46,16 +47,33 @@ beforeEach(() => {
require('jest-resolve').findNodeModule = findNodeModule;
});

it('assigns a random 32-byte hash as a name to avoid clashes', () => {
it('picks a name based on the rootDir', () => {
const rootDir = '/root/path/foo';
const {name: name1} = normalize({rootDir}, {}).options;
const {name: name2} = normalize({rootDir}, {}).options;

expect(name1).toEqual(expect.any(String));
expect(name1).toHaveLength(32);
expect(name2).toEqual(expect.any(String));
expect(name2).toHaveLength(32);
expect(name1).not.toBe(name2);
const expected = crypto
.createHash('md5')
.update('/root/path/foo')
.digest('hex');
expect(
normalize(
{
rootDir,
},
{},
).options.name,
).toBe(expected);
});

it('keeps custom project name based on the projects rootDir', () => {
const name = 'test';
const options = normalize(
{
projects: [{name, rootDir: '/path/to/foo'}],
rootDir: '/root/path/baz',
},
{},
);

expect(options.options.projects[0].name).toBe(name);
});

it('keeps custom names based on the rootDir', () => {
Expand All @@ -70,6 +88,16 @@ it('keeps custom names based on the rootDir', () => {
).toBe('custom-name');
});

it('minimal config is stable across runs', () => {
const firstNormalization = normalize({rootDir: '/root/path/foo'}, {});
const secondNormalization = normalize({rootDir: '/root/path/foo'}, {});

expect(firstNormalization).toEqual(secondNormalization);
expect(JSON.stringify(firstNormalization)).toBe(
JSON.stringify(secondNormalization),
);
});

it('sets coverageReporters correctly when argv.json is set', () => {
expect(
normalize(
Expand Down
11 changes: 8 additions & 3 deletions packages/jest-config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ export function readConfig(
rawOptions = readConfigFileAndSetRootDir(configPath);
}

const {options, hasDeprecationWarnings} = normalize(rawOptions, argv);
const {options, hasDeprecationWarnings} = normalize(
rawOptions,
argv,
configPath,
);

const {globalConfig, projectConfig} = groupOptions(options);
return {
configPath,
Expand Down Expand Up @@ -210,7 +215,7 @@ const groupOptions = (
}),
});

const ensureNoDuplicateConfigs = (parsedConfigs, projects, rootConfigPath) => {
const ensureNoDuplicateConfigs = (parsedConfigs, projects) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my IDE told me this was an unused arg 🤷‍♂️

const configPathMap = new Map();

for (const config of parsedConfigs) {
Expand Down Expand Up @@ -299,7 +304,7 @@ export function readConfigs(
})
.map(root => readConfig(argv, root, true, configPath));

ensureNoDuplicateConfigs(parsedConfigs, projects, configPath);
ensureNoDuplicateConfigs(parsedConfigs, projects);
configs = parsedConfigs.map(({projectConfig}) => projectConfig);
if (!hasDeprecationWarnings) {
hasDeprecationWarnings = parsedConfigs.some(
Expand Down
21 changes: 16 additions & 5 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import type {
DefaultOptions,
ReporterConfig,
GlobalConfig,
Path,
ProjectConfig,
} from 'types/Config';

import crypto from 'crypto';
import uuid from 'uuid/v4';
import glob from 'glob';
import path from 'path';
import {ValidationError, validate} from 'jest-validate';
Expand Down Expand Up @@ -264,12 +264,16 @@ const normalizePreprocessor = (options: InitialOptions): InitialOptions => {
return options;
};

const normalizeMissingOptions = (options: InitialOptions): InitialOptions => {
const normalizeMissingOptions = (
options: InitialOptions,
configPath: ?Path,
): InitialOptions => {
if (!options.name) {
options.name = crypto
.createHash('md5')
.update(options.rootDir)
.update(uuid())
// In case we load config from some path that has the same root dir
.update(configPath || '')
.digest('hex');
}

Expand Down Expand Up @@ -375,7 +379,11 @@ const showTestPathPatternError = (testPathPattern: string) => {
);
};

export default function normalize(options: InitialOptions, argv: Argv) {
export default function normalize(
options: InitialOptions,
argv: Argv,
configPath: ?Path,
) {
const {hasDeprecationWarnings} = validate(options, {
comment: DOCUMENTATION_NOTE,
deprecatedConfig: DEPRECATED_CONFIG,
Expand All @@ -394,7 +402,10 @@ export default function normalize(options: InitialOptions, argv: Argv) {

options = normalizePreprocessor(
normalizeReporters(
normalizeMissingOptions(normalizeRootDir(setFromArgv(options, argv))),
normalizeMissingOptions(
normalizeRootDir(setFromArgv(options, argv)),
configPath,
),
),
);

Expand Down