vue 項目增加單元測試
- 在package.json 增加以下配置
// package.json
"devDependencies": {
"@vue/test-utils": "^1.0.0-beta.13",
"babel-jest": "^21.2.0",
"babel-preset-env": "^1.6.0",
"jest": "^21.2.1",
"regenerator-runtime": "^0.11.0",
"vue-template-compiler": "^2.4.4"
},
"scripts": {
"test": "jest"
},
- 根目錄增加 .babelrc
{
"presets": [
["env", {
"targets:": { "node": "6" } // change this to your node version
}]
]
}
- 安裝依賴
npm install
不支持 import
這是因為測試是在 nodejs 的環(huán)境下跑的直砂,但是 nodejs 不支持 ES6 的語法,所以需要配置一下
// 根目錄 .babelrc 添加以下配置即可
{
"env": {
"development": {
"plugins": [
"transform-es2015-modules-commonjs"
]
},
"test": {
"plugins": [
"transform-es2015-modules-commonjs"
]
}
}
}
不支持 locaStorage
- 安裝
yarn add --dev jest-localstorage-mock // yarn
或
npm i --save-dev jest-localstorage-mock // npm
- 在你的 package.json 里 jest 配置部分下帮匾,創(chuàng)建一個setupFiles陣列并添加jest-localstorage-mock到該陣列中
{
"jest": {
"setupFiles": ["jest-localstorage-mock"]
}
}