Skip to content

Commit

Permalink
feat(jest-config): allow to use .js files in presets (jestjs#3564)
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-drexler committed Oct 21, 2017
1 parent 0748e6f commit 4009c9a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 13 deletions.
38 changes: 38 additions & 0 deletions packages/jest-config/src/__tests__/normalize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,44 @@ describe('preset', () => {
});
});

describe('preset with .js file', () => {
beforeEach(() => {
const Resolver = require('jest-resolve');
Resolver.findNodeModule = jest.fn(name => {
if (name === 'react-native/jest-preset.json') {
return '/node_modules/react-native/jest-preset-dont-exist.json';
}
if (name === 'react-native/jest-preset.js') {
return '/node_modules/react-native/jest-preset.js';
}
return '/node_modules/' + name;
});
jest.mock(
'/node_modules/react-native/jest-preset.js',
() => ({
moduleNameMapper: {b: 'b'},
modulePathIgnorePatterns: ['js'],
setupFiles: ['b'],
}),
{virtual: true},
);
});
afterEach(() => {
jest.unmock('/node_modules/react-native/jest-preset.js');
});

it('should resolve jest-preset.js file', () => {
const normalized = normalize(
{
preset: 'react-native',
rootDir: '/root/path/foo',
},
{},
);
expect(normalized.options.modulePathIgnorePatterns).toEqual(['js']);
});
});

describe('preset without setupFiles', () => {
let Resolver;
beforeEach(() => {
Expand Down
40 changes: 27 additions & 13 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,20 @@ import setFromArgv from './set_from_argv';
import VALID_CONFIG from './valid_config';
const ERROR = `${BULLET}Validation Error`;
const JSON_EXTENSION = '.json';
const PRESET_NAME = 'jest-preset' + JSON_EXTENSION;
const JS_EXTENSION = '.js';
const PRESET_NAME = 'jest-preset';

const requirePresetModule = (presetPath, rootDir): InitialOptions | null => {
try {
const presetModule = Resolver.findNodeModule(presetPath, {
basedir: rootDir,
});
// $FlowFixMe
return (require(presetModule): InitialOptions);
} catch (error) {
return null;
}
};

const createConfigError = message =>
new ValidationError(ERROR, message, DOCUMENTATION_NOTE);
Expand All @@ -51,19 +64,20 @@ const setupPreset = (
): InitialOptions => {
let preset;
const presetPath = _replaceRootDirInPath(options.rootDir, optionsPreset);
const presetModule = Resolver.findNodeModule(
presetPath.endsWith(JSON_EXTENSION)
? presetPath
: path.join(presetPath, PRESET_NAME),
{
basedir: options.rootDir,
},
);

try {
// $FlowFixMe
preset = (require(presetModule): InitialOptions);
} catch (error) {
if (presetPath.endsWith(JSON_EXTENSION)) {
preset = requirePresetModule(presetPath, options.rootDir);
} else {
[JSON_EXTENSION, JS_EXTENSION].some(extension => {
preset = requirePresetModule(
path.join(presetPath, PRESET_NAME + extension),
options.rootDir,
);
return preset !== null;
});
}

if (!preset) {
throw createConfigError(` Preset ${chalk.bold(presetPath)} not found.`);
}

Expand Down

0 comments on commit 4009c9a

Please sign in to comment.