Skip to content

Gracefully return not found if jest config not found #32

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
19 changes: 10 additions & 9 deletions src/__tests__/resolver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,16 @@ describe('Jest resolver', () => {
});

test('Throws when specified config file does not exist', () => {
expect(() => {
jestResolver.resolve(
'resolveme',
'/path/to/project/__tests__/iamtest.js',
{
jestConfigFile: '__tests__/idonotexist.json',
}
);
}).toThrow(/not\sfound/);
const result = jestResolver.resolve(
'resolveme',
'/path/to/project/__tests__/iamtest.js',
{
jestConfigFile: '__tests__/idonotexist.json',
}
);
expect(result).toEqual({
found: false,
});
});

test('Resolves when <rootDir> is in testMatch', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ exports.resolve = function resolver(
config?: ResolverConfig
): ResolverResult {
const jestConfig = getJestConfig(config, file);
if (!isTestFile(jestConfig, file)) {
if (jestConfig == null || !isTestFile(jestConfig, file)) {
return NOTFOUND;
}
const pathToResolve = applyModuleNameMapper(jestConfig, source) || source;
Expand All @@ -67,15 +67,15 @@ exports.resolve = function resolver(
* Will use jest configuration from package.json if no jestConfigFile is defined
* Applies default configuration for any required properties that are undeclared
*/
function getJestConfig(config?: ResolverConfig = {}, file: Path): JestConfig {
function getJestConfig(config?: ResolverConfig = {}, file: Path): ?JestConfig {
let jestConfig;
const root = findRoot(file);
if (config.jestConfigFile) {
const configFilePath = path.resolve(root, config.jestConfigFile);
try {
jestConfig = require(configFilePath);
} catch (e) {
throw new Error(`jestConfigFile not found in ${configFilePath}`);
return undefined;
}
} else {
const packageJson = require(path.resolve(root, 'package.json'));
Expand Down