Javascript单元测试工具-Jest 学习笔记(一)

写在前面

首先这并非一篇完整的关于Jest的教程,只是我的在接触jest学习的一点随手笔记,大部份内容都是对官方文档的一些翻译。只是让想了解jest的人快速知道一下jest是作什么的,怎么作。对于如何对项目进行测试,特别是如今很火的React,后面待熟练使用或者会另写一篇心得体会。javascript

What's Jest

Jest是Facebook开发的一个对javascript进行单元测试的工具,以前仅在其内部使用,后开源,而且是在Jasmine测试框架上演变开发而来,使用了咱们熟知的expect(value).toBe(other)这种断言格式。java

Getting Start

第一个Jest测试Demo

经过npm安装android

$ npm install jest --save-dev

编辑一个待测试的sum.js文件以下:git

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

编辑一个测试文件sum.test.jsgithub

注意:关于这个测试文件的位置,建议是对每一个组件新建一个__test__文件夹,而后文件命名是name.test.js,用于存放测试文件。正则表达式

const sum = require('./sum');
    
test('adds 1 + 2 to equal 3', ()=> {
    expect(sum(1, 2)).toBe(3);
});

接着在package.json文件里添加测试命令npm

"scripts:" {
    "test": "jest"
}

最后,运行 npm test命令后jest将会打印以下信息json

PASS ./sum.test.js
✓ adds 1 + 2 to equal 3 (5ms)

至此,第一个测试Demo完成了。数组

Using Matchers

Jest使用matchers来使用不一样的方式测试你的结果。promise

Common Matchers

最简单的测试方式就是测试一个值是否全等

test('2加2等于4', ()=> {
    expect(2 + 2).toBe(4);
});

在上述代码中,expect(2+2)返回一个“指望”对象,一般咱们不会对这个指望对象进行匹配,而是由matchers完成,在这里.toBe(4)即是这个matchers,当Jest运行时,它会跟踪全部失败的matchers以便打印正确的错误信息给咱们。

toBe使用 === 来测试全等于,若是咱们想检查一个对象object中的值,使用toEqual来替代,toEqual递归遍历检查对象或数组里的每个领域。

"use strict";
    
test('object assigenment', ()=> {
    let data = { one: 1};
    data['two'] = 2;
    expect(data).toEqual({ one: 1, two: 2 });
})

注意:官网的例子是没有使用的"use strict";,而后npm test的时候就会报出一条错误
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

使用not能够测试一个matchers的反向规则:

test('adding positive numbers is not zero', () => {
    for (let a = 1; a < 10; a++) {
        for (let b = 1; b < 10; b++) {
            expect(a + b).not.toBe(0);
        }
    }
});

Truthiness

在测试的时候,有时候咱们须要在undefinednullfalse进行区别,可是咱们又不想去了解他们的不一样点,Jest也会帮助咱们获得咱们想要的结果。

  • toBeNull 检查是否为null

  • toBeUndefined 检查是否为undefined

  • toBeDefinedtoBeUndefined的相反

  • toBeTruthy 检查任何经过if显示转换是否为true

  • toBeFalsy 检查任何经过if显示转换是否为false

以下:

test('null', () => {
    let n = null;
      expect(n).toBeNull();
      expect(n).toBeDefined();
      expect(n).not.toBeUndefined();
      expect(n).not.toBeTruthy();
      expect(n).toBeFalsy();
});

test('zero', () => {
      let z = 0;
      expect(z).not.toBeNull();
      expect(z).toBeDefined();
      expect(z).not.toBeUndefined();
      expect(z).not.toBeTruthy();
      expect(z).toBeFalsy();
});

Numbers

比较数字的大多数方法都有其对应的matchers

  • toBeGreaterThan 大于

  • toBeGreaterThanOrEqual 大于等于

  • toBeLessThan 小于

  • toBeLessThanOrEqual 小于等于

test('two plus two', () => {
    let value = 2 + 2;
    expect(value).toBeGreaterThan(3);
    expect(value).toBeGreaterThanOrEqual(3.5);
    expect(value).toBeLessThan(5);
    expect(value).toBeLessThanOrEqual(4.5);

    // toBe and toEqual 对于number类型做用是同样的
    expect(value).toBe(4);
    expect(value).toEqual(4);
});

对于浮点数的测试,使用toBeCloseTo来替代toEqual,由于咱们不会让一个测试依赖于一个微小的舍入型错误。

test('adding floating point numbers', () => {
    let value = 0.1 + 0.2;
    expect(value).not.toBe(0.3);    // It isn't! Because rounding error
    expect(value).toBeCloseTo(0.3); // This works.
});

Strings

使用toMatch对字符串进行正则表达式匹配

test('there is no I in team', () => {
    expect('team').not.toMatch(/I/);
});

test('but there is a "stop" in Christoph', () => {
    expect('Christoph').toMatch(/stop/);
})

Arrays

使用toContain对数组内的特定项进行匹配测试

let shoppingList = ['diapers', 'kleenex', 'trash bags', 'paper towels', 'beer'];

test('the shopping list has beer on it', () => {
    expect(shoppingList).toContain('beer');
});

Exceptions

使用toThrow对一个特定函数调用时候抛出的错误进行测试

function compileAndroidCode() {
    throw new ConfigError('you are using the wrong JDK');
}

test('compiling android goes as expected', () => {
    expect(compileAndroidCode).toThrow();
    expect(compileAndroidCode).toThrow(ConfigError);

    // You can also use the exact error message or a regexp
    expect(compileAndroidCode).toThrow('you are using the wrong JDK');
    expect(compileAndroidCode).toThrow(/JDK/);
});

Testing Asynchronous Code

在javascript程序中,会常常见到一些异步执行的代码,当咱们有这些异步执行的代码时,Jest须要知道当前这个代码测试是否已经完成,而后才能转向另外一个测试。Jest提供了一些方法来处理这种问题。

Callbacks

最经常使用的异步测试模式即是callbacks

列如,咱们有一个fetchData(callback)方法,当callback(data)方法调用的时候,咱们会获取一些data数据,而且想测试返回的数据是否只是一个字符串uyun

默认状况下下,Jest在全部的代码执行完以后便会完成测试,这意味这些测试再也不会按计划的工做下去。

// Don't do this!
test('the data is uyun', () => {
    function callback(data) {
        expect(data).toBe('uyun');
    }

    fetchData(callback);
});

问题是,测试但愿一旦fatchData成功后才能完成,并在其以前调用回调。

这里有另外一种形式修复这个测试的问题,在这个测试方法里使用一个参数为done的回调参数,而不是放置一个空参数,Jest要等到done被调用后才会结束这次测试。

test('the data is uyun', done => {
    function callback(data) {
        expect(data).toBe('uyun');
        done();
    }

    fetchData(callback);
});

若是done()没被调用,测试即失败了,这时候咱们也会获得咱们想要的错误结果了。

Promises

若是咱们的代码中使用到了Promises ,这里有一个简单的异步测试处理方法。只是咱们的测试中返回一个promise,而且Jest会等待这个promise解析完成,若是rejected了,这个测试便会自动视为失败。以下:

test('the data is uyun', () => {
    return fetchData().then(data => {
        expect(data).toBe('uyun');
    });
});

注意:必定要确保返回了Promise,若是省略了这步,你的测试将会在fetchData完成以前首先结束掉。

Async/Await

若是代码中使用到asyncawait,能够这样作。编写一个异步测试,仅须要在测试方法前面使用async关键词,而后传递给测试函数便可。以下:

test('the data is uyun', async () => {
    const data = await fetchData();
    expect(data).toBe('uyun');
});

在这个例子中,asyncawait等同于promises方法的一种语法糖实现方式。

相关文章
相关标签/搜索