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

Throw when moduleNameMapper points to inexistent module #3567

Merged
merged 27 commits into from
Jun 29, 2017
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
7508200
Fixes issue-1888
lindgr3n May 14, 2017
0ffa9ca
Prettier fix and chalk to package.json dependencies
lindgr3n May 14, 2017
c7a412f
Added integration test
lindgr3n May 16, 2017
6148367
Refactored test to use snapshot
lindgr3n May 16, 2017
1638f31
Rename to camelCase
lindgr3n May 16, 2017
c4ec69c
Fixed lint warnings
lindgr3n May 16, 2017
a4e5cd9
Added integration test
lindgr3n May 16, 2017
e42de45
Refactored test to use snapshot
lindgr3n May 16, 2017
a3e9a74
Fixed lint warnings
lindgr3n May 16, 2017
ae6e2e8
Minor fixes
lindgr3n May 16, 2017
ee40b20
Use strict on integration tests
thymikee May 17, 2017
c8e99b0
Added integration test
lindgr3n May 16, 2017
48e965c
Refactored test to use snapshot
lindgr3n May 16, 2017
c2cefb8
Fixed lint warnings
lindgr3n May 16, 2017
c0ca429
Added integration test
lindgr3n May 16, 2017
f0238fc
Refactored test to use snapshot
lindgr3n May 16, 2017
2d49788
Minor fixes
lindgr3n May 16, 2017
3961af0
Use strict on integration tests
thymikee May 17, 2017
be6995f
Use Error instead of ValidationError to avoid duplicate message
thymikee Jun 27, 2017
0859f30
Add fb copyright back to auto_reset_mocks test
thymikee Jun 27, 2017
e5bd79d
Removed jest-validate dep from jest-resolve
thymikee Jun 27, 2017
22f13ed
Run prettier
thymikee Jun 28, 2017
5f4b5b0
Revert auto_clear_mocks change
thymikee Jun 29, 2017
fe24f1a
Revert auto_reset_mocks change
thymikee Jun 29, 2017
f6e5b14
Normalize test paths on windows
thymikee Jun 29, 2017
7f2ad5a
Don't use slash to normalize summary
thymikee Jun 29, 2017
62952cd
Skip test on windows
thymikee Jun 29, 2017
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
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\\"
}

"
`;
22 changes: 14 additions & 8 deletions integration_tests/__tests__/auto_clear_mocks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@
* 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.
*
* @emails oncall+jsinfra
*/

'use strict';

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

test('suite with auto-clear', () => {
const result = runJest('auto-clear-mocks/with-auto-clear');
expect(result.status).toBe(0);
test('moduleNameMapper wrong configuration', () => {
const {stderr, status} = runJest('moduleNameMapper-wrong-config');
const {rest} = extractSummary(stderr);

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

test('suite without auto-clear', () => {
const result = runJest('auto-clear-mocks/without-auto-clear');
expect(result.status).toBe(0);
test('moduleNameMapper correct configuration', () => {
const {stderr, status} = runJest('moduleNameMapper-correct-config');
const {rest} = extractSummary(stderr);

expect(status).toBe(0);
expect(rest).toMatchSnapshot();
});
1 change: 1 addition & 0 deletions integration_tests/__tests__/auto_reset_mocks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*
* @emails oncall+jsinfra
*/

'use strict';

const runJest = require('../runJest');
Expand Down
27 changes: 27 additions & 0 deletions integration_tests/__tests__/module_name_mapper.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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');

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
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