Skip to content

Commit af556eb

Browse files
authored
feat: allow to pass compilerOptions via the Jest config for Vue 3 (#412)
* chore: update to vue v3.2.22 Note that this also removes the now unneeded `@vue/compiler-sfc` dependency. Fixes #410 * feat: allow to pass compilerOptions via the Jest config for Vue 3 These options can be used to define Vue compiler options in `@vue/vue3-jest`. For example, to enable the newly intrioduced `propsDestructureTransform` in Vue v3.2.20: ```js globals: { 'vue-jest': { compilerOptions: { propsDestructureTransform: true } } } ``` or to disable `refTransform` (which is enabled by default): ```js globals: { 'vue-jest': { compilerOptions: { refTransform: false } } } ```
1 parent 592f676 commit af556eb

File tree

7 files changed

+136
-4
lines changed

7 files changed

+136
-4
lines changed

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,34 @@ vue-jest compiles `<script />`, `<template />`, and `<style />` blocks with supp
6868

6969
You can change the behavior of `vue-jest` by using `jest.globals`.
7070

71+
#### Compiler Options in Vue 3
72+
73+
These options can be used to define Vue compiler options in `@vue/vue3-jest`.
74+
75+
For example, to enable `propsDestructureTransform`:
76+
77+
```js
78+
globals: {
79+
'vue-jest': {
80+
compilerOptions: {
81+
propsDestructureTransform: true
82+
}
83+
}
84+
}
85+
```
86+
87+
or disable `refTransform` (which is enabled by default):
88+
89+
```js
90+
globals: {
91+
'vue-jest': {
92+
compilerOptions: {
93+
refTransform: false
94+
}
95+
}
96+
}
97+
```
98+
7199
#### Supporting custom blocks
72100

73101
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "vue3-typescript-with-compiler-options",
3+
"version": "1.0.0",
4+
"license": "MIT",
5+
"private": true,
6+
"scripts": {
7+
"test": "jest --no-cache ./src/test.ts"
8+
},
9+
"dependencies": {
10+
"vue": "^3.2.22"
11+
},
12+
"devDependencies": {
13+
"@types/jest": "16.0.10",
14+
"jest": "^27.0.0",
15+
"ts-jest": "^27.0.1",
16+
"typescript": "^4.1.2",
17+
"@vue/vue3-jest": "^27.0.0-alpha.1"
18+
},
19+
"jest": {
20+
"testEnvironment": "jsdom",
21+
"moduleFileExtensions": [
22+
"js",
23+
"ts",
24+
"json",
25+
"vue"
26+
],
27+
"moduleNameMapper": {
28+
"^@/(.*)$": "<rootDir>/src/$1"
29+
},
30+
"transform": {
31+
"^.+\\.ts$": "ts-jest",
32+
"^.+\\.vue$": "@vue/vue3-jest"
33+
},
34+
"globals": {
35+
"vue-jest": {
36+
"compilerOptions": {
37+
"propsDestructureTransform": true
38+
}
39+
}
40+
}
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<template>
2+
<h1>{{ name }}</h1>
3+
</template>
4+
5+
<script setup lang="ts">
6+
const { name = 'name' } = defineProps<{ name?: string }>()
7+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare module '*.vue' {
2+
import type { DefineComponent } from 'vue';
3+
const component: DefineComponent<{}, {}, any>;
4+
export default component;
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { createApp, h } from 'vue'
2+
3+
import PropsDestructureTransform from '@/components/PropsDestructureTransform.vue'
4+
5+
function mount(Component: any) {
6+
document.getElementsByTagName('html')[0].innerHTML = ''
7+
const el = document.createElement('div')
8+
el.id = 'app'
9+
document.body.appendChild(el)
10+
const Parent = {
11+
render() {
12+
return h(Component)
13+
}
14+
}
15+
createApp(Parent).mount(el)
16+
}
17+
18+
test('support additional compiler options like `propsDestructureTransform`', () => {
19+
// `propsDestructureTransform` is a new compiler option in v3.2.20
20+
// that allows to destructure props with default values and retain reactivity
21+
// The option is passed to the compiler via `globals.vue-jest.compilerOptions` of the Jest config in the package.json
22+
mount(PropsDestructureTransform)
23+
// if the option is properly passed, then the default value of the props is used
24+
expect(document.querySelector('h1')!.textContent).toBe('name')
25+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "esnext",
5+
"strict": true,
6+
"jsx": "preserve",
7+
"importHelpers": true,
8+
"moduleResolution": "node",
9+
"skipLibCheck": true,
10+
"esModuleInterop": true,
11+
"allowSyntheticDefaultImports": true,
12+
"forceConsistentCasingInFileNames": true,
13+
"useDefineForClassFields": true,
14+
"sourceMap": true,
15+
"baseUrl": ".",
16+
"types": ["webpack-env", "jest"],
17+
"paths": {
18+
"@/*": ["src/*"]
19+
},
20+
"lib": ["esnext", "dom", "dom.iterable", "scripthost"]
21+
},
22+
"include": ["src/**/*.ts", "src/**/*.vue"],
23+
"exclude": ["node_modules"]
24+
}

packages/vue3-jest/lib/process.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ function processScriptSetup(descriptor, filePath, config) {
5151
if (!descriptor.scriptSetup) {
5252
return null
5353
}
54+
const vueJestConfig = getVueJestConfig(config)
5455
const content = compileScript(descriptor, {
5556
id: filePath,
56-
refTransform: true
57+
refTransform: true,
58+
...vueJestConfig.compilerOptions
5759
})
5860
const contentMap = mapLines(descriptor.scriptSetup.map, content.map)
5961

60-
const vueJestConfig = getVueJestConfig(config)
6162
const transformer = resolveTransformer(
6263
descriptor.scriptSetup.lang,
6364
vueJestConfig
@@ -77,7 +78,6 @@ function processTemplate(descriptor, filename, config) {
7778
}
7879

7980
const vueJestConfig = getVueJestConfig(config)
80-
8181
if (template.src) {
8282
template.content = loadSrc(template.src, filename)
8383
}
@@ -86,7 +86,8 @@ function processTemplate(descriptor, filename, config) {
8686
if (scriptSetup) {
8787
const scriptSetupResult = compileScript(descriptor, {
8888
id: filename,
89-
refTransform: true
89+
refTransform: true,
90+
...vueJestConfig.compilerOptions
9091
})
9192
bindings = scriptSetupResult.bindings
9293
}

0 commit comments

Comments
 (0)