简体   繁体   中英

Test case for node js with sinon and 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);

Here is the solution to stub getFinancialYears function:

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();
  });
});

Unit test result with coverage report:

  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 |
----------------|----------|----------|----------|----------|-------------------|

Source code: https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/55573978

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM