Git Product home page Git Product logo

js-unit's Introduction

jest单元测试

  • 基础测试
  • 异步方法测试
  • 依赖外部方法的测试
  • Snapshot测试

单元测试设计原则

  • 全面性
  • 互斥性

单元测试示例

尽管此单元测试覆盖率为100%,但并不是一个优秀的单元测试。

test('1+1应该等于2', () => {
    expect(add(1, 1)).toBe(2);
});

应该尽可能考虑多种场景,多种情况。

// 一个优秀的测试用例
describe('add方法的测试', () => {
  // 正常情况
  test('1+2应该等于3', () => {
    expect(add(1, 2)).toBe(3);
  });
  // 异常输入
  test('null+2应该等于2', () => {
    expect(add(null, 2)).toBe(2);
  });
  test('null+null应该等于0', () => {
    expect(add(null, null)).toBe(0);
  });
  test('"a"+"b"应该等于NaN', () => {
    expect(add('a', 'b')).toBe(NaN);
  });
  // 隐含的需求场景
  test('1+"2"应该等于3', () => {
    expect(add(1, '2')).toBe(3);
  });
  test('"1"+2应该等于3', () => {
    expect(add('1', 2)).toBe(3);
  });
  test('0.1+0.2不应该等于0.3', () => {
    expect(add(0.1, 0.2)).not.toBe(0.3);
    expect(add(0.1, 0.2)).toBeCloseTo(0.3);
  });
})

js-unit's People

Watchers

 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.