Skip to content

Commit

Permalink
Allow files called just test.jsx? or spec.jsx? in root (jestjs#2211)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored and cpojer committed Dec 13, 2016
1 parent 3882f2f commit b68c615
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 3 deletions.
5 changes: 3 additions & 2 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,12 @@ An array of regexp pattern strings that are matched against all test paths befor
These pattern strings match against the full path. Use the `<rootDir>` string token to include the path to your project's root directory to prevent it from accidentally ignoring all of your files in different environments that may have different root directories. Example: `['<rootDir>/build/', '<rootDir>/node_modules/']`.

### `testRegex` [string]
(default: `(/__tests__/.*|\\.(test|spec))\\.jsx?$`)
(default: `(/__tests__/.*|(\\.|/)(test|spec))\\.jsx?$`)

The pattern Jest uses to detect test files. By default it looks for `.js` and `.jsx` files
inside of `__tests__` folders, as well as any files with a suffix of `.test` or `.spec`
(e.g. `Component.test.js` or `Component.spec.js`).
(e.g. `Component.test.js` or `Component.spec.js`). It will also find files called `test.js`
or `spec.js`.

### `testResultsProcessor` [string]
(default: `undefined`)
Expand Down
29 changes: 29 additions & 0 deletions integration_tests/__tests__/test-in-root-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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.
*
* @emails oncall+jsinfra
*/
'use strict';

const path = require('path');
const runJest = require('../runJest');

it('runs tests in only test.js and spec.js', () => {
const result = runJest.json('test-in-root').json;

expect(result.success).toBe(true);
expect(result.numTotalTests).toBe(2);

const testNames = result.testResults
.map(res => res.name)
.map(name => path.basename(name))
.sort();

expect(testNames.length).toBe(2);
expect(testNames[0]).toBe('spec.js');
expect(testNames[1]).toBe('test.js');
});
11 changes: 11 additions & 0 deletions integration_tests/test-in-root/foo.js
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';

// This test shouldn't run
test('stub', () => expect(1).toBe(2));
11 changes: 11 additions & 0 deletions integration_tests/test-in-root/footest.js
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';

// This test shouldn't run
test('stub', () => expect(1).toBe(2));
5 changes: 5 additions & 0 deletions integration_tests/test-in-root/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
11 changes: 11 additions & 0 deletions integration_tests/test-in-root/spec.js
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';

// This test should run
test('stub', () => expect(1).toBe(1));
11 changes: 11 additions & 0 deletions integration_tests/test-in-root/test.js
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';

// This test should run
test('stub', () => expect(1).toBe(1));
2 changes: 1 addition & 1 deletion packages/jest-config/src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module.exports = ({
testEnvironment: 'jest-environment-jsdom',
testPathDirs: ['<rootDir>'],
testPathIgnorePatterns: [NODE_MODULES_REGEXP],
testRegex: '(/__tests__/.*|\\.(test|spec))\\.jsx?$',
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.jsx?$',
testURL: 'about:blank',
timers: 'real',
transformIgnorePatterns: [NODE_MODULES_REGEXP],
Expand Down

0 comments on commit b68c615

Please sign in to comment.