问答

返回 一个字符串,jest单元测试要怎么断言,如下代码

作者:admin 2021-08-21 我要评论

main.js let timeContent = () = { let content = ''; let nowDate = new Date(); let nowDay = nowDate.getDay(); let nowHours = nowDate.getHours(); let now...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

main.js

let timeContent = () => {
    let content = '';
    let nowDate = new Date();
    let nowDay = nowDate.getDay();
    let nowHours = nowDate.getHours();
    let nowMinutes = nowDate.getMinutes();
    let nowSeconds = nowDate.getSeconds();
    if (nowDay === 0 || nowDay === 6) {
        content = '距离周末还有0天';
    } else {
        content = `距离周末还有<span>${5-nowDay}天${23-nowHours}时${59-nowMinutes}分${59-nowSeconds}秒</span> `;
    }
    return content;
};

main.test.js

const timeContent = require('../src/main.js');

test('返回值是否包含距离周末还有', () => {
  expect(timeContent()).toMatch('距离周末还有');
});
###

需要mock Date构造函数,并返回确定的时间,测试结果必须是可预测的,否则是不可测试的。

main.js:

let timeContent = () => {
  let content = '';
  let nowDate = new Date();
  let nowDay = nowDate.getDay();
  let nowHours = nowDate.getHours();
  let nowMinutes = nowDate.getMinutes();
  let nowSeconds = nowDate.getSeconds();
  if (nowDay === 0 || nowDay === 6) {
    content = '距离周末还有0天';
  } else {
    content = `距离周末还有<span>${5 - nowDay}天${23 - nowHours}时${59 - nowMinutes}分${59 - nowSeconds}秒</span>`;
  }
  return content;
};

module.exports = timeContent;
const timeContent = require('./main');

describe('1010000022139507', () => {
  it('should pass', () => {
    const mockedDate = new Date(2020, 4, 9, 0, 0);
    const DateMock = jest.spyOn(global, 'Date').mockImplementationOnce(() => mockedDate);
    const actual = timeContent();
    expect(actual).toBe('距离周末还有0天');
    DateMock.mockRestore();
  });

  it('should pass 2', () => {
    const mockedDate = new Date(2020, 4, 8, 0, 0);
    const DateMock = jest.spyOn(global, 'Date').mockImplementationOnce(() => mockedDate);
    const actual = timeContent();
    expect(actual).toBe('距离周末还有<span>0天23时59分59秒</span>');
    DateMock.mockRestore();
  });
});

测试结果+测试覆盖率报告:

 PASS  segmentfault/1010000022139507/main.test.js (10.512s)
  1010000022139507
    ? should pass (4ms)
    ? should pass 2

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |                   
 main.js  |     100 |      100 |     100 |     100 |                   
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        12.487s

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 使用箭头函数return数据不能实现

    使用箭头函数return数据不能实现

  • 个人小程序号能接入小程序3D地图吗?

    个人小程序号能接入小程序3D地图吗?

  • vue 移动端项目中,使用了better-scrol

    vue 移动端项目中,使用了better-scrol

  • css如何做出区域平均划分为四列

    css如何做出区域平均划分为四列

腾讯云代理商
海外云服务器