Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6556261
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: JavaScript

2021-11-26 13:04:53

学习开课吧的WEB前端架构,第一天的课程就搞得头大,一个Jest都搞了好几天。感谢强大的度娘了。一共遇到下面的几个问题。除了第一个是自身原因外,后两个真的无语,为什么最新的版本就不能搭配呢。看来开源唯一不好的就是相互关联的不能同时更新版本,你更新了我不更新,对不起,不能合作了。
  1. no tests found
  2. Cannot destructure property 'config'
  3. babelJest.getCacheKey is not a function
单元测试
安装依赖
yarn add jest
yarn add vue-jest@5.0.0-alpha.5
yarn add babel-jest
yarn add @vue/compiler-sfc@3.0.2
yarn add @vue/test-utils@next  
yarn add typescript
配置文件
jest.config.js

点击(此处)折叠或打开

  1. module.exports = {
  2.     testEnvironment: 'jsdom', // 默认JSdom
  3.     roots: [
  4.         '/src',
  5.         '/packages',
  6.     ], //
  7.     transform: {
  8.         '^.+\\.vue$': 'vue-jest', // vue单?件
  9.         '^.+\\js$': 'babel-jest' // esm最新语法 import
  10.     },
  11.     moduleFileExtensions: ['vue', 'js', 'json', 'jsx', 'ts', 'tsx', 'node'],
  12.     testMatch: ['**/__tests__/**/*.spec.js'],
  13.     // 别名
  14.     moduleNameMapper: {
  15.         '^element-ui(.*)$': '$1',
  16.         '^main(.*)$': '/src$1'
  17.     }
  18. }

测试文件 
/packages/button/tests/Button.spec.js

点击(此处)折叠或打开

  1. import Button from "../Button.vue";
  2. import { mount } from "@vue/test-utils";
  3.  
  4. //模拟运行组件
  5. it("content", () => {
  6.   const Comp = {
  7.   template: `<div><Button>默认按钮</Button></div>`,
  8.  };
  9.  
  10. const wrapper = mount(Comp, {
  11.   global: {
  12.     components: {
  13.       Button,
  14.     },
  15.   },
  16.  });
  17.  
  18.  //断言
  19. expect(wrapper.findComponent({ name: "ElButton" }).text()).toContain("默认按钮");
  20.  
  21. });

测试
配置 package.json
"test": "jest --runInBand", # 序列化执?

点击(此处)折叠或打开

  1. "scripts": {
  2.     "test": "jest --runInBand"
  3.   },

 运行
npm run jest

错误调试记录
错误 1:no tests found


错误原因
__tests__ 文件夹命名错误,少些了s。

错误 2:Cannot destructure property 'config'


TypeError: Cannot destructure property 'config' of 'undefined' as it is undefined.
      at Object.getCacheKey (node_modules/vue-jest/lib/index.js:10:7)


网上查了类似的错误,说是 jest 的版本问题。

参考



原来用的 

yarn add jest

"jest": "^27.0.4"

重装 jest 

yarn add jest@26.6.3

"jest": "26.6.3" 

错误3:babelJest.getCacheKey is not a function


Test suite failed to run

    TypeError: babelJest.getCacheKey is not a function

错误原因:jest 相关资源 babel-jest 版本也不对

修改

yarn add babel-jest@26.3.0
修改后再测试就好了。


[Vue warn]: Property "$ELEMENT" was accessed during render but is not defined on instance.

 对于单元测试,下图讲解 得不错。
阅读(4421) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~