Test case for node js with sinon and chai

时间:2019-04-08 12:54:19

标签: node.js unit-testing sinon chai

I am trying to use sinon to do a unit test for one of my node functions.

Below is the actual function.

import { replaceYears, getFinancialYears } from './utils/years/';

const replaceAssetFileName = () => {
  const years = getFinancialYears();    // return array of years [2019, 2018, 2017]
  for ( let i = 0; i < years.length; i++ ) {
    replaceYears( years[ i ], 'NEWNAME' ); // replace years just replace 2019 to FY19, don't ask why a function is written for this.
  }
return true;
};

I want to mock the function getFinancialYears, so that just for test the function can return only one or two years instead of 100s of years.

I tried below test case with sinon and chai. But still I see the function “getFinancialYears” giving out the actually list of years instead of fakes.

it( 'We can replace file names', () => {
    const stub = sinon.stub( util, 'getFinancialYears' ).callsFake(() => [ '2019' ]);
    expect( replaceAssetFileName()).to.be( true );
    stub.restore();
  }).timeout( 20000 );

1 个答案:

答案 0 :(得分:0)

以下是存根getFinancialYears函数的解决方案:

index.ts

import { replaceYears, getFinancialYears } from './utils/years';

export const replaceAssetFileName = () => {
  const years = getFinancialYears();
  console.log(years);
  for (let i = 0; i < years.length; i++) {
    replaceYears(years[i], 'NEWNAME');
  }
  return true;
};

utils/years.ts

export function getFinancialYears() {
  return ['2019', '2018', '2017'];
}

export function replaceYears(year, replacer) {}

index.spec.ts

import { replaceAssetFileName } from './';
import sinon from 'sinon';
import { expect } from 'chai';
import * as util from './utils/years';

describe('replaceAssetFileName', () => {
  it('We can replace file names', () => {
    const logSpy = sinon.spy(console, 'log');
    const stub = sinon.stub(util, 'getFinancialYears').callsFake(() => ['2019']);
    expect(replaceAssetFileName()).to.be.true;
    expect(logSpy.calledWith(['2019'])).to.be.true;
    stub.restore();
  });
});

带有覆盖率报告的单元测试结果:

  replaceAssetFileName
[ '2019' ]
    ✓ We can replace file names


  1 passing (10ms)

----------------|----------|----------|----------|----------|-------------------|
File            |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------------|----------|----------|----------|----------|-------------------|
All files       |    95.65 |      100 |    83.33 |    95.24 |                   |
 55573978       |      100 |      100 |      100 |      100 |                   |
  index.spec.ts |      100 |      100 |      100 |      100 |                   |
  index.ts      |      100 |      100 |      100 |      100 |                   |
 55573978/utils |    66.67 |      100 |       50 |    66.67 |                   |
  years.ts      |    66.67 |      100 |       50 |    66.67 |                 2 |
----------------|----------|----------|----------|----------|-------------------|

源代码:https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/55573978