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 2 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
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
116 changes: 103 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,95 @@ 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 expected = crypto
.createHash('md5')
.update('/root/path/foo')
.digest('hex');
expect(
normalize(
{
rootDir,
},
{},
).options.name,
).toBe(expected);
});

it('picks a name for projects based on the main 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 firstExpected = crypto
.createHash('md5')
.update('/root/path/foo')
.digest('hex');
const secondExpected = crypto
.createHash('md5')
.update('/root/path/foo:1')
.digest('hex');

const options = normalize(
{
projects: [{}, {}],
rootDir,
},
{},
);

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

it('picks a name for projects based on the projects rootDir', () => {
const firstRootDir = '/root/path/foo';
const secondRootDir = '/root/path/bar';
const firstExpected = crypto
.createHash('md5')
.update('/root/path/foo')
.digest('hex');
const secondExpected = crypto
.createHash('md5')
.update('/root/path/foo:1')
.digest('hex');
const thirdExpected = crypto
.createHash('md5')
.update('/root/path/bar')
.digest('hex');
const fourthExpected = crypto
.createHash('md5')
.update('/root/path/baz')
.digest('hex');

const options = normalize(
{
projects: [
{rootDir: firstRootDir},
{rootDir: firstRootDir},
{rootDir: secondRootDir},
{},
],
rootDir: '/root/path/baz',
},
{},
);

expect(options.options.projects[0].name).toBe(firstExpected);
expect(options.options.projects[1].name).toBe(secondExpected);
expect(options.options.projects[2].name).toBe(thirdExpected);
expect(options.options.projects[3].name).toBe(fourthExpected);
});

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 +150,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
22 changes: 20 additions & 2 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import type {
} 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 @@ -265,14 +264,33 @@ const normalizePreprocessor = (options: InitialOptions): InitialOptions => {
};

const normalizeMissingOptions = (options: InitialOptions): InitialOptions => {
const knownRootDirs: Set<string> = new Set();
if (!options.name) {
options.name = crypto
.createHash('md5')
.update(options.rootDir)
.update(uuid())
.digest('hex');
}

if (Array.isArray(options.projects)) {
options.projects = options.projects.map((project, index) => {
if (typeof project !== 'string' && !project.name) {
let rootDir = project.rootDir || options.rootDir;
if (knownRootDirs.has(rootDir)) {
rootDir = `${rootDir}:${index}`;
}

knownRootDirs.add(rootDir);
project.name = crypto
.createHash('md5')
.update(rootDir)
.digest('hex');
}

return project;
});
}

if (!options.setupFiles) {
options.setupFiles = [];
}
Expand Down