Skip to content

feat: allow to pass compilerOptions via the Jest config for Vue 3 #412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,34 @@ vue-jest compiles `<script />`, `<template />`, and `<style />` blocks with supp

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

#### Compiler Options in Vue 3

These options can be used to define Vue compiler options in `@vue/vue3-jest`.

For example, to enable `propsDestructureTransform`:

```js
globals: {
'vue-jest': {
compilerOptions: {
propsDestructureTransform: true
}
}
}
```

or disable `refTransform` (which is enabled by default):

```js
globals: {
'vue-jest': {
compilerOptions: {
refTransform: false
}
}
}
```

#### Supporting custom blocks

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.
Expand Down
3 changes: 1 addition & 2 deletions e2e/3.x/babel-in-package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
"test": "jest --no-cache test.js"
},
"dependencies": {
"vue": "^3.2.19"
"vue": "^3.2.22"
},
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"@vue/compiler-sfc": "^3.2.19",
"coffeescript": "^2.3.2",
"jest": "^27.0.0",
"ts-jest": "^27.0.1",
Expand Down
3 changes: 1 addition & 2 deletions e2e/3.x/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
"test": "jest --no-cache --coverage test.js"
},
"dependencies": {
"vue": "^3.2.19"
"vue": "^3.2.22"
},
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"@vue/compiler-sfc": "^3.2.19",
"babel-helper-vue-jsx-merge-props": "^2.0.3",
"babel-plugin-syntax-jsx": "^6.18.0",
"babel-plugin-transform-vue-jsx": "^3.7.0",
Expand Down
3 changes: 1 addition & 2 deletions e2e/3.x/custom-transformers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"test": "jest --no-cache --coverage test.js"
},
"dependencies": {
"@vue/compiler-sfc": "^3.2.19",
"vue": "^3.2.19"
"vue": "^3.2.22"
},
"devDependencies": {
"@babel/core": "^7.9.0",
Expand Down
2 changes: 1 addition & 1 deletion e2e/3.x/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test": "jest --no-cache test.js"
},
"dependencies": {
"vue": "^3.2.19"
"vue": "^3.2.22"
},
"devDependencies": {
"@babel/core": "^7.9.0",
Expand Down
3 changes: 1 addition & 2 deletions e2e/3.x/style/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"test": "jest --no-cache test.js"
},
"dependencies": {
"@vue/compiler-sfc": "^3.2.19",
"vue": "^3.2.19"
"vue": "^3.2.22"
},
"devDependencies": {
"@babel/core": "^7.9.0",
Expand Down
3 changes: 1 addition & 2 deletions e2e/3.x/typescript-with-babel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"test": "jest --no-cache ./sub-project/test.js"
},
"dependencies": {
"@vue/compiler-sfc": "^3.2.19",
"vue": "^3.2.19"
"vue": "^3.2.22"
},
"devDependencies": {
"@babel/core": "^7.9.0",
Expand Down
42 changes: 42 additions & 0 deletions e2e/3.x/typescript-with-compiler-options/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "vue3-typescript-with-compiler-options",
"version": "1.0.0",
"license": "MIT",
"private": true,
"scripts": {
"test": "jest --no-cache ./src/test.ts"
},
"dependencies": {
"vue": "^3.2.22"
},
"devDependencies": {
"@types/jest": "16.0.10",
"jest": "^27.0.0",
"ts-jest": "^27.0.1",
"typescript": "^4.1.2",
"@vue/vue3-jest": "^27.0.0-alpha.1"
},
"jest": {
"testEnvironment": "jsdom",
"moduleFileExtensions": [
"js",
"ts",
"json",
"vue"
],
"moduleNameMapper": {
"^@/(.*)$": "<rootDir>/src/$1"
},
"transform": {
"^.+\\.ts$": "ts-jest",
"^.+\\.vue$": "@vue/vue3-jest"
},
"globals": {
"vue-jest": {
"compilerOptions": {
"propsDestructureTransform": true
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<template>
<h1>{{ name }}</h1>
</template>

<script setup lang="ts">
const { name = 'name' } = defineProps<{ name?: string }>()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did not realize this was something now supported in Vue, neat.

</script>
5 changes: 5 additions & 0 deletions e2e/3.x/typescript-with-compiler-options/src/shims-vue.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module '*.vue' {
import type { DefineComponent } from 'vue';
const component: DefineComponent<{}, {}, any>;
export default component;
}
25 changes: 25 additions & 0 deletions e2e/3.x/typescript-with-compiler-options/src/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { createApp, h } from 'vue'

import PropsDestructureTransform from '@/components/PropsDestructureTransform.vue'

function mount(Component: any) {
document.getElementsByTagName('html')[0].innerHTML = ''
const el = document.createElement('div')
el.id = 'app'
document.body.appendChild(el)
const Parent = {
render() {
return h(Component)
}
}
createApp(Parent).mount(el)
}

test('support additional compiler options like `propsDestructureTransform`', () => {
// `propsDestructureTransform` is a new compiler option in v3.2.20
// that allows to destructure props with default values and retain reactivity
// The option is passed to the compiler via `globals.vue-jest.compilerOptions` of the Jest config in the package.json
mount(PropsDestructureTransform)
// if the option is properly passed, then the default value of the props is used
expect(document.querySelector('h1')!.textContent).toBe('name')
})
24 changes: 24 additions & 0 deletions e2e/3.x/typescript-with-compiler-options/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"target": "es5",
"module": "esnext",
"strict": true,
"jsx": "preserve",
"importHelpers": true,
"moduleResolution": "node",
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"useDefineForClassFields": true,
"sourceMap": true,
"baseUrl": ".",
"types": ["webpack-env", "jest"],
"paths": {
"@/*": ["src/*"]
},
"lib": ["esnext", "dom", "dom.iterable", "scripthost"]
},
"include": ["src/**/*.ts", "src/**/*.vue"],
"exclude": ["node_modules"]
}
3 changes: 1 addition & 2 deletions e2e/3.x/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"test": "jest --no-cache ./src/test.ts"
},
"dependencies": {
"@vue/compiler-sfc": "^3.2.19",
"vue": "^3.2.19"
"vue": "^3.2.22"
},
"devDependencies": {
"@types/jest": "16.0.10",
Expand Down
9 changes: 5 additions & 4 deletions packages/vue3-jest/lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ function processScriptSetup(descriptor, filePath, config) {
if (!descriptor.scriptSetup) {
return null
}
const vueJestConfig = getVueJestConfig(config)
const content = compileScript(descriptor, {
id: filePath,
refTransform: true
refTransform: true,
...vueJestConfig.compilerOptions
})
const contentMap = mapLines(descriptor.scriptSetup.map, content.map)

const vueJestConfig = getVueJestConfig(config)
const transformer = resolveTransformer(
descriptor.scriptSetup.lang,
vueJestConfig
Expand All @@ -77,7 +78,6 @@ function processTemplate(descriptor, filename, config) {
}

const vueJestConfig = getVueJestConfig(config)

if (template.src) {
template.content = loadSrc(template.src, filename)
}
Expand All @@ -86,7 +86,8 @@ function processTemplate(descriptor, filename, config) {
if (scriptSetup) {
const scriptSetupResult = compileScript(descriptor, {
id: filename,
refTransform: true
refTransform: true,
...vueJestConfig.compilerOptions
})
bindings = scriptSetupResult.bindings
}
Expand Down
3 changes: 1 addition & 2 deletions packages/vue3-jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.9.0",
"@vue/compiler-sfc": "^3.2.19",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "^27.0.0",
"conventional-changelog": "^1.1.5",
Expand All @@ -34,7 +33,7 @@
"semantic-release": "^15.13.2",
"ts-jest": "^27.0.1",
"typescript": "^4.1.2",
"vue": "^3.2.19"
"vue": "^3.2.22"
},
"peerDependencies": {
"@babel/core": "7.x",
Expand Down
Loading