Вот решение, для простоты я создаю объект fsExtra
для имитации модуля реального узла.
index.ts
:
export const logger = {
warn(message) {
console.warn(message);
}
};
export const yaml = {
safeLoad(file, callback) {}
};
export const fsExtra = {
readFileSync(filepath, options) {}
};
export function main() {
const filepath = './.tmp/sinon.js';
let file = yaml.safeLoad(fsExtra.readFileSync(filepath, 'utf8'), err => {
logger.warn(err);
});
}
index.spec.ts
:
import { main, fsExtra, yaml, logger } from '.';
import sinon from 'sinon';
import { expect } from 'chai';
describe('main', () => {
it('should safeLoad error', done => {
const mError = new Error('fake error');
const warnSpy = sinon.spy(logger, 'warn');
const readFileSyncStub = sinon.stub<any, string>(fsExtra, 'readFileSync').returns('file content');
const safeLoadStub = sinon.stub(yaml, 'safeLoad').callsFake((content, callback) => {
callback(mError);
done();
});
main();
expect(readFileSyncStub.calledWith('./.tmp/sinon.js', 'utf8')).to.be.true;
expect(safeLoadStub.calledWith('file content', sinon.match.func));
expect(warnSpy.calledWith(mError)).to.be.true;
});
});
Результат модульного теста с отчетом о покрытии:
main
Error: fake error
at Context.<anonymous> (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/src/stackoverflow/58357977/index.spec.ts:1:3887)
at callFnAsync (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runnable.js:415:21)
at Test.Runnable.run (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runnable.js:357:7)
at Runner.runTest (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:535:10)
at /Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:653:12
at next (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:447:14)
at /Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:457:7
at next (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:362:14)
at Immediate._onImmediate (/Users/ldu020/workspace/github.com/mrdulin/mocha-chai-sinon-codelab/node_modules/mocha/lib/runner.js:425:5)
at runCallback (timers.js:705:18)
at tryOnImmediate (timers.js:676:5)
at processImmediate (timers.js:658:5)
✓ should safeLoad error (46ms)
1 passing (55ms)
---------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 75 | 100 | |
index.spec.ts | 100 | 100 | 100 | 100 | |
index.ts | 100 | 100 | 60 | 100 | |
---------------|----------|----------|----------|----------|-------------------|
Исходный код: https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/58357977