1 | # vue-jest
|
2 |
|
3 | Jest Vue transformer with source map support
|
4 |
|
5 | > **NOTE:** This is documentation for `vue-jest@3.x`. [View the vue-jest@2.x documentation](https://github.com/vuejs/vue-jest/tree/e694fc7ce11ae1ac1c778ed7c4402515c5f0d5aa)
|
6 |
|
7 | ## Usage
|
8 |
|
9 | ```bash
|
10 | npm install --save-dev vue-jest
|
11 | ```
|
12 |
|
13 | ## Setup
|
14 |
|
15 | To define `vue-jest` as a transformer for your `.vue` files, map them to the `vue-jest` module:
|
16 |
|
17 | ```json
|
18 | {
|
19 | "jest": {
|
20 | "transform": {
|
21 | "^.+\\.vue$": "vue-jest"
|
22 | }
|
23 | }
|
24 | ```
|
25 |
|
26 | A full config will look like this.
|
27 |
|
28 | ```json
|
29 | {
|
30 | "jest": {
|
31 | "moduleFileExtensions": [
|
32 | "js",
|
33 | "json",
|
34 | "vue"
|
35 | ],
|
36 | "transform": {
|
37 | "^.+\\.js$": "babel-jest",
|
38 | "^.+\\.vue$": "vue-jest"
|
39 | }
|
40 | }
|
41 | }
|
42 | ```
|
43 |
|
44 | If you're on a version of Jest older than 22.4.0, you need to set `mapCoverage` to `true` in order to use source maps.
|
45 |
|
46 | ## Example Projects
|
47 |
|
48 | Example repositories testing Vue components with jest and vue-jest:
|
49 |
|
50 | - [Avoriaz with Jest](https://github.com/eddyerburgh/avoriaz-jest-example)
|
51 | - [Vue Test Utils with Jest](https://github.com/eddyerburgh/vue-test-utils-jest-example)
|
52 |
|
53 | ## Supported langs
|
54 |
|
55 | vue-jest compiles the script and template of SFCs into a JavaScript file that Jest can run. **Currently, SCSS, SASS and Stylus are the only style languages that are compiled**.
|
56 |
|
57 | ### Supported script languages
|
58 |
|
59 | - **typescript** (`lang="ts"`, `lang="typescript"`)
|
60 | - **coffeescript** (`lang="coffee"`, `lang="coffeescript"`)
|
61 |
|
62 | ### Global Jest options
|
63 |
|
64 | You can change the behavior of `vue-jest` by using `jest.globals`.
|
65 |
|
66 | > *Tip:* Need programmatic configuration? Use the [--config](https://jestjs.io/docs/en/cli.html#config-path) option in Jest CLI, and export a `.js` file
|
67 |
|
68 | #### babelConfig
|
69 |
|
70 | Provide `babelConfig` in one of the following formats:
|
71 |
|
72 | - `<Boolean>`
|
73 | - `<Object>`
|
74 | - `<String>`
|
75 |
|
76 | ##### Boolean
|
77 |
|
78 | - `true` - Enable Babel processing. `vue-jest` will try to find Babel configuration using [find-babel-config](https://www.npmjs.com/package/find-babel-config).
|
79 |
|
80 | > This is the default behavior if [babelConfig](#babelconfig) is not defined.
|
81 |
|
82 | - `false` - Skip Babel processing entirely:
|
83 |
|
84 | ```json
|
85 | {
|
86 | "jest": {
|
87 | "globals": {
|
88 | "vue-jest": {
|
89 | "babelConfig": false
|
90 | }
|
91 | }
|
92 | }
|
93 | }
|
94 | ```
|
95 |
|
96 | ##### Object
|
97 |
|
98 | Provide inline [Babel options](https://babeljs.io/docs/en/options):
|
99 |
|
100 | ```json
|
101 | {
|
102 | "jest": {
|
103 | "globals": {
|
104 | "vue-jest": {
|
105 | "babelConfig": {
|
106 | "presets": [
|
107 | [
|
108 | "env",
|
109 | {
|
110 | "useBuiltIns": "entry",
|
111 | "shippedProposals": true
|
112 | }
|
113 | ]
|
114 | ],
|
115 | "plugins": [
|
116 | "syntax-dynamic-import"
|
117 | ],
|
118 | "env": {
|
119 | "test": {
|
120 | "plugins": [
|
121 | "dynamic-import-node"
|
122 | ]
|
123 | }
|
124 | }
|
125 | }
|
126 | }
|
127 | }
|
128 | }
|
129 | }
|
130 | ```
|
131 |
|
132 | ##### String
|
133 |
|
134 | If a string is provided, it will be an assumed path to a babel configuration file (e.g. `.babelrc`, `.babelrc.js`).
|
135 | - Config file should export a Babel configuration object.
|
136 | - Should *not* point to a [project-wide configuration file (babel.config.js)](https://babeljs.io/docs/en/config-files#project-wide-configuration), which exports a function.
|
137 |
|
138 | ```json
|
139 | {
|
140 | "jest": {
|
141 | "globals": {
|
142 | "vue-jest": {
|
143 | "babelConfig": "path/to/.babelrc.js"
|
144 | }
|
145 | }
|
146 | }
|
147 | }
|
148 | ```
|
149 |
|
150 | To use the [Config Function API](https://babeljs.io/docs/en/config-files#config-function-api), use inline options instead. i.e.:
|
151 |
|
152 | ```json
|
153 | {
|
154 | "jest": {
|
155 | "globals": {
|
156 | "vue-jest": {
|
157 | "babelConfig": {
|
158 | "configFile": "path/to/babel.config.js"
|
159 | }
|
160 | }
|
161 | }
|
162 | }
|
163 | }
|
164 | ```
|
165 |
|
166 | #### tsConfig
|
167 |
|
168 | Provide `tsConfig` in one of the following formats:
|
169 |
|
170 | - `<Boolean>`
|
171 | - `<Object>`
|
172 | - `<String>`
|
173 |
|
174 | ##### Boolean
|
175 |
|
176 | - `true` - Process TypeScript files using custom configuration. `vue-jest` will try to find TypeScript configuration using [tsconfig.loadSync](https://www.npmjs.com/package/tsconfig#api).
|
177 |
|
178 | > This is the default behavior if [tsConfig](#tsConfig) is not defined.
|
179 |
|
180 | - `false` - Process TypeScript files using the [default configuration provided by vue-jest](https://github.com/vuejs/vue-jest/blob/master/lib/load-typescript-config.js#L5-L27).
|
181 |
|
182 | ##### Object
|
183 |
|
184 | Provide inline [TypeScript compiler options](https://www.typescriptlang.org/docs/handbook/compiler-options.html):
|
185 |
|
186 | ```json
|
187 | {
|
188 | "jest": {
|
189 | "globals": {
|
190 | "vue-jest": {
|
191 | "tsConfig": {
|
192 | "importHelpers": true
|
193 | }
|
194 | }
|
195 | }
|
196 | }
|
197 | }
|
198 | ```
|
199 |
|
200 | ##### String
|
201 |
|
202 | If a string is provided, it will be an assumed path to a TypeScript configuration file:
|
203 |
|
204 | ```json
|
205 | {
|
206 | "jest": {
|
207 | "globals": {
|
208 | "vue-jest": {
|
209 | "tsConfig": "path/to/tsconfig.json"
|
210 | }
|
211 | }
|
212 | }
|
213 | }
|
214 | ```
|
215 |
|
216 | ### Supported template languages
|
217 |
|
218 | - **pug** (`lang="pug"`)
|
219 | - To give options for the Pug compiler, enter them into the Jest configuration.
|
220 | The options will be passed to pug.compile().
|
221 |
|
222 | ```json
|
223 | {
|
224 | "jest": {
|
225 | "globals": {
|
226 | "vue-jest": {
|
227 | "pug": {
|
228 | "basedir": "mybasedir"
|
229 | }
|
230 | }
|
231 | }
|
232 | }
|
233 | }
|
234 | ```
|
235 |
|
236 | - **jade** (`lang="jade"`)
|
237 | - **haml** (`lang="haml"`)
|
238 |
|
239 | ### Supported style languages
|
240 |
|
241 | - **stylus** (`lang="stylus"`, `lang="styl"`)
|
242 | - **sass** (`lang="sass"`)
|
243 | - The SASS compiler supports jest's [moduleNameMapper](https://facebook.github.io/jest/docs/en/configuration.html#modulenamemapper-object-string-string) which is the suggested way of dealing with Webpack aliases.
|
244 | - **scss** (`lang="scss"`)
|
245 | - The SCSS compiler supports jest's [moduleNameMapper](https://facebook.github.io/jest/docs/en/configuration.html#modulenamemapper-object-string-string) which is the suggested way of dealing with Webpack aliases.
|
246 | - To import globally included files (ie. variables, mixins, etc.), include them in the Jest configuration at `jest.globals['vue-jest'].resources.scss`:
|
247 |
|
248 | ```json
|
249 | {
|
250 | "jest": {
|
251 | "globals": {
|
252 | "vue-jest": {
|
253 | "resources": {
|
254 | "scss": [
|
255 | "./node_modules/package/_mixins.scss",
|
256 | "./src/assets/css/globals.scss"
|
257 | ]
|
258 | }
|
259 | }
|
260 | }
|
261 | }
|
262 | }
|
263 | ```
|
264 |
|
265 | ## CSS options
|
266 |
|
267 | `experimentalCSSCompile`: `Boolean` Default true. Turn off CSS compilation
|
268 | `hideStyleWarn`: `Boolean` Default false. Hide warnings about CSS compilation
|
269 | `resources`:
|
270 |
|
271 | ```json
|
272 | {
|
273 | "jest": {
|
274 | "globals": {
|
275 | "vue-jest": {
|
276 | "hideStyleWarn": true,
|
277 | "experimentalCSSCompile": true
|
278 | }
|
279 | }
|
280 | }
|
281 | }
|
282 | ```
|
283 |
|
\ | No newline at end of file |