Skip to content

Commit d23e728

Browse files
LinusBorghaoqunjiang
authored andcommitted
docs: Document jest's transformIgnorePatterns option to avoid pitfall (#3523) [ci skip]
1 parent b2d6dbd commit d23e728

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

docs/config/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ Deprecated since Vue CLI 3.3, please use [`publicPath`](#publicPath) instead.
188188

189189
By default `babel-loader` ignores all files inside `node_modules`. If you want to explicitly transpile a dependency with Babel, you can list it in this option.
190190

191+
::: warning Jest config
192+
This option is not respected by the [cli-unit-jest plugin](#jest), because in jest, we don't have to transpile code from `/node_modules` unless it uses non-standard features - Node >8.11 supports the latest ECMAScript features already.
193+
194+
However, jest sometimes has to transform content from node_modules if that code uses ES6 `import`/`export` syntax. In that case, use the `tranformIgnorePatterns` option in `jest.config.js`.
195+
196+
See [the plugin's README](https://github.com/vuejs/vue-cli/blob/dev/packages/%40vue/cli-plugin-unit-jest/README.md) for more information.
197+
:::
198+
191199
### productionSourceMap
192200

193201
- Type: `boolean`

packages/@vue/cli-plugin-unit-jest/README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Note that directly running `jest` will fail because the Babel preset requires hi
2121

2222
If you want to debug your tests via the Node inspector, you can run the following:
2323

24-
``` sh
24+
```sh
2525
# macOS or linux
2626
node --inspect-brk ./node_modules/.bin/vue-cli-service test:unit
2727

@@ -35,6 +35,36 @@ Jest can be configured via `jest.config.js` in your project root, or the `jest`
3535

3636
## Installing in an Already Created Project
3737

38-
``` sh
38+
```sh
3939
vue add @vue/unit-jest
4040
```
41+
42+
## Transform dependencies from `/node_modules`
43+
44+
By default, jest doesn't transform anything from `/nodee_modules`.
45+
46+
Since jest runs in node, we also don't have to transpile anything that uses modern ECMAScript features as Node >=8 already supports these features, so it's a sensible default. cli-plugin-jest also doesn't respect the `transpileDependencies` option in `vue.config.js` for the same reason.
47+
48+
However, we have (at least) three cases where we do need to transpile code from `/node_modules` in jest:
49+
50+
1. Usage of ES6 `import`/`export` statements, which have to be compiled to commonjs `module.exports`
51+
2. Single File Components (`.vue` files) which have to be run through `vue-jest`
52+
3. Typescript code
53+
54+
To do this, we need to add an exception to the `tranformIgnorePatterns` option of jest. This is its default value:
55+
56+
```javascript
57+
transformIgnorePatterns: ['/node_modules/']
58+
```
59+
60+
We have to add exceptions to this pattern with a RegExp negative lookahead:
61+
62+
```javascript
63+
transformIgnorePatterns: ['/node_modules/(?!name-of-lib-o-transform)']
64+
```
65+
66+
To exclude multiple libraries:
67+
68+
```javascript
69+
transformIgnorePatterns: ['/node_modules/(?!lib-to-transform|other-lib)']
70+
```

packages/@vue/cli-plugin-unit-jest/generator/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = (api, _, __, invoking) => {
2323
'^.+\\.vue$': 'vue-jest',
2424
'.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub'
2525
},
26+
'transformIgnorePatterns': ['/node_modules/'],
2627
// support the same @ -> src alias mapping in source code
2728
'moduleNameMapper': {
2829
'^@/(.*)$': '<rootDir>/src/$1'

0 commit comments

Comments
 (0)