-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
docs: Fix broken transform link on webpack page #9155
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
--- | ||
id: version-24.9-webpack | ||
title: Using with webpack | ||
original_id: webpack | ||
--- | ||
|
||
Jest can be used in projects that use [webpack](https://webpack.js.org/) to manage assets, styles, and compilation. webpack _does_ offer some unique challenges over other tools because it integrates directly with your application to allow managing stylesheets, assets like images and fonts, along with the expansive ecosystem of compile-to-JavaScript languages and tools. | ||
|
||
## A webpack example | ||
|
||
Let's start with a common sort of webpack config file and translate it to a Jest setup. | ||
|
||
```js | ||
// webpack.config.js | ||
module.exports = { | ||
module: { | ||
loaders: [ | ||
{exclude: ['node_modules'], loader: 'babel', test: /\.jsx?$/}, | ||
{loader: 'style-loader!css-loader', test: /\.css$/}, | ||
{loader: 'url-loader', test: /\.gif$/}, | ||
{loader: 'file-loader', test: /\.(ttf|eot|svg)$/}, | ||
], | ||
}, | ||
resolve: { | ||
alias: { | ||
config$: './configs/app-config.js', | ||
react: './vendor/react-master', | ||
}, | ||
extensions: ['', 'js', 'jsx'], | ||
modules: [ | ||
'node_modules', | ||
'bower_components', | ||
'shared', | ||
'/shared/vendor/modules', | ||
], | ||
}, | ||
}; | ||
``` | ||
|
||
If you have JavaScript files that are transformed by Babel, you can [enable support for Babel](GettingStarted.md#using-babel) by installing the `babel-jest` plugin. Non-Babel JavaScript transformations can be handled with Jest's [`transform`](Configuration.md#transform-object-string-pathtotransformer-pathtotransformer-object) config option. | ||
|
||
### Handling Static Assets | ||
|
||
Next, let's configure Jest to gracefully handle asset files such as stylesheets and images. Usually, these files aren't particularly useful in tests so we can safely mock them out. However, if you are using CSS Modules then it's better to mock a proxy for your className lookups. | ||
|
||
```json | ||
// package.json | ||
{ | ||
"jest": { | ||
"moduleNameMapper": { | ||
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js", | ||
"\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
And the mock files themselves: | ||
|
||
```js | ||
// __mocks__/styleMock.js | ||
|
||
module.exports = {}; | ||
``` | ||
|
||
```js | ||
// __mocks__/fileMock.js | ||
|
||
module.exports = 'test-file-stub'; | ||
``` | ||
|
||
### Mocking CSS Modules | ||
|
||
You can use an [ES6 Proxy](https://github.com/keyanzhang/identity-obj-proxy) to mock [CSS Modules](https://github.com/css-modules/css-modules): | ||
|
||
```bash | ||
yarn add --dev identity-obj-proxy | ||
``` | ||
|
||
Then all your className lookups on the styles object will be returned as-is (e.g., `styles.foobar === 'foobar'`). This is pretty handy for React [Snapshot Testing](SnapshotTesting.md). | ||
|
||
```json | ||
// package.json (for CSS Modules) | ||
{ | ||
"jest": { | ||
"moduleNameMapper": { | ||
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js", | ||
"\\.(css|less)$": "identity-obj-proxy" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
> Notice that Proxy is enabled in Node 6 by default. If you are not on Node 6 yet, make sure you invoke Jest using `node --harmony_proxies node_modules/.bin/jest`. | ||
|
||
If `moduleNameMapper` cannot fulfill your requirements, you can use Jest's [`transform`](Configuration.md#transform-object-string-pathtotransformer-pathtotransformer-object) config option to specify how assets are transformed. For example, a transformer that returns the basename of a file (such that `require('logo.jpg');` returns `'logo'`) can be written as: | ||
|
||
```js | ||
// fileTransformer.js | ||
const path = require('path'); | ||
|
||
module.exports = { | ||
process(src, filename, config, options) { | ||
return 'module.exports = ' + JSON.stringify(path.basename(filename)) + ';'; | ||
}, | ||
}; | ||
``` | ||
|
||
```json | ||
// package.json (for custom transformers and CSS Modules) | ||
{ | ||
"jest": { | ||
"moduleNameMapper": { | ||
"\\.(css|less)$": "identity-obj-proxy" | ||
}, | ||
"transform": { | ||
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/fileTransformer.js" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
We've told Jest to ignore files matching a stylesheet or image extension, and instead, require our mock files. You can adjust the regular expression to match the file types your webpack config handles. | ||
|
||
_Note: if you are using babel-jest with additional code preprocessors, you have to explicitly define babel-jest as a transformer for your JavaScript code to map `.js` files to the babel-jest module._ | ||
|
||
```json | ||
"transform": { | ||
"^.+\\.js$": "babel-jest", | ||
"^.+\\.css$": "custom-transformer", | ||
... | ||
} | ||
``` | ||
|
||
### Configuring Jest to find our files | ||
|
||
Now that Jest knows how to process our files, we need to tell it how to _find_ them. For webpack's `modulesDirectories`, and `extensions` options there are direct analogs in Jest's `moduleDirectories` and `moduleFileExtensions` options. | ||
|
||
```json | ||
// package.json | ||
{ | ||
"jest": { | ||
"moduleFileExtensions": ["js", "jsx"], | ||
"moduleDirectories": ["node_modules", "bower_components", "shared"], | ||
|
||
"moduleNameMapper": { | ||
"\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js", | ||
"\\.(gif|ttf|eot|svg)$": "<rootDir>/__mocks__/fileMock.js" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
> Note: `<rootDir>` is a special token that gets replaced by Jest with the root of your project. Most of the time this will be the folder where your `package.json` is located unless you specify a custom `rootDir` option in your configuration. | ||
|
||
Similarly webpack's `resolve.root` option functions like setting the `NODE_PATH` env variable, which you can set, or make use of the `modulePaths` option. | ||
|
||
```json | ||
// package.json | ||
{ | ||
"jest": { | ||
"modulePaths": ["/shared/vendor/modules"], | ||
"moduleFileExtensions": ["js", "jsx"], | ||
"moduleDirectories": ["node_modules", "bower_components", "shared"], | ||
"moduleNameMapper": { | ||
"\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js", | ||
"\\.(gif|ttf|eot|svg)$": "<rootDir>/__mocks__/fileMock.js" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
And finally, we have to handle the webpack `alias`. For that we can make use of the `moduleNameMapper` option again. | ||
|
||
```json | ||
// package.json | ||
{ | ||
"jest": { | ||
"modulePaths": ["/shared/vendor/modules"], | ||
"moduleFileExtensions": ["js", "jsx"], | ||
"moduleDirectories": ["node_modules", "bower_components", "shared"], | ||
|
||
"moduleNameMapper": { | ||
"\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js", | ||
"\\.(gif|ttf|eot|svg)$": "<rootDir>/__mocks__/fileMock.js", | ||
|
||
"^react(.*)$": "<rootDir>/vendor/react-master$1", | ||
"^config$": "<rootDir>/configs/app-config.js" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
That's it! webpack is a complex and flexible tool, so you may have to make some adjustments to handle your specific application's needs. Luckily for most projects, Jest should be more than flexible enough to handle your webpack config. | ||
|
||
> Note: For more complex webpack configurations, you may also want to investigate projects such as: [babel-plugin-webpack-loaders](https://github.com/istarkov/babel-plugin-webpack-loaders). | ||
|
||
## Using with webpack 2 | ||
|
||
webpack 2 offers native support for ES modules. However, Jest runs in Node, and thus requires ES modules to be transpiled to CommonJS modules. As such, if you are using webpack 2, you most likely will want to configure Babel to transpile ES modules to CommonJS modules only in the `test` environment. | ||
|
||
```json | ||
// .babelrc | ||
{ | ||
"presets": [["env", {"modules": false}]], | ||
|
||
"env": { | ||
"test": { | ||
"plugins": ["transform-es2015-modules-commonjs"] | ||
} | ||
} | ||
} | ||
``` | ||
|
||
> Note: Jest caches files to speed up test execution. If you updated .babelrc and Jest is still not working, try running Jest with `--no-cache`. | ||
|
||
If you use dynamic imports (`import('some-file.js').then(module => ...)`), you need to enable the `dynamic-import-node` plugin. | ||
|
||
```json | ||
// .babelrc | ||
{ | ||
"presets": [["env", {"modules": false}]], | ||
|
||
"plugins": ["syntax-dynamic-import"], | ||
|
||
"env": { | ||
"test": { | ||
"plugins": ["dynamic-import-node"] | ||
} | ||
} | ||
} | ||
``` | ||
|
||
For an example of how to use Jest with Webpack with React, Redux, and Node, you can view one [here](https://github.com/jenniferabowd/jest_react_redux_node_webpack_complex_example). |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need this file as it should use the one in
docs
.However, can you verify the other versioned docs are linked correctly? https://jestjs.io/en/versions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I check other versioned docs (Past Versions) are linked correctly. (
/configuration#transform-object-string-string
)And, I checked that link is broken in Current(24.9) and Latest version.
Because Configuration page's transform fragment was changed since #7288
(
/configuration#transform-object-string-pathtotransformer-pathtotransformer-object
)In development, I added this file to fix broken link in Current version(24.9).
Did I miss something or misunderstand in development and your feedback?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it seems you've done it entirely correct. Thank you!