Skip to content

Commit

Permalink
fix: make sure generated name in config is stable across runs of Jest
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Jan 29, 2019
1 parent 31b81ba commit d94585a
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 17 deletions.
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

0 comments on commit d94585a

Please sign in to comment.