首页 > 其他 > 详细

Vue 项目添加单元测试发现的问题

时间:2019-10-28 15:43:34      阅读:186      评论:0      收藏:0      [点我收藏+]

用 Jest 测试单文件组件

1、安装 Jest 和 Vue Test Utils

npm install --save-dev jest @vue/test-utils

2、配置 package.json

// package.json
{
  "scripts": {
    "test": "jest"
  }
}

3、需要安装和配置 vue-jest 预处理器

npm install --save-dev vue-jest

4、在package.json 中创建一个 jest 块或在项目根目录创建 jest.config.js

module.exports = {
  moduleFileExtensions: [‘js‘, ‘jsx‘, ‘json‘, ‘vue‘],
  transform: {
    ‘^.+\\.vue$‘: ‘vue-jest‘,
    ‘.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$‘:
      ‘jest-transform-stub‘,
     // 为 Jest 配置 Babel
    ‘^.+\\.jsx?$‘: ‘babel-jest‘
  },
  transformIgnorePatterns: [‘/node_modules/‘],
  // 别名
  moduleNameMapper: {
    ‘^@/(.*)$‘: ‘<rootdir>/src/$1‘
  },
  snapshotSerializers: [‘jest-serializer-vue‘],
  testMatch: [
    ‘**/tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)‘
  ],
  testURL: ‘http://localhost/‘,
  watchPlugins: [
    ‘jest-watch-typeahead/filename‘,
    ‘jest-watch-typeahead/testname‘
  ]
};

错误信息处理

1、babel 解析 es6 出错,需要配置 babel 预设
技术分享图片
技术分享图片
> 需要配置安装 @babel/preset-env [babel-preset-env 会有问题]

npm install --save-dev @babel/preset-env

> 配置 babel.config.js

module.exports = {
  presets: [
    // 使可以正常运行 vue 项目,
    ‘@vue/app‘,
    [
      ‘@babel/preset-env‘,
      {
        modules: false,
      }
    ]
  ],
  env: {
    test: {
      presets: [[‘@babel/preset-env‘, { targets: { node: ‘current‘ } }]]
    }
  }
}

2、npm test
技术分享图片
> 清除缓存,然后再执行测试参考链接

 

Vue 项目添加单元测试发现的问题

原文:https://www.cnblogs.com/chentingjun/p/11752643.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!