Skip to content

Commit

Permalink
Throw when moduleNameMapper points to inexistent module (#3567)
Browse files Browse the repository at this point in the history
* Fixes issue-1888

* Prettier fix and chalk to package.json dependencies

* Added integration test

* Added integration test

* Refactored test to use snapshot

* Refactored test to use snapshot

* Rename to camelCase

* Fixed lint warnings

* Fixed lint warnings

* Minor fixes

* Use strict on integration tests

* Added integration test

* Refactored test to use snapshot

* Fixed lint warnings

* Added integration test

* Refactored test to use snapshot

* Minor fixes

* Use strict on integration tests

* Use Error instead of ValidationError to avoid duplicate message

* Add fb copyright back to auto_reset_mocks test

* Removed jest-validate dep from jest-resolve

* Run prettier

* Revert auto_clear_mocks change

* Revert auto_reset_mocks change

* Normalize test paths on windows

* Don't use slash to normalize summary

* Skip test on windows
  • Loading branch information
lindgr3n authored and cpojer committed Jun 29, 2017
1 parent aa33f2a commit 30f35c3
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`moduleNameMapper correct configuration 1`] = `
" PASS __tests__/index.js
✓ moduleNameMapping correct configuration
"
`;

exports[`moduleNameMapper wrong configuration 1`] = `
" FAIL __tests__/index.js
● Test suite failed to run
Configuration error:
Unknown module in configuration option moduleNameMapper
Please check:
\\"moduleNameMapper\\": {
\\"/\\\\.(css|less)$/\\": \\"no-such-module\\"
}
"
`;
31 changes: 31 additions & 0 deletions integration_tests/__tests__/module_name_mapper.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

const runJest = require('../runJest');
const {extractSummary} = require('../utils');
const skipOnWindows = require('skipOnWindows');

// Works on windows, we just need to adjust snapshot test output
skipOnWindows.suite();

test('moduleNameMapper wrong configuration', () => {
const {stderr, status} = runJest('module_name_mapper_wrong_config');
const {rest} = extractSummary(stderr);

expect(status).toBe(1);
expect(rest).toMatchSnapshot();
});

test('moduleNameMapper correct configuration', () => {
const {stderr, status} = runJest('module_name_mapper_correct_config');
const {rest} = extractSummary(stderr);

expect(status).toBe(0);
expect(rest).toMatchSnapshot();
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/

module.exports = () => {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

'use strict';

module.exports = {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

'use strict';

const importedFn = require('../');

test('moduleNameMapping correct configuration', () => {
expect(importedFn).toBeDefined();
});
13 changes: 13 additions & 0 deletions integration_tests/module_name_mapper_correct_config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

'use strict';

require('./style.css');

module.exports = () => 'test';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"jest": {
"moduleNameMapper": {
"\\.(css|less)$": "./__mocks__/style_mock.js"
}
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

'use strict';

const importedFn = require('../');

test('moduleNameMapping wrong configuration', () => {
expect(importedFn).toBeDefined();
});
13 changes: 13 additions & 0 deletions integration_tests/module_name_mapper_wrong_config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

'use strict';

require('./style.css');

module.exports = () => 'test';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"jest": {
"moduleNameMapper": {
"\\.(css|less)$": "no-such-module"
}
}
}
Empty file.
1 change: 1 addition & 0 deletions packages/jest-resolve/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"main": "build/index.js",
"dependencies": {
"browser-resolve": "^1.11.2",
"chalk": "^1.1.3",
"is-builtin-module": "^1.0.0",
"resolve": "^1.3.2"
},
Expand Down
24 changes: 20 additions & 4 deletions packages/jest-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import path from 'path';
import nodeModulesPaths from 'resolve/lib/node-modules-paths';
import isBuiltinModule from 'is-builtin-module';
import defaultResolver from './default_resolver.js';
import chalk from 'chalk';

type ResolverConfig = {|
browser?: boolean,
Expand Down Expand Up @@ -317,8 +318,8 @@ class Resolver {
const paths = this._options.modulePaths;
const extensions = this._options.extensions;
const moduleDirectory = this._options.moduleDirectories;

const moduleNameMapper = this._options.moduleNameMapper;

if (moduleNameMapper) {
for (const {moduleName: mappedModuleName, regex} of moduleNameMapper) {
if (regex.test(moduleName)) {
Expand All @@ -331,16 +332,31 @@ class Resolver {
(_, index) => matches[parseInt(index, 10)],
);
}
return (

const module =
this.getModule(moduleName) ||
Resolver.findNodeModule(moduleName, {
basedir: dirname,
browser: this._options.browser,
extensions,
moduleDirectory,
paths,
})
);
});
if (!module) {
const error = new Error(
chalk.red(`${chalk.bold('Configuration error')}:
Unknown module in configuration option ${chalk.bold('moduleNameMapper')}
Please check:
"moduleNameMapper": {
"${regex.toString()}": "${chalk.bold(moduleName)}"
}`),
);
error.stack = '';
throw error;
}
return module;
}
}
}
Expand Down

0 comments on commit 30f35c3

Please sign in to comment.