美文网首页
vue-test-util一些笔记

vue-test-util一些笔记

作者: 一点金光 | 来源:发表于2019-08-21 16:44 被阅读0次

the official unit testing utility library for Vue.js

now using vuejs-cli!!!!--It's now recommended to scaffold your project with Vue CLI 3 which provides out-of-the-box configurations for unit testing.

为什么要

01.已有方案
02.方便快速
03.借鉴参考

它是什么

the official unit testing utility library for Vue.js

如何使用

安装软件

::<<eof
npm install --save-dev vue-test-utils
eof

基本结构

::<<eof
# 创某组件
// counter.js
export default {
  template: `
    <div>
      <span class="count">{{ count }}</span>
      <button @click="increment">Increment</button>
    </div>
  `,

  data() {
    return {
      count: 0
    }
  },

  methods: {
    increment() {
      this.count++
    }
  }
}

#挂载组件
#2 导入类库
import { mount } from '@vue/test-utils'
#2 导入组件
import Counter from './counter'
#2 挂载组件
const wrapper = mount(Counter)
#2 访问实例
const vm = wrapper.vm
#2 查看更多
console.log(wrapper)
#2 其他操作:获取内容|用户交互...
eof

获取内容

::<<eof
expect(wrapper.html()).toContain('<span class="count">0</span>')
expect(wrapper.contains('button')).toBe(true)
eof

用户交互
此处用户交互主要讲述如何模拟用户交互

::<<eof
# 获取实例
  expect(wrapper.vm.count).toBe(0)
# 获取元素
  const button = wrapper.find('button')
# 触发点击
  button.trigger('click')
# 获取实例
  expect(wrapper.vm.count).toBe(1)
eof

异步操作
此处异步操作主要讲述如何模拟异步操作。

::<<eof
it('will catch the error using done', done => {
  Vue.config.errorHandler = done
  Vue.nextTick(() => {
    expect(true).toBe(false)
    done()
  })
})

it('will catch the error using a promise', () => {
  return Vue.nextTick().then(function() {
    expect(true).toBe(false)
  })
})
eof

一些技巧

01.Knowing What to Test
02.Shallow Rendering
03.Asserting Emitted Events
04.Emitting Event from Child Component
05.Manipulating Component State
06.Mocking Props
07.Applying Global Plugins and Mixins
08.Mocking Injections
09.Stubbing components
10.Dealing with Routing
11.Detecting styles

一些单元

用运行器
A test runner is a program that runs tests.

::<<eof
mocha-webpack
jest
eof

模拟浏端
running the tests in Node with a virtual browser environment using JSDOM

::<<eof
#安装
npm install --save-dev jsdom jsdom-global
#引入
require('jsdom-global')()

测试单组
一些测试单文件组件的示例
01.Testing Single-File Components with Jest
02.Testing Single-File Components with Mocha + webpack
03.Testing Single-File Components with Karma

一些示例

Testing Single-File Components with Jest

开始搭建

::<<eof
#安装软件
npm install --save-dev jest @vue/test-utils
#设置命令
// package.json
{
  "scripts": {
    "test": "jest"
  }
}
#处理单组
#2 安装
npm install --save-dev vue-jest
npm install --save-dev babel-jest
#2 设置
#3 01.tell Jest to handle `*.vue` files //{01}
#3 02.process `*.vue` files with `vue-jest` //{02}
#3 03.handling webpack Aliases //{03}
#3 04.configuring Babel for Jest  //{04}
#3 
// package.json
{
  // ...
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      // tell Jest to handle `*.vue` files  //{01}
      "vue"
    ],
    "transform": {
      // process `*.vue` files with `vue-jest` //{02}
      ".*\\.(vue)$": "vue-jest",
      // process js with `babel-jest` //{04}
      "^.+\\.js$": "<rootDir>/node_modules/babel-jest"
    },
    // support the same @ -> src alias mapping in source code //{03}
    "moduleNameMapper": {
      "^@/(.*)$": "<rootDir>/src/$1"
    },

  }
}
eof

哪些文件
01.By default, Jest will recursively pick up all files that have a .spec.js or .test.js extension in the entire project.
02.If this does not fit your needs, it's possible to change the testRegex
in the config section in the package.json file.
测覆盖率

::<<eof
{
  "jest": {
    // ...
    "collectCoverage": true,
    // define the files for which coverage information should be collected
    "collectCoverageFrom": ["**/*.{js,vue}", "!**/node_modules/**"],
    "coverageReporters": ["html", "text-summary"]
  }
}
eof

单元示例

::<<eof
import { mount } from '@vue/test-utils'
import Component from './component'

describe('Component', () => {
  test('is a Vue instance', () => {
    const wrapper = mount(Component)
    expect(wrapper.isVueInstance()).toBeTruthy()
  })
})
eof

测试快照

::<<eof
# 安装
npm install --save-dev jest-serializer-vue
# 配置
{
  // ...
  "jest": {
    // ...
    // serializer for snapshots
    "snapshotSerializers": ["jest-serializer-vue"]
  }
}
# 测试
test('renders correctly', () => {
  const wrapper = mount(Component)
  expect(wrapper.element).toMatchSnapshot()
})
eof

Testing Single-File Components with Mocha + webpack

::<<eof
# 安装
npm install --save-dev @vue/test-utils mocha mocha-webpack
# 设置
#2 01.设置命令
#2 02.Externalizing NPM Dependencies
#2 02.Setting Up Browser Environment
#2 03.Picking an Assertion Library
#2 04.Optimizing Babel for Tests
// package.json
{
  "scripts": {
    "test": "mocha-webpack --webpack-config webpack.config.js --require test/setup.js test/**/*.spec.js"
  }
}

// webpack.config.js
const nodeExternals = require('webpack-node-externals')

module.exports = {
  // ...
  externals: [nodeExternals()],
  devtool: 'inline-cheap-module-source-map',
}
eof

Testing Single-File Components with Karma

testing-single-file-components-with-karma

参考文献

vue-test-utils.guides.vuejs[p].
eddyerburgh.vue-test-utils-karma-example[p].github
vuejs.vue-test-utils-mocha-webpack-example[p].github
vuejs.vue-test-utils-jest-example[p].github

相关文章

  • vue-test-util一些笔记

    the official unit testing utility library for Vue.js now ...

  • 2019-03-16 一些临时笔记

    1. 近期的一些笔记 2. 仍然是一些笔记

  • 抑郁自我护理手册

    一些学习笔记~

  • 想成长,这有22个行动模型

    印象笔记一直是高效率人士推崇备至的笔记管理软件,其实印象笔记+一些行动管理模型功效倍增。今天印象笔记归纳了一些方法...

  • 2020-03-02 临时笔记

    只是一些临时笔记。

  • 我为什么要学习写作

    自己下载了包括印象笔记、有道云笔记在内的一些笔记和日记APP,在日记APP也写了一些日记,但是我更喜欢写...

  • Vue.js笔记

    整理的一些笔记一些自己在学习Vue中遇到的一些问题,都很简单的一些笔记 $ref介绍 侦测路由变化问题 keep-...

  • shiny搭建自己的笔记检索

    以前一直使用notepad++保存一些纯文本笔记,有时也会在简书保存一些总结性的笔记,后来苦于简书没有搜索自己笔记...

  • 开机自启

    一、说明   笔记主要是记录一些本人在开发当中的学习和使用笔记。笔记内容包含一些本人觉得重要的知识点、本人易犯的错...

  • JNI

    一、说明   笔记主要是记录一些本人在开发当中的学习和使用笔记。笔记内容包含一些本人觉得重要的知识点、本人易犯的错...

网友评论

      本文标题:vue-test-util一些笔记

      本文链接:https://www.haomeiwen.com/subject/szcmsctx.html