|
1 |
| -# vue-jest-next |
| 1 | +# vue-jest |
2 | 2 |
|
3 |
| -Jest Vue transformer for Vue 3 with source map support (WIP). |
| 3 | +Jest transformer for Vue single file components |
4 | 4 |
|
5 |
| -Docs for current stable version (targeting Vue 2.x): https://github.com/vuejs/vue-jest/ |
| 5 | +> **NOTE:** This is documentation for `vue-jest@4.x`. [View the vue-jest@3.x documentation](https://github.com/vuejs/vue-jest/tree/v3) |
6 | 6 |
|
7 |
| -## TODO (supported in vue-jest@4.x but not vue-jest@next): |
| 7 | +## Usage |
8 | 8 |
|
9 |
| -- [ ] Support loading styles |
10 |
| -- [ ] Support custom blocks |
| 9 | +```bash |
| 10 | +npm install --save-dev vue-jest |
| 11 | +yarn add vue-jest --dev |
| 12 | +``` |
| 13 | + |
| 14 | +### Usage with Babel 7 |
| 15 | + |
| 16 | +If you use [jest](https://github.com/facebook/jest) > 24.0.0 and [babel-jest](https://github.com/facebook/jest/tree/master/packages/babel-jest) make sure to install babel-core@bridge |
| 17 | + |
| 18 | +```bash |
| 19 | +npm install --save-dev babel-core@bridge |
| 20 | +yarn add babel-core@bridge --dev |
| 21 | +``` |
| 22 | + |
| 23 | +## Setup |
| 24 | + |
| 25 | +To use `vue-jest` as a transformer for your `.vue` files, map them to the `vue-jest` module: |
| 26 | + |
| 27 | +```json |
| 28 | +{ |
| 29 | + "jest": { |
| 30 | + "transform": { |
| 31 | + "^.+\\.vue$": "vue-jest" |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +A full config will look like this. |
| 38 | + |
| 39 | +```json |
| 40 | +{ |
| 41 | + "jest": { |
| 42 | + "moduleFileExtensions": ["js", "json", "vue"], |
| 43 | + "transform": { |
| 44 | + "^.+\\.js$": "babel-jest", |
| 45 | + "^.+\\.vue$": "vue-jest" |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +## Examples |
| 52 | + |
| 53 | +Example repositories testing Vue components with jest and vue-jest: |
| 54 | + |
| 55 | +- [Vue Test Utils with Jest](https://github.com/eddyerburgh/vue-test-utils-jest-example) |
| 56 | + |
| 57 | +## Supported langs |
| 58 | + |
| 59 | +vue-jest compiles `<script />`, `<template />`, and `<style />` blocks with supported `lang` attributes into JavaScript that Jest can run. |
| 60 | + |
| 61 | +### Supported script languages |
| 62 | + |
| 63 | +- **typescript** (`lang="ts"`, `lang="typescript"`) |
| 64 | +- **coffeescript** (`lang="coffee"`, `lang="coffeescript"`) |
| 65 | + |
| 66 | +### Global Jest options |
| 67 | + |
| 68 | +You can change the behavior of `vue-jest` by using `jest.globals`. |
| 69 | + |
| 70 | +#### Supporting custom blocks |
| 71 | + |
| 72 | +A great feature of the Vue SFC compiler is that it can support custom blocks. You might want to use those blocks in your tests. To render out custom blocks for testing purposes, you'll need to write a transformer. Once you have your transformer, you'll add an entry to vue-jest's transform map. This is how [vue-i18n's](https://github.com/kazupon/vue-i18n) `<i18n>` custom blocks are supported in unit tests. |
| 73 | + |
| 74 | +A `package.json` Example |
| 75 | + |
| 76 | +```json |
| 77 | +{ |
| 78 | + "jest": { |
| 79 | + "moduleFileExtensions": ["js", "json", "vue"], |
| 80 | + "transform": { |
| 81 | + "^.+\\.js$": "babel-jest", |
| 82 | + "^.+\\.vue$": "vue-jest" |
| 83 | + }, |
| 84 | + "globals": { |
| 85 | + "vue-jest": { |
| 86 | + "transform": { |
| 87 | + "your-custom-block": "./custom-block-processor.js" |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +> _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 |
| 96 | +
|
| 97 | +A `jest.config.js` Example - If you're using a dedicated configuration file like you can reference and require your processor in the config file instead of using a file reference. |
| 98 | + |
| 99 | +```js |
| 100 | +module.exports = { |
| 101 | + globals: { |
| 102 | + 'vue-jest': { |
| 103 | + transform: { |
| 104 | + 'your-custom-block': require('./custom-block-processor') |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +#### Writing a processor |
| 112 | + |
| 113 | +Processors must return an object with a "process" method, like so... |
| 114 | + |
| 115 | +```js |
| 116 | +module.exports = { |
| 117 | + /** |
| 118 | + * Process the content inside of a custom block and prepare it for execution in a testing environment |
| 119 | + * @param {SFCCustomBlock[]} blocks All of the blocks matching your type, returned from `@vue/component-compiler-utils` |
| 120 | + * @param {string} vueOptionsNamespace The internal namespace for a component's Vue Options in vue-jest |
| 121 | + * @param {string} filename The SFC file being processed |
| 122 | + * @param {Object} config The full Jest config |
| 123 | + * @returns {string} The code to be output after processing all of the blocks matched by this type |
| 124 | + */ |
| 125 | + process({ blocks, vueOptionsNamespace, filename, config }) {} |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +#### templateCompiler |
| 130 | + |
| 131 | +You can provide [TemplateCompileOptions](https://github.com/vuejs/component-compiler-utils#compiletemplatetemplatecompileoptions-templatecompileresults) in `templateCompiler` section like this: |
| 132 | + |
| 133 | +```json |
| 134 | +{ |
| 135 | + "jest": { |
| 136 | + "globals": { |
| 137 | + "vue-jest": { |
| 138 | + "templateCompiler": { |
| 139 | + "transpileOptions": { |
| 140 | + "transforms": { |
| 141 | + "dangerousTaggedTemplateString": true |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +### Supported template languages |
| 152 | + |
| 153 | +- **pug** (`lang="pug"`) |
| 154 | + |
| 155 | + - To give options for the Pug compiler, enter them into the Jest configuration. |
| 156 | + The options will be passed to pug.compile(). |
| 157 | + |
| 158 | + ```json |
| 159 | + { |
| 160 | + "jest": { |
| 161 | + "globals": { |
| 162 | + "vue-jest": { |
| 163 | + "pug": { |
| 164 | + "basedir": "mybasedir" |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + ``` |
| 171 | + |
| 172 | +- **jade** (`lang="jade"`) |
| 173 | +- **haml** (`lang="haml"`) |
| 174 | + |
| 175 | +### Supported style languages |
| 176 | + |
| 177 | +- **stylus** (`lang="stylus"`, `lang="styl"`) |
| 178 | +- **sass** (`lang="sass"`), and |
| 179 | +- **scss** (`lang="scss"`) |
| 180 | + |
| 181 | + - 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. Webpack's `sass-loader` uses a [special syntax](https://github.com/webpack-contrib/sass-loader/blob/v9.0.2/README.md#resolving-import-at-rules) for indicating non-relative imports, so you'll likely need to copy this syntax into your `moduleNameMapper` entries if you make use of it. For aliases of bare imports (imports that require node module resolution), the aliased value must also be prepended with this `~` or `vue-jest`'s custom resolver won't recognize it. |
| 182 | + ```json |
| 183 | + { |
| 184 | + "jest": { |
| 185 | + "moduleNameMapper": { |
| 186 | + "^~foo/(.*)": "<rootDir>/foo/$1", |
| 187 | + // @import '~foo'; -> @import 'path/to/project/foo'; |
| 188 | + "^~bar/(.*)": "~baz/lib/$1" |
| 189 | + // @import '~bar/qux'; -> @import 'path/to/project/node_modules/baz/lib/qux'; |
| 190 | + // Notice how the tilde (~) was needed on the bare import to baz. |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + ``` |
| 195 | + - To import globally included files (ie. variables, mixins, etc.), include them in the Jest configuration at `jest.globals['vue-jest'].resources.scss`: |
| 196 | + |
| 197 | + ```json |
| 198 | + { |
| 199 | + "jest": { |
| 200 | + "globals": { |
| 201 | + "vue-jest": { |
| 202 | + "resources": { |
| 203 | + "scss": [ |
| 204 | + "./node_modules/package/_mixins.scss", |
| 205 | + "./src/assets/css/globals.scss" |
| 206 | + ] |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + ``` |
| 213 | + |
| 214 | +## CSS options |
| 215 | + |
| 216 | +`experimentalCSSCompile`: `Boolean` Default true. Turn off CSS compilation |
| 217 | + |
| 218 | +`hideStyleWarn`: `Boolean` Default false. Hide warnings about CSS compilation |
| 219 | + |
| 220 | +`resources`: |
| 221 | + |
| 222 | +```json |
| 223 | +{ |
| 224 | + "jest": { |
| 225 | + "globals": { |
| 226 | + "vue-jest": { |
| 227 | + "hideStyleWarn": true, |
| 228 | + "experimentalCSSCompile": true |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | +} |
| 233 | +``` |
0 commit comments