Skip to content

Commit

Permalink
Make mocks folder configurable (like the tests folder).
Browse files Browse the repository at this point in the history
  • Loading branch information
cpojer committed Nov 17, 2015
1 parent ca467f9 commit 40f1e1e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
6 changes: 6 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ permalink: docs/api.html
- [`config.collectCoverage` [boolean]](#config-collectcoverage-boolean)
- [`config.collectCoverageOnlyFrom` [object]](#config-collectcoverageonlyfrom-object)
- [`config.globals` [object]](#config-globals-object)
- [`config.mocksPattern` [string]](#config-mockspattern-string)
- [`config.moduleFileExtensions` [array<string>]](#config-modulefileextensions-array-string)
- [`config.modulePathIgnorePatterns` [array<string>]](#config-modulepathignorepatterns-array-string)
- [`config.moduleNameMapper` [object<string, string>]](#config-modulenamemapper-object-string-string)
Expand Down Expand Up @@ -316,6 +317,11 @@ For example, the following would create a global `__DEV__` variable set to `true

Note that, if you specify a global reference value (like an object or array) here, and some code mutates that value in the midst of running a test, that mutation will *not* be persisted across test runs for other test files.

### `config.mocksPattern` [string]
(default: `(?:[\\/]|^)__mocks__[\\/]`)

A pattern that is matched against file paths to determine which folder contains manual mocks.

### `config.moduleFileExtensions` [array<string>]
(default: `['js', 'json', 'node']`)

Expand Down
3 changes: 2 additions & 1 deletion src/HasteModuleLoader/HasteModuleLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ class Loader {
this._resolver = HasteResolver.get(
[config.rootDir],
{
extensions: this._extensions.concat(this._config.testFileExtensions),
extensions: this._extensions.concat(config.testFileExtensions),
ignoreFilePattern: [config.cacheDirectory]
.concat(config.modulePathIgnorePatterns).join('|'),
mocksPattern: config.mocksPattern,
}
);
this._resolvedModules = Object.create(null);
Expand Down
2 changes: 2 additions & 0 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const DEFAULT_CONFIG_VALUES = {
modulePathIgnorePatterns: [],
moduleNameMapper: [],
testDirectoryName: '__tests__',
mocksPattern: '(?:[\\/]|^)__mocks__[\\/]',
testEnvironment: require.resolve('../environments/JSDOMEnvironment'),
testEnvData: {},
testFileExtensions: ['js'],
Expand Down Expand Up @@ -242,6 +243,7 @@ function normalizeConfig(config) {
case 'collectCoverage':
case 'coverageCollector':
case 'globals':
case 'mocksPattern':
case 'moduleLoader':
case 'name':
case 'persistModuleRegistryBetweenSpecs':
Expand Down
6 changes: 3 additions & 3 deletions src/resolvers/HasteResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
const DependencyGraph = require('node-haste/lib/DependencyGraph');
const extractRequires = require('node-haste/lib/lib/extractRequires');

const MOCKS_PATTERN = /(?:[\\/]|^)__mocks__[\\/]([^\/]+)\.js$/;
const REQUIRE_EXTENSIONS_PATTERN = /(\brequire\s*?\.\s*?(?:requireActual|requireMock)\s*?\(\s*?)(['"])([^'"]+)(\2\s*?\))/g;

const resolvers = Object.create(null);
Expand All @@ -37,7 +36,7 @@ class HasteResolver {
isWatchman: () => Promise.resolve(false),
},
extensions: options.extensions,
mocksPattern: MOCKS_PATTERN,
mocksPattern: new RegExp(options.mocksPattern),
extractRequires: code => {
const data = extractRequires(code);
data.code = data.code.replace(
Expand Down Expand Up @@ -93,7 +92,8 @@ class HasteResolver {
const key =
roots.sort().join(':') +
'$' + options.extensions.sort().join(':') +
'$' + options.ignoreFilePattern;
'$' + options.ignoreFilePattern +
'$' + options.mocksPattern;
if (!resolvers[key]) {
resolvers[key] = new HasteResolver(roots, options);
}
Expand Down

0 comments on commit 40f1e1e

Please sign in to comment.