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 1 commit
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
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
62 changes: 0 additions & 62 deletions packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,68 +63,6 @@ it('picks a name based on the rootDir', () => {
).toBe(expected);
});

it('picks a name for projects based on the main rootDir', () => {
const rootDir = '/root/path/foo';
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(
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
39 changes: 16 additions & 23 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
DefaultOptions,
ReporterConfig,
GlobalConfig,
Path,
ProjectConfig,
} from 'types/Config';

Expand Down Expand Up @@ -263,34 +264,19 @@ const normalizePreprocessor = (options: InitialOptions): InitialOptions => {
return options;
};

const normalizeMissingOptions = (options: InitialOptions): InitialOptions => {
const knownRootDirs: Set<string> = new Set();
const normalizeMissingOptions = (
options: InitialOptions,
configPath: ?Path,
): InitialOptions => {
if (!options.name) {
options.name = crypto
.createHash('md5')
.update(options.rootDir)
// In case we load config from some path that has the same root dir
.update(configPath || '')
.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 Expand Up @@ -393,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 @@ -412,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