Skip to content

Commit

Permalink
[jest-config] Changed "Preset ... not found" validation error (jestjs…
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanBacon authored and aleclarson committed Nov 15, 2019
1 parent 1f3ba7e commit 0aa70ab
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
23 changes: 23 additions & 0 deletions packages/jest-config/src/__tests__/normalize.test.js
Expand Up @@ -1021,6 +1021,29 @@ describe('preset', () => {
}).toThrowErrorMatchingSnapshot();
});

test('throws when a dependency is missing in the preset', () => {
jest.doMock(
'/node_modules/react-native-js-preset/jest-preset.js',
() => {
require('library-that-is-not-installed');
return {
transform: {},
};
},
{virtual: true},
);

expect(() => {
normalize(
{
preset: 'react-native-js-preset',
rootDir: '/root/path/foo',
},
{},
);
}).toThrowError(/Cannot find module 'library-that-is-not-installed'/);
});

test('throws when preset is invalid', () => {
jest.doMock('/node_modules/react-native/jest-preset.json', () =>
jest.requireActual('./jest-preset.json'),
Expand Down
32 changes: 24 additions & 8 deletions packages/jest-config/src/normalize.ts
Expand Up @@ -97,19 +97,35 @@ const setupPreset = (
);
}

const preset = Resolver.findNodeModule(presetPath, {
basedir: options.rootDir,
});
if (error.message.includes('Cannot find module')) {
if (error.message.includes(presetPath)) {
const preset = Resolver.findNodeModule(presetPath, {
basedir: options.rootDir,
});

if (preset) {
if (preset) {
throw createConfigError(
` Module ${chalk.bold(
presetPath,
)} should have "jest-preset.js" or "jest-preset.json" file at the root.`,
);
}
throw createConfigError(
` Preset ${chalk.bold(presetPath)} not found.`,
);
}
throw createConfigError(
` Module ${chalk.bold(
presetPath,
)} should have "jest-preset.js" or "jest-preset.json" file at the root.`,
` Missing dependency in ${chalk.bold(presetPath)}:\n\n ${
error.message
}\n ${error.stack}`,
);
}

throw createConfigError(` Preset ${chalk.bold(presetPath)} not found.`);
throw createConfigError(
` An unknown error occurred in ${chalk.bold(presetPath)}:\n\n ${
error.message
}\n ${error.stack}`,
);
}

if (options.setupFiles) {
Expand Down

0 comments on commit 0aa70ab

Please sign in to comment.