Skip to content

Commit e40cc7a

Browse files
committed
Merge branch 'master' into next
2 parents f10563a + 4dd33a2 commit e40cc7a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+5091
-1394
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ pids
2525
*.pid.lock
2626

2727
package-lock.json
28+
/e2e/**/yarn.lock
29+

README.md

Lines changed: 229 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,233 @@
1-
# vue-jest-next
1+
# vue-jest
22

3-
Jest Vue transformer for Vue 3 with source map support (WIP).
3+
Jest transformer for Vue single file components
44

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)
66
7-
## TODO (supported in vue-jest@4.x but not vue-jest@next):
7+
## Usage
88

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+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<template>
2+
<div class="hello">
3+
<h1 :class="headingClasses">{{ msg }}</h1>
4+
</div>
5+
</template>
6+
7+
<style module="css">
8+
.testA {
9+
background-color: red;
10+
}
11+
</style>
12+
<style module>
13+
.testB {
14+
background-color: blue;
15+
}
16+
</style>
17+
<style>
18+
.testC {
19+
background-color: blue;
20+
}
21+
</style>
22+
23+
<script>
24+
export default {
25+
name: 'basic',
26+
computed: {
27+
headingClasses: function headingClasses() {
28+
return {
29+
red: this.isCrazy,
30+
blue: !this.isCrazy,
31+
shadow: this.isCrazy
32+
}
33+
}
34+
},
35+
data: function data() {
36+
return {
37+
msg: 'Welcome to Your Vue.js App',
38+
isCrazy: false
39+
}
40+
},
41+
methods: {
42+
toggleClass: function toggleClass() {
43+
this.isCrazy = !this.isCrazy
44+
}
45+
}
46+
}
47+
</script>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<template>
2+
<div />
3+
</template>
4+
5+
<script lang="coffee">
6+
export default
7+
data: -> {}
8+
</script>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<template>
2+
<div>
3+
{{ exclamationMarks }}
4+
<type-script-child />
5+
</div>
6+
</template>
7+
8+
<script lang="ts">
9+
import TypeScriptChild from './TypeScriptChild.vue'
10+
11+
export default {
12+
computed: {
13+
exclamationMarks(): string {
14+
return 'string'
15+
}
16+
},
17+
components: {
18+
TypeScriptChild
19+
}
20+
}
21+
</script>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<template>
2+
<div>{{ exclamationMarks }}</div>
3+
</template>
4+
5+
<script lang="ts">
6+
export default {
7+
computed: {
8+
exclamationMarks(): string {
9+
return 'string'
10+
}
11+
}
12+
}
13+
</script>

e2e/2.x/babel-in-package/package.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "vue2-babel-in-package",
3+
"version": "1.0.0",
4+
"license": "MIT",
5+
"private": true,
6+
"scripts": {
7+
"test": "jest --no-cache test.js"
8+
},
9+
"dependencies": {
10+
"source-map": "0.5.6",
11+
"vue": "^2.5.21",
12+
"vue-template-compiler": "^2.5.21"
13+
},
14+
"devDependencies": {
15+
"@babel/core": "^7.9.0",
16+
"@babel/preset-env": "^7.9.0",
17+
"@vue/test-utils": "^1.1.0",
18+
"coffeescript": "^2.3.2",
19+
"jest": "26.x",
20+
"ts-jest": "^26.3.0",
21+
"typescript": "^3.2.2",
22+
"vue2-jest": "~26.0.0"
23+
},
24+
"jest": {
25+
"moduleFileExtensions": [
26+
"js",
27+
"json",
28+
"vue"
29+
],
30+
"transform": {
31+
"^.+\\.js$": "babel-jest",
32+
"^.+\\.vue$": "vue2-jest"
33+
}
34+
},
35+
"babel": {
36+
"presets": [
37+
"@babel/env"
38+
]
39+
}
40+
}

e2e/2.x/babel-in-package/test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { mount } from '@vue/test-utils'
2+
import TypeScript from './components/TypeScript.vue'
3+
import Basic from './components/Basic.vue'
4+
import Coffee from './components/Coffee.vue'
5+
6+
test('processes .vue files', () => {
7+
const wrapper = mount(Basic)
8+
wrapper.vm.toggleClass()
9+
})
10+
11+
test('processes .vue file with lang set to coffee', () => {
12+
const wrapper = mount(Coffee)
13+
expect(wrapper.vm).toBeTruthy()
14+
})
15+
16+
test('processes .vue files with lang set to typescript', () => {
17+
const wrapper = mount(TypeScript)
18+
expect(wrapper.vm).toBeTruthy()
19+
})

0 commit comments

Comments
 (0)