# vue-jest
Jest transformer for Vue Single File Components.
## Installation
Since we need to support a variety of Vue and Jest versions, vue-jest doesn't follow semantic versioning.
| Vue version | Jest Version | npm Package | Branch |
| ----------- | ----------------- | ------------------- | ------ |
| Vue 2 | Jest 26 and below | `vue-jest@4` | |
| Vue 3 | Jest 26 and below | `vue-jest@5` | |
| Vue 2 | Jest 27 and above | `@vue/vue2-jest@27` | 27.x |
| Vue 3 | Jest 27 and above | `@vue/vue3-jest@27` | 27.x |
| Vue 2 | Jest 28 and above | `@vue/vue2-jest@28` | 28.x |
| Vue 3 | Jest 28 and above | `@vue/vue3-jest@28` | 28.x |
```bash
# Vue 2
npm install --save-dev @vue/vue2-jest@28 # (use the appropriate version)
yarn add @vue/vue2-jest@28 --dev
# Vue 3
npm install --save-dev @vue/vue3-jest@28 # (use the appropriate version)
yarn add @vue/vue3-jest@28 --dev
```
## Setup
To use `vue-jest` as a transformer for your `.vue` files, map them to the appropriate `vue-jest` module:
```json
{
"jest": {
"transform": {
"^.+\\.vue$": "@vue/vue2-jest" // Update to match your installed version
}
}
}
```
A full config will look like this.
```json
{
"jest": {
"moduleFileExtensions": ["js", "json", "vue"],
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.vue$": "@vue/vue2-jest"
}
}
}
```
### Usage with Babel 7
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
```bash
npm install --save-dev babel-core@bridge
yarn add babel-core@bridge --dev
```
## Supported languages for SFC sections
vue-jest compiles `<script />`, `<template />`, and `<style />` blocks with supported `lang` attributes into JavaScript that Jest can run.
### Supported script languages
- **typescript** (`lang="ts"`, `lang="typescript"`)
- **coffeescript** (`lang="coffee"`, `lang="coffeescript"`)
### Global Jest options
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.
A `package.json` Example
```json
{
"jest": {
"moduleFileExtensions": ["js", "json", "vue"],
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.vue$": "@vue/vue2-jest"
},
"globals": {
"vue-jest": {
"transform": {
"your-custom-block": "./custom-block-processor.js"
}
}
}
}
}
```
> _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
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.
```js
module.exports = {
globals: {
'vue-jest': {
transform: {
'your-custom-block': require('./custom-block-processor')
}
}
}
}
```
#### Writing a processor
Processors must return an object with a "process" method, like so...
```js
module.exports = {
/**
* Process the content inside of a custom block and prepare it for execution in a testing environment
* @param {SFCCustomBlock[]} blocks All of the blocks matching your type, returned from `@vue/component-compiler-utils`
* @param {string} vueOptionsNamespace The internal namespace for a component's Vue Options in vue-jest
* @param {string} filename The SFC file being processed
* @param {Object} config The full Jest config
* @returns {string} The code to be output after processing all of the blocks matched by this type
*/
process({ blocks, vueOptionsNamespace, filename, config }) {}
}
```
#### templateCompiler
You can provide [TemplateCompileOptions](https://github.com/vuejs/component-compiler-utils#compiletemplatetemplatecompileoptions-templatecompileresults) in `templateCompiler` section like this:
```json
{
"jest": {
"globals": {
"vue-jest": {
"templateCompiler": {
"transpileOptions": {
"transforms": {
"dangerousTaggedTemplateString": true
}
}
}
}
}
}
}
```
### Supported template languages
- **pug** (`lang="pug"`)
- To give options for the Pug compiler, enter them into the Jest configuration.
The options will be passed to pug.compile().
```json
{
"jest": {
"globals": {
"vue-jest": {
"pug": {
"basedir": "mybasedir"
}
}
}
}
}
```
- **jade** (`lang="jade"`)
- **haml** (`lang="haml"`)
### Supported style languages
- **stylus** (`lang="stylus"`, `lang="styl"`)
- **sass** (`lang="sass"`), and
- **scss** (`lang="scss"`)
- 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.
```json
{
"jest": {
"moduleNameMapper": {
"^~foo/(.*)": "<rootDir>/foo/$1",
// @import '~foo'; -> @import 'path/to/project/foo';
"^~bar/(.*)": "~baz/lib/$1"
// @import '~bar/qux'; -> @import 'path/to/project/node_modules/baz/lib/qux';
// Notice how the tilde (~) was needed on the bare import to baz.
}
}
}
```
- To import globally included files (ie. variables, mixins, etc.), include them in the Jest configuration at `jest.globals['vue-jest'].resources.scss`:
```json
{
"jest": {
"globals": {
"vue-jest": {
"resources": {
"scss": [
"./node_modules/package/_mixins.scss",
"./src/assets/css/globals.scss"
]
}
}
}
}
}
```
## CSS options
`experimentalCSSCompile`: `Boolean` Default true. Turn off CSS compilation
`hideStyleWarn`: `Boolean` Default false. Hide warnings about CSS compilation
`resources`:
```json
{
"jest": {
"globals": {
"vue-jest": {
"hideStyleWarn": true,
"experimentalCSSCompile": true
}
}
}
}
```
## Style options
Possbility to change style loader options (sass, scss, less etc).
`styleOptions`: `Object` Default `{}`.
```json
{
"jest": {
"globals": {
"vue-jest": {
"styleOptions": {
"quietDeps" // e.q. sass options https://sass-lang.com/documentation/js-api#quietdeps
// unfortunately rest options like `data`, `file` doesnt work because @vue/compiler-component-utils internally overwrite options with their values
没有合适的资源?快使用搜索试试~ 我知道了~
Jest Vue 变压器.zip
共240个文件
vue:87个
js:66个
json:34个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 98 浏览量
2024-12-01
14:57:47
上传
评论
收藏 278KB ZIP 举报
温馨提示
vue-jest用于 Vue 单文件组件的 Jest 转换器。安装由于我们需要支持各种 Vue 和 Jest 版本,因此 vue-jest 不遵循语义版本控制。Vue 版本 Jest 版本 npm 包 分支Vue 2 Jest 26 及以下 vue-jest@4 Vue 3 Jest 26 及以下 vue-jest@5 Vue 2 Jest 27 及以上 @vue/vue2-jest@27 27.xVue 3 Jest 27 及以上 @vue/vue3-jest@27 27.xVue 2 Jest 28 及以上 @vue/vue2-jest@28 28.xVue 3 Jest 28 及以上 @vue/vue3-jest@28 28.x# Vue 2npm install --save-dev @vue/vue2-jest@28 # (use the appropriate version)yarn add @vue/vue2-jest@28 --dev# Vue 3npm install --save-dev @vue/vue3-jest@28
资源推荐
资源详情
资源评论
收起资源包目录
Jest Vue 变压器.zip (240个子文件)
external.css 40B
external.css 40B
.editorconfig 244B
.eslintrc 192B
.gitignore 246B
BasicSrc.html 71B
BasicSrc.html 71B
test.js 7KB
test.js 6KB
process.js 6KB
utils.js 5KB
process.js 5KB
utils.js 5KB
process-style.js 3KB
process-style.js 3KB
generate-code.js 3KB
module-name-mapper-helper.js 2KB
module-name-mapper-helper.js 2KB
generate-code.js 2KB
test.js 2KB
map-lines.js 2KB
map-lines.js 2KB
test.js 1KB
test.js 1KB
typescript.js 1KB
typescript.js 1KB
ensure-require.js 1KB
ensure-require.js 1KB
process-custom-blocks.js 1000B
process-custom-blocks.js 913B
coffee.spec.js 903B
coffee.spec.js 903B
test.js 748B
test.js 728B
coffee.js 651B
coffee.js 651B
transformer.js 626B
transformer.js 613B
scss-transformer.js 600B
scss-transformer.js 600B
test.js 594B
test.js 594B
jest.config.js 545B
test.js 537B
jest.config.js 531B
index.js 527B
index.js 527B
test.js 524B
test.js 524B
BasicSrc.js 429B
BasicSrc.js 429B
setup.js 386B
pcss-transformer.js 229B
pcss-transformer.js 229B
test.js 214B
test.js 214B
babel-transformer.js 133B
babel-transformer.js 133B
babel-transformer.js 133B
babel-transformer.js 133B
constants.js 107B
constants.js 107B
throw-error.js 101B
throw-error.js 101B
babel.config.js 88B
v-test-directive.js 88B
babel.config.js 88B
babel.config.js 54B
babel.config.js 54B
babel.config.js 54B
babel.config.js 54B
ModuleRequiringEsModuleInterop.js 29B
ModuleRequiringEsModuleInterop.js 29B
package.json 2KB
package.json 2KB
package.json 1KB
package.json 1KB
package.json 1KB
package.json 1KB
package.json 1KB
package.json 1KB
package.json 1KB
package.json 890B
package.json 867B
package.json 848B
package.json 834B
package.json 830B
package.json 813B
package.json 750B
package.json 710B
package.json 688B
tsconfig.json 620B
tsconfig.json 620B
tsconfig.base.json 559B
tsconfig.base.json 559B
package.json 558B
tsconfig.json 541B
tsconfig.json 530B
tsconfig.json 512B
package.json 346B
共 240 条
- 1
- 2
- 3
资源评论
徐浪老师
- 粉丝: 8585
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功