Skip to content

Commit

Permalink
fix: simplify transform RegExp (#10207)
Browse files Browse the repository at this point in the history
  • Loading branch information
elado committed Oct 19, 2020
1 parent ecb31a7 commit 7d7e97f
Show file tree
Hide file tree
Showing 32 changed files with 64 additions and 68 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

### Fixes

- `[jest-config]` Simplify transform RegExp ([#10207](https://github.com/facebook/jest/pull/10207))
- `[jest-fake-timers]` Lazily instantiate mock timers ([#10551](https://github.com/facebook/jest/pull/10551))
- `[jest-runtime]` `require.main` is no longer `undefined` when using `jest.resetModules` ([#10626](https://github.com/facebook/jest/pull/10626))
- `[@jest/types]` Add missing values for `timers` ([#10632](https://github.com/facebook/jest/pull/10632))
Expand Down
4 changes: 2 additions & 2 deletions docs/Configuration.md
Expand Up @@ -1223,7 +1223,7 @@ If the value is `modern`, [`@sinonjs/fake-timers`](https://github.com/sinonjs/fa

### `transform` [object\<string, pathToTransformer | [pathToTransformer, object]>]

Default: `{"^.+\\.[jt]sx?$": "babel-jest"}`
Default: `{"\\.[jt]sx?$": "babel-jest"}`

A map from regular expressions to paths to transformers. A transformer is a module that provides a synchronous function for transforming source files. For example, if you wanted to be able to use a new language feature in your modules or tests that aren't yet supported by node, you might plug in one of many compilers that compile a future version of JavaScript to a current one. Example: see the [examples/typescript](https://github.com/facebook/jest/blob/master/examples/typescript/package.json#L16) example or the [webpack tutorial](Webpack.md).

Expand All @@ -1238,7 +1238,7 @@ You can pass configuration to a transformer like `{filePattern: ['path-to-transf

_Note: a transformer is only run once per file unless the file has changed. During the development of a transformer it can be useful to run Jest with `--no-cache` to frequently [delete Jest's cache](Troubleshooting.md#caching-issues)._

_Note: when adding additional code transformers, this will overwrite the default config and `babel-jest` is no longer automatically loaded. If you want to use it to compile JavaScript or Typescript, it has to be explicitly defined by adding `{"^.+\\.[jt]sx?$": "babel-jest"}` to the transform property. See [babel-jest plugin](https://github.com/facebook/jest/tree/master/packages/babel-jest#setup)_
_Note: when adding additional code transformers, this will overwrite the default config and `babel-jest` is no longer automatically loaded. If you want to use it to compile JavaScript or Typescript, it has to be explicitly defined by adding `{"\\.[jt]sx?$": "babel-jest"}` to the transform property. See [babel-jest plugin](https://github.com/facebook/jest/tree/master/packages/babel-jest#setup)_

### `transformIgnorePatterns` [array\<string>]

Expand Down
4 changes: 2 additions & 2 deletions docs/Webpack.md
Expand Up @@ -125,8 +125,8 @@ _Note: if you are using babel-jest with additional code preprocessors, you have

```json
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.css$": "custom-transformer",
"\\.js$": "babel-jest",
"\\.css$": "custom-transformer",
...
}
```
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/showConfig.test.ts.snap
Expand Up @@ -67,7 +67,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
"timers": "real",
"transform": [
[
"^.+\\\\.[jt]sx?$",
"\\\\.[jt]sx?$",
"<<REPLACED_JEST_PACKAGES_DIR>>/babel-jest/build/index.js",
{}
]
Expand Down
2 changes: 1 addition & 1 deletion e2e/coverage-remapping/package.json
Expand Up @@ -2,7 +2,7 @@
"jest": {
"rootDir": "./",
"transform": {
"^.+\\.(ts|js)$": "<rootDir>/typescriptPreprocessor.js"
"\\.(ts|js)$": "<rootDir>/typescriptPreprocessor.js"
},
"testEnvironment": "node"
},
Expand Down
2 changes: 1 addition & 1 deletion e2e/coverage-transform-instrumented/package.json
Expand Up @@ -2,7 +2,7 @@
"jest": {
"rootDir": "./",
"transform": {
"^.+\\.(js)$": "<rootDir>/preprocessor.js"
"\\.(js)$": "<rootDir>/preprocessor.js"
},
"testRegex": "/__tests__/.*\\.(js)$",
"testEnvironment": "node",
Expand Down
2 changes: 1 addition & 1 deletion e2e/global-setup-custom-transform/package.json
Expand Up @@ -3,7 +3,7 @@
"testEnvironment": "node",
"globalSetup": "<rootDir>/setup.js",
"transform": {
"^.+\\.js$": "<rootDir>/transformer.js"
"\\.js$": "<rootDir>/transformer.js"
}
}
}
2 changes: 1 addition & 1 deletion e2e/node-path/package.json
Expand Up @@ -2,7 +2,7 @@
"jest": {
"testEnvironment": "node",
"transform": {
"^.+\\.jsx?$": "../../packages/babel-jest"
"\\.jsx?$": "../../packages/babel-jest"
}
}
}
2 changes: 1 addition & 1 deletion e2e/package.json
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.js$": "<rootDir>/../packages/babel-jest"
"\\.js$": "<rootDir>/../packages/babel-jest"
},
"testEnvironment": "node",
"testPathIgnorePatterns": [
Expand Down
2 changes: 1 addition & 1 deletion e2e/snapshot-serializers/package.json
Expand Up @@ -2,7 +2,7 @@
"jest": {
"testEnvironment": "node",
"transform": {
"^.+\\.js$": "<rootDir>/transformer.js"
"\\.js$": "<rootDir>/transformer.js"
},
"snapshotSerializers": [
"./plugins/foo",
Expand Down
2 changes: 1 addition & 1 deletion e2e/stack-trace-source-maps-with-coverage/package.json
Expand Up @@ -2,7 +2,7 @@
"jest": {
"rootDir": "./",
"transform": {
"^.+\\.(ts)$": "<rootDir>/preprocessor.js"
"\\.(ts)$": "<rootDir>/preprocessor.js"
},
"testEnvironment": "node",
"testRegex": "fails"
Expand Down
2 changes: 1 addition & 1 deletion e2e/stack-trace-source-maps/package.json
Expand Up @@ -2,7 +2,7 @@
"jest": {
"rootDir": "./",
"transform": {
"^.+\\.(ts)$": "<rootDir>/preprocessor.js"
"\\.(ts)$": "<rootDir>/preprocessor.js"
},
"testEnvironment": "node",
"testRegex": "fails"
Expand Down
2 changes: 1 addition & 1 deletion e2e/transform-linked-modules/package.json
Expand Up @@ -7,7 +7,7 @@
"<rootDir>/ignored/"
],
"transform": {
"^.+\\.js$": "<rootDir>/preprocessor.js"
"\\.js$": "<rootDir>/preprocessor.js"
}
}
}
2 changes: 1 addition & 1 deletion e2e/transform/cache/package.json
Expand Up @@ -4,7 +4,7 @@
"jest": {
"testEnvironment": "node",
"transform": {
"^.+\\.js$": "<rootDir>/transformer.js"
"\\.js$": "<rootDir>/transformer.js"
}
}
}
@@ -1,7 +1,7 @@
{
"jest": {
"transform": {
"^.+\\.js$": "<rootDir>/preprocessor.js"
"\\.js$": "<rootDir>/preprocessor.js"
},
"testEnvironment": "node"
}
Expand Down
2 changes: 1 addition & 1 deletion e2e/transform/ecmascript-modules-support/package.json
Expand Up @@ -12,7 +12,7 @@
"**/__tests__/**/*.mjs"
],
"transform": {
"^.+\\.mjs?$": "../../../packages/babel-jest"
"\\.mjs?$": "../../../packages/babel-jest"
}
}
}
6 changes: 3 additions & 3 deletions e2e/transform/multiple-transformers/package.json
@@ -1,9 +1,9 @@
{
"jest": {
"transform": {
"^.+\\.css$": "<rootDir>/cssPreprocessor.js",
"^.+\\.js$": "<rootDir>/jsPreprocessor.js",
"^.+\\.svg$": "<rootDir>/filePreprocessor.js"
"\\.css$": "<rootDir>/cssPreprocessor.js",
"\\.js$": "<rootDir>/jsPreprocessor.js",
"\\.svg$": "<rootDir>/filePreprocessor.js"
},
"testEnvironment": "node"
},
Expand Down
2 changes: 1 addition & 1 deletion e2e/typescript-coverage/package.json
Expand Up @@ -2,7 +2,7 @@
"jest": {
"rootDir": "./",
"transform": {
"^.+\\.(ts|js)$": "<rootDir>/typescriptPreprocessor.js"
"\\.(ts|js)$": "<rootDir>/typescriptPreprocessor.js"
},
"testEnvironment": "node"
},
Expand Down
4 changes: 2 additions & 2 deletions e2e/v8-coverage/no-sourcemap/package.json
Expand Up @@ -4,8 +4,8 @@
"jest": {
"testEnvironment": "node",
"transform": {
"^.+\\.[jt]sx?$": "babel-jest",
"^.+\\.css$": "<rootDir>/cssTransform.js"
"\\.[jt]sx?$": "babel-jest",
"\\.css$": "<rootDir>/cssTransform.js"
}
}
}
5 changes: 1 addition & 4 deletions examples/angular/jest.config.js
Expand Up @@ -2,9 +2,6 @@ module.exports = {
moduleFileExtensions: ['ts', 'html', 'js', 'json'],
setupFilesAfterEnv: ['<rootDir>/setupJest.js'],
transform: {
'^.+\\.[t|j]s$': [
'babel-jest',
{configFile: require.resolve('./.babelrc')},
],
'\\.[tj]s$': ['babel-jest', {configFile: require.resolve('./.babelrc')}],
},
};
2 changes: 1 addition & 1 deletion examples/react-native/jest.config.js
@@ -1,6 +1,6 @@
module.exports = {
preset: 'react-native',
transform: {
'^.+\\.(js|ts|tsx)$': require.resolve('react-native/jest/preprocessor.js'),
'\\.(js|ts|tsx)$': require.resolve('react-native/jest/preprocessor.js'),
},
};
2 changes: 1 addition & 1 deletion jest.config.js
Expand Up @@ -63,7 +63,7 @@ module.exports = {
'/e2e/__tests__/iterator-to-null-test.ts',
],
transform: {
'^.+\\.[jt]sx?$': '<rootDir>/packages/babel-jest',
'\\.[jt]sx?$': '<rootDir>/packages/babel-jest',
},
watchPathIgnorePatterns: ['coverage'],
watchPlugins: [
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-jest/README.md
Expand Up @@ -20,6 +20,6 @@ To explicitly define `babel-jest` as a transformer for your JavaScript code, map

```json
"transform": {
"^.+\\.[t|j]sx?$": "babel-jest"
"\\.[jt]sx?$": "babel-jest"
},
```
2 changes: 1 addition & 1 deletion packages/jest-config/src/ValidConfig.ts
Expand Up @@ -119,7 +119,7 @@ const initialOptions: Config.InitialOptions = {
testURL: 'http://localhost',
timers: 'real',
transform: {
'^.+\\.js$': '<rootDir>/preprocessor.js',
'\\.js$': '<rootDir>/preprocessor.js',
},
transformIgnorePatterns: [NODE_MODULES_REGEXP],
unmockedModulePathPatterns: ['mock'],
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-config/src/__tests__/normalize.test.js
Expand Up @@ -14,7 +14,7 @@ import Defaults from '../Defaults';

import {DEFAULT_JS_PATTERN} from '../constants';

const DEFAULT_CSS_PATTERN = '^.+\\.(css)$';
const DEFAULT_CSS_PATTERN = '\\.(css)$';

jest
.mock('jest-resolve')
Expand Down Expand Up @@ -779,7 +779,7 @@ describe('babel-jest', () => {
});

it('uses babel-jest if babel-jest is explicitly specified in a custom transform options', () => {
const customJSPattern = '^.+\\.js$';
const customJSPattern = '\\.js$';
const {options} = normalize(
{
rootDir: '/root',
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-config/src/constants.ts
Expand Up @@ -8,7 +8,7 @@
import * as path from 'path';

export const NODE_MODULES = path.sep + 'node_modules' + path.sep;
export const DEFAULT_JS_PATTERN = '^.+\\.[jt]sx?$';
export const DEFAULT_JS_PATTERN = '\\.[jt]sx?$';
export const DEFAULT_REPORTER_LABEL = 'default';
export const PACKAGE_JSON = 'package.json';
export const JEST_CONFIG_BASE_NAME = 'jest.config';
Expand Down
Expand Up @@ -51,7 +51,7 @@ describe('generateEmptyCoverage', () => {
cacheDirectory: os.tmpdir(),
cwd: rootDir,
rootDir,
transform: [['^.+\\.js$', require.resolve('babel-jest')]],
transform: [['\\.js$', require.resolve('babel-jest')]],
}),
);

Expand Down Expand Up @@ -95,7 +95,7 @@ describe('generateEmptyCoverage', () => {
cacheDirectory: os.tmpdir(),
cwd: rootDir,
rootDir,
transform: [['^.+\\.js$', require.resolve('babel-jest')]],
transform: [['\\.js$', require.resolve('babel-jest')]],
}),
);

Expand Down Expand Up @@ -124,7 +124,7 @@ describe('generateEmptyCoverage', () => {
cacheDirectory: os.tmpdir(),
cwd: rootDir,
rootDir,
transform: [['^.+\\.js$', require.resolve('babel-jest')]],
transform: [['\\.js$', require.resolve('babel-jest')]],
}),
);

Expand Down
Expand Up @@ -22,7 +22,7 @@ describe('Runtime', () => {
describe('internalModule', () => {
it('loads modules and applies transforms', () =>
createRuntime(__filename, {
transform: {'^.+\\.js$': './test_preprocessor'},
transform: {'\\.js$': './test_preprocessor'},
}).then(runtime => {
const modulePath = path.resolve(
path.dirname(runtime.__mockRootPath),
Expand All @@ -35,7 +35,7 @@ describe('Runtime', () => {

it('loads internal modules without applying transforms', () =>
createRuntime(__filename, {
transform: {'^.+\\.js$': './test_preprocessor'},
transform: {'\\.js$': './test_preprocessor'},
}).then(runtime => {
const modulePath = path.resolve(
path.dirname(runtime.__mockRootPath),
Expand All @@ -47,7 +47,7 @@ describe('Runtime', () => {

it('loads JSON modules and applies transforms', () =>
createRuntime(__filename, {
transform: {'^.+\\.json$': './test_json_preprocessor'},
transform: {'\\.json$': './test_json_preprocessor'},
}).then(runtime => {
const modulePath = path.resolve(
path.dirname(runtime.__mockRootPath),
Expand All @@ -59,7 +59,7 @@ describe('Runtime', () => {

it('loads internal JSON modules without applying transforms', () =>
createRuntime(__filename, {
transform: {'^.+\\.json$': './test_json_preprocessor'},
transform: {'\\.json$': './test_json_preprocessor'},
}).then(runtime => {
const modulePath = path.resolve(
path.dirname(runtime.__mockRootPath),
Expand Down
Expand Up @@ -58,7 +58,7 @@ Object {
"timers": "real",
"transform": Array [
Array [
"^.+\\\\.js$",
"\\\\.js$",
"test_preprocessor",
],
],
Expand Down Expand Up @@ -231,7 +231,7 @@ exports[`ScriptTransformer uses multiple preprocessors 1`] = `
const TRANSFORMED = {
filename: '/fruits/banana.js',
script: 'module.exports = "banana";',
config: '{"automock":false,"cache":true,"cacheDirectory":"/cache/","clearMocks":false,"coveragePathIgnorePatterns":[],"cwd":"/test_root_dir/","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{},"haste":{},"injectGlobals":true,"moduleDirectories":[],"moduleFileExtensions":["js"],"moduleLoader":"/test_module_loader_path","moduleNameMapper":[],"modulePathIgnorePatterns":[],"modulePaths":[],"name":"test","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/","roots":[],"runner":"jest-runner","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"skipNodeResolution":false,"slowTestThreshold":5,"snapshotSerializers":[],"testEnvironment":"node","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":[],"testPathIgnorePatterns":[],"testRegex":["\\\\.test\\\\.js$"],"testRunner":"jest-jasmine2","testURL":"http://localhost","timers":"real","transform":[["^.+\\\\.js$","test_preprocessor"],["^.+\\\\.css$","css-preprocessor"]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]}',
config: '{"automock":false,"cache":true,"cacheDirectory":"/cache/","clearMocks":false,"coveragePathIgnorePatterns":[],"cwd":"/test_root_dir/","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{},"haste":{},"injectGlobals":true,"moduleDirectories":[],"moduleFileExtensions":["js"],"moduleLoader":"/test_module_loader_path","moduleNameMapper":[],"modulePathIgnorePatterns":[],"modulePaths":[],"name":"test","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/","roots":[],"runner":"jest-runner","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"skipNodeResolution":false,"slowTestThreshold":5,"snapshotSerializers":[],"testEnvironment":"node","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":[],"testPathIgnorePatterns":[],"testRegex":["\\\\.test\\\\.js$"],"testRunner":"jest-jasmine2","testURL":"http://localhost","timers":"real","transform":[["\\\\.js$","test_preprocessor"],["\\\\.css$","css-preprocessor"]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]}',
};
`;

Expand All @@ -248,7 +248,7 @@ exports[`ScriptTransformer uses the supplied preprocessor 1`] = `
const TRANSFORMED = {
filename: '/fruits/banana.js',
script: 'module.exports = "banana";',
config: '{"automock":false,"cache":true,"cacheDirectory":"/cache/","clearMocks":false,"coveragePathIgnorePatterns":[],"cwd":"/test_root_dir/","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{},"haste":{},"injectGlobals":true,"moduleDirectories":[],"moduleFileExtensions":["js"],"moduleLoader":"/test_module_loader_path","moduleNameMapper":[],"modulePathIgnorePatterns":[],"modulePaths":[],"name":"test","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/","roots":[],"runner":"jest-runner","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"skipNodeResolution":false,"slowTestThreshold":5,"snapshotSerializers":[],"testEnvironment":"node","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":[],"testPathIgnorePatterns":[],"testRegex":["\\\\.test\\\\.js$"],"testRunner":"jest-jasmine2","testURL":"http://localhost","timers":"real","transform":[["^.+\\\\.js$","test_preprocessor"]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]}',
config: '{"automock":false,"cache":true,"cacheDirectory":"/cache/","clearMocks":false,"coveragePathIgnorePatterns":[],"cwd":"/test_root_dir/","detectLeaks":false,"detectOpenHandles":false,"errorOnDeprecated":false,"extraGlobals":[],"forceCoverageMatch":[],"globals":{},"haste":{},"injectGlobals":true,"moduleDirectories":[],"moduleFileExtensions":["js"],"moduleLoader":"/test_module_loader_path","moduleNameMapper":[],"modulePathIgnorePatterns":[],"modulePaths":[],"name":"test","prettierPath":"prettier","resetMocks":false,"resetModules":false,"restoreMocks":false,"rootDir":"/","roots":[],"runner":"jest-runner","setupFiles":[],"setupFilesAfterEnv":[],"skipFilter":false,"skipNodeResolution":false,"slowTestThreshold":5,"snapshotSerializers":[],"testEnvironment":"node","testEnvironmentOptions":{},"testLocationInResults":false,"testMatch":[],"testPathIgnorePatterns":[],"testRegex":["\\\\.test\\\\.js$"],"testRunner":"jest-jasmine2","testURL":"http://localhost","timers":"real","transform":[["\\\\.js$","test_preprocessor"]],"transformIgnorePatterns":["/node_modules/"],"watchPathIgnorePatterns":[]}',
};
`;
Expand Down

0 comments on commit 7d7e97f

Please sign in to comment.