Coder Social home page Coder Social logo

hk-test-template's Introduction

web-template

单元测试

这里有一些关于单元测试相关的介绍。

单元测试简介

所选技术栈

提供了vue编写的组件测试的相关解决方法,底层基于Mocha,Chai,Sinon,Jsdom等;当然我们对函数等工具库编写单测的时候可以灵活搭配Mocha,Jest,Chai等使用。

开箱即用,是一个集成框架,集成了断言库,覆盖率报告等多种功能。

编写示例

  • 技术选型:Jest,@vue/test-utils
  • 常用API:describe(),test(),expect()
  • 测试命令:
jest add.spec.js 
 // or 通过package.json配置
npm run test:unit
  • 覆盖率命令:
jest add.spec.js --coverage
// or 通过package.json配置
npm run test:unit --coverage
以测试add方法为例
// add.js

module.exports = function add(a, b) {
  return a + b;
};

// add.spec.js

const add = require('../../src/utils/add');

describe('add', () => {
  test('1+2 equals 3', () => {
    expect(add(1, 2)).toBe(3);
  });
});

执行测试用例通过,会打印如下信息:

PASS  tests/unit/add.spec.js
  add
    ✓ 1+2 equals 3 (2ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.587s, estimated 1s

未通过打印如下:

 FAIL  tests/unit/add.spec.js
  add
    ✕ 1+2 equals 3 (3ms)

  ● add › 1+2 equals 3

    expect(received).toBe(expected) // Object.is equality

    Expected: 6
    Received: 3

      3 | describe('add', () => {
      4 |   test('1+2 equals 3', () => {
    > 5 |     expect(add(1, 2)).toBe(6);
        |                       ^
      6 |   });
      7 | });

      at Object.<anonymous> (tests/unit/add.spec.js:5:23)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.807s, estimated 1s

如何分析覆盖率报告

单元测试编写规范

hk-test-template's People

Contributors

mr-yuhuashi avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.