Skip to content

Commit 1354116

Browse files
committed
Merge branch 'master' of github.com:vuejs/vue-jest
2 parents 2054c1e + 4012b2c commit 1354116

File tree

25 files changed

+376
-258
lines changed

25 files changed

+376
-258
lines changed

Diff for: README.md

+47-25
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
# vue-jest
22

3-
Jest transformer for Vue single file components
3+
Jest transformer for Vue Single File Components.
44

5-
> **NOTE:** This is documentation for `[email protected]`. [View the [email protected] documentation](https://github.com/vuejs/vue-jest/tree/v3)
5+
## Installation
66

7-
## Usage
7+
Since we need to support a variety of Vue and Jest versions, vue-jest doesn't follow semantic versioning.
88

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
9+
| Vue version | Jest Version | Package |
10+
| ----------- | ----------------- | ------------------- |
11+
| Vue 2 | Jest 26 and below | `vue-jest@4` |
12+
| Vue 3 | Jest 26 and below | `vue-jest@5` |
13+
| Vue 2 | Jest 27 | `@vue/vue2-jest@27` |
14+
| Vue 3 | Jest 27 | `@vue/vue3-jest@27` |
1715

1816
```bash
19-
npm install --save-dev babel-core@bridge
20-
yarn add babel-core@bridge --dev
17+
npm install --save-dev @vue/vue2-jest@27 # (use the appropriate version)
18+
yarn add @vue/vue2-jest@27 --dev
2119
```
2220

2321
## Setup
2422

25-
To use `vue-jest` as a transformer for your `.vue` files, map them to the `vue-jest` module:
23+
To use `vue-jest` as a transformer for your `.vue` files, map them to the appropriate `vue-jest` module:
2624

2725
```json
2826
{
2927
"jest": {
3028
"transform": {
31-
"^.+\\.vue$": "vue-jest"
29+
"^.+\\.vue$": "@vue/vue2-jest" // Update to match your installed version
3230
}
3331
}
3432
}
@@ -42,19 +40,22 @@ A full config will look like this.
4240
"moduleFileExtensions": ["js", "json", "vue"],
4341
"transform": {
4442
"^.+\\.js$": "babel-jest",
45-
"^.+\\.vue$": "vue-jest"
43+
"^.+\\.vue$": "@vue/vue2-jest"
4644
}
4745
}
4846
}
4947
```
5048

51-
## Examples
49+
### Usage with Babel 7
5250

53-
Example repositories testing Vue components with jest and vue-jest:
51+
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
5452

55-
- [Vue Test Utils with Jest](https://github.com/eddyerburgh/vue-test-utils-jest-example)
53+
```bash
54+
npm install --save-dev babel-core@bridge
55+
yarn add babel-core@bridge --dev
56+
```
5657

57-
## Supported langs
58+
## Supported languages for SFC sections
5859

5960
vue-jest compiles `<script />`, `<template />`, and `<style />` blocks with supported `lang` attributes into JavaScript that Jest can run.
6061

@@ -79,10 +80,10 @@ A `package.json` Example
7980
"moduleFileExtensions": ["js", "json", "vue"],
8081
"transform": {
8182
"^.+\\.js$": "babel-jest",
82-
"^.+\\.vue$": "vue-jest"
83+
"^.+\\.vue$": "@vue/vue2-jest"
8384
},
8485
"globals": {
85-
"vue-jest": {
86+
"@vue/vue2-jest": {
8687
"transform": {
8788
"your-custom-block": "./custom-block-processor.js"
8889
}
@@ -99,7 +100,7 @@ A `jest.config.js` Example - If you're using a dedicated configuration file like
99100
```js
100101
module.exports = {
101102
globals: {
102-
'vue-jest': {
103+
'@vue/vue2-jest': {
103104
transform: {
104105
'your-custom-block': require('./custom-block-processor')
105106
}
@@ -134,7 +135,7 @@ You can provide [TemplateCompileOptions](https://github.com/vuejs/component-comp
134135
{
135136
"jest": {
136137
"globals": {
137-
"vue-jest": {
138+
"@vue/vue2-jest": {
138139
"templateCompiler": {
139140
"transpileOptions": {
140141
"transforms": {
@@ -159,7 +160,7 @@ You can provide [TemplateCompileOptions](https://github.com/vuejs/component-comp
159160
{
160161
"jest": {
161162
"globals": {
162-
"vue-jest": {
163+
"@vue/vue2-jest": {
163164
"pug": {
164165
"basedir": "mybasedir"
165166
}
@@ -231,3 +232,24 @@ You can provide [TemplateCompileOptions](https://github.com/vuejs/component-comp
231232
}
232233
}
233234
```
235+
236+
## Style options
237+
238+
Possbility to change style loader options (sass, scss, less etc).
239+
240+
`styleOptions`: `Object` Default `{}`.
241+
242+
```json
243+
{
244+
"jest": {
245+
"globals": {
246+
"vue-jest": {
247+
"styleOptions": {
248+
"quietDeps" // e.q. sass options https://sass-lang.com/documentation/js-api#quietdeps
249+
// unfortunately rest options like `data`, `file` doesnt work because @vue/compiler-component-utils internally overwrite options with their values
250+
},
251+
}
252+
}
253+
}
254+
}
255+
```

Diff for: e2e/2.x/babel-in-package/components/Tsx.vue

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script lang="tsx">
2+
import Vue from 'vue'
3+
4+
export default Vue.extend({
5+
computed: {
6+
exclamationMarks(): string {
7+
return 'tsx components'
8+
}
9+
},
10+
render(): VNode {
11+
return <div>{this.exclamationMarks}</div>
12+
}
13+
})
14+
</script>

Diff for: e2e/2.x/babel-in-package/package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
"devDependencies": {
1515
"@babel/core": "^7.9.0",
1616
"@babel/preset-env": "^7.9.0",
17+
"@vue/babel-preset-jsx": "^1.2.4",
1718
"@vue/test-utils": "^1.1.0",
19+
"@vue/vue2-jest": "~27.0.0-alpha.1",
1820
"coffeescript": "^2.3.2",
1921
"jest": "27.x",
2022
"ts-jest": "^27.0.4",
21-
"typescript": "^3.2.2",
22-
"@vue/vue2-jest": "~27.0.0-alpha.1"
23+
"typescript": "^3.2.2"
2324
},
2425
"jest": {
2526
"testEnvironment": "jsdom",
@@ -35,7 +36,8 @@
3536
},
3637
"babel": {
3738
"presets": [
38-
"@babel/env"
39+
"@babel/env",
40+
"@vue/babel-preset-jsx"
3941
]
4042
}
4143
}

Diff for: e2e/2.x/babel-in-package/test.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { mount } from '@vue/test-utils'
22
import TypeScript from './components/TypeScript.vue'
33
import Basic from './components/Basic.vue'
44
import Coffee from './components/Coffee.vue'
5+
import Tsx from './components/Tsx.vue'
56

67
test('processes .vue files', () => {
78
const wrapper = mount(Basic)
@@ -13,7 +14,12 @@ test('processes .vue file with lang set to coffee', () => {
1314
expect(wrapper.vm).toBeTruthy()
1415
})
1516

16-
test('processes .vue files with lang set to typescript', () => {
17+
test('processes .vue files with lang set to ts(typescript)', () => {
1718
const wrapper = mount(TypeScript)
1819
expect(wrapper.vm).toBeTruthy()
1920
})
21+
22+
test('processes .vue files with lang set to tsx(typescript)', () => {
23+
const wrapper = mount(Tsx)
24+
expect(wrapper.text()).toContain('tsx components')
25+
})

Diff for: e2e/2.x/babel-in-package/tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"emitDecoratorMetadata": true,
1515
"suppressImplicitAnyIndexErrors": true,
1616
"allowSyntheticDefaultImports": true,
17-
"sourceMap": true
17+
"sourceMap": true,
18+
"esModuleInterop": true
1819
}
1920
}

Diff for: e2e/2.x/basic/test.js

+32
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ import Pug from './components/Pug.vue'
1919
import PugRelative from './components/PugRelativeExtends.vue'
2020
import Jsx from './components/Jsx.vue'
2121
import Constructor from './components/Constructor.vue'
22+
import { compileStyle } from '@vue/component-compiler-utils'
23+
jest.mock('@vue/component-compiler-utils', () => ({
24+
...jest.requireActual('@vue/component-compiler-utils'),
25+
compileStyle: jest.fn(() => ({ errors: [], code: '' }))
26+
}))
2227

28+
beforeEach(() => jest.clearAllMocks())
2329
test('processes .vue files', () => {
2430
const wrapper = mount(Basic)
2531
expect(wrapper.vm.msg).toEqual('Welcome to Your Vue.js App')
@@ -149,3 +155,29 @@ test('processes SFC with no template', () => {
149155
const wrapper = mount(RenderFunction)
150156
expect(wrapper.element.tagName).toBe('SECTION')
151157
})
158+
159+
test('should pass properly "styleOptions" into "preprocessOptions"', () => {
160+
const filePath = resolve(__dirname, './components/Basic.vue')
161+
const fileString = readFileSync(filePath, { encoding: 'utf8' })
162+
const config = {
163+
moduleFileExtensions: ['js', 'vue'],
164+
globals: {
165+
'vue-jest': {
166+
styleOptions: {
167+
quietDeps: true
168+
}
169+
}
170+
}
171+
}
172+
173+
jestVue.process(fileString, filePath, {
174+
config
175+
})
176+
177+
expect(compileStyle.mock.calls[0][0].preprocessOptions).toStrictEqual({
178+
quietDeps: true
179+
})
180+
expect(compileStyle.mock.calls[1][0].preprocessOptions).toStrictEqual({
181+
quietDeps: true
182+
})
183+
})

Diff for: e2e/3.x/babel-in-package/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
"test": "jest --no-cache test.js"
88
},
99
"dependencies": {
10-
"vue": "^3.2.6"
10+
"vue": "^3.2.19"
1111
},
1212
"devDependencies": {
1313
"@babel/core": "^7.9.0",
1414
"@babel/preset-env": "^7.9.0",
15-
"@vue/compiler-sfc": "^3.2.6",
15+
"@vue/compiler-sfc": "^3.2.19",
1616
"coffeescript": "^2.3.2",
1717
"jest": "^27.0.0",
1818
"ts-jest": "^27.0.1",

Diff for: e2e/3.x/basic/components/CompilerDirective.vue

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template>
2+
<div>
3+
<h1 class="header" v-test="'value'">Hello</h1>
4+
</div>
5+
</template>

Diff for: e2e/3.x/basic/jest.config.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const vTestDirective = require('./v-test-directive')
2+
3+
module.exports = {
4+
testEnvironment: 'jsdom',
5+
moduleFileExtensions: ['js', 'json', 'vue', 'ts'],
6+
transform: {
7+
'^.+\\.ts$': 'ts-jest',
8+
'^.+\\.js$': 'babel-jest',
9+
'^.+\\.vue$': '@vue/vue3-jest'
10+
},
11+
moduleNameMapper: {
12+
'^~?__styles/(.*)$': '<rootDir>/components/styles/$1'
13+
},
14+
globals: {
15+
'vue-jest': {
16+
pug: {
17+
basedir: './'
18+
},
19+
compilerOptions: {
20+
directiveTransforms: {
21+
test: vTestDirective
22+
}
23+
}
24+
}
25+
}
26+
}

Diff for: e2e/3.x/basic/package.json

+2-26
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
"test": "jest --no-cache --coverage test.js"
88
},
99
"dependencies": {
10-
"vue": "^3.2.6"
10+
"vue": "^3.2.19"
1111
},
1212
"devDependencies": {
1313
"@babel/core": "^7.9.0",
1414
"@babel/preset-env": "^7.9.0",
15-
"@vue/compiler-sfc": "^3.2.6",
15+
"@vue/compiler-sfc": "^3.2.19",
1616
"babel-helper-vue-jsx-merge-props": "^2.0.3",
1717
"babel-plugin-syntax-jsx": "^6.18.0",
1818
"babel-plugin-transform-vue-jsx": "^3.7.0",
@@ -25,29 +25,5 @@
2525
"vue-class-component": "^8.0.0-beta.4",
2626
"@vue/vue3-jest": "^27.0.0-alpha.1",
2727
"vue-property-decorator": "^10.0.0-rc.3"
28-
},
29-
"jest": {
30-
"testEnvironment": "jsdom",
31-
"moduleFileExtensions": [
32-
"js",
33-
"json",
34-
"vue",
35-
"ts"
36-
],
37-
"transform": {
38-
"^.+\\.ts$": "ts-jest",
39-
"^.+\\.js$": "babel-jest",
40-
"^.+\\.vue$": "@vue/vue3-jest"
41-
},
42-
"moduleNameMapper": {
43-
"^~?__styles/(.*)$": "<rootDir>/components/styles/$1"
44-
},
45-
"globals": {
46-
"vue-jest": {
47-
"pug": {
48-
"basedir": "./"
49-
}
50-
}
51-
}
5228
}
5329
}

Diff for: e2e/3.x/basic/test.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { randomExport } from './components/NamedExport.vue'
2222
import ScriptSetup from './components/ScriptSetup.vue'
2323
import ScriptSetupSugarRef from './components/ScriptSetupSugarRef.vue'
2424
import FunctionalRenderFn from './components/FunctionalRenderFn.vue'
25+
import CompilerDirective from './components/CompilerDirective.vue'
2526

2627
// TODO: JSX for Vue 3? TSX?
2728
import Jsx from './components/Jsx.vue'
@@ -36,7 +37,9 @@ function mount(Component, props, slots) {
3637
return h(Component, props, slots)
3738
}
3839
}
39-
createApp(Parent).mount(el)
40+
const app = createApp(Parent)
41+
app.directive('test', el => el.setAttribute('data-test', 'value'))
42+
app.mount(el)
4043
}
4144

4245
test('supports <script setup>', () => {
@@ -197,3 +200,10 @@ test('processes functional component exported as function', () => {
197200
expect(elem).toBeTruthy()
198201
expect(elem.innerHTML).toBe('Nyan')
199202
})
203+
204+
test('ensure compilerOptions is passed down', () => {
205+
mount(CompilerDirective)
206+
207+
const elm = document.querySelector('h1')
208+
expect(elm.hasAttribute('data-test')).toBe(false)
209+
})

Diff for: e2e/3.x/basic/v-test-directive.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = (dir, node, ...args) => {
2+
return { needRuntime: false, props: [] }
3+
}

Diff for: e2e/3.x/custom-transformers/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"test": "jest --no-cache --coverage test.js"
88
},
99
"dependencies": {
10-
"@vue/compiler-sfc": "^3.2.6",
11-
"vue": "^3.2.6"
10+
"@vue/compiler-sfc": "^3.2.19",
11+
"vue": "^3.2.19"
1212
},
1313
"devDependencies": {
1414
"@babel/core": "^7.9.0",

0 commit comments

Comments
 (0)