When working with Mocha, it's important to note that it doesn't have built-in stub/mock functions. In order to implement these functionalities, you can make use of an external library such as sinonjs.
For example:
fetchSettings.ts
:
interface MyDocment {}
export const fetchSettings = async (firestore): Promise<MyDocment> => {
try {
const response = await firestore.collection('someCollection').doc('someDoc').get();
const data = response.data()!;
return {
dataOne: data.someDataOne,
dataTwo: data.someDataTwo,
};
} catch (error) {
console.log(error);
return error;
}
};
fetchSettings.test.ts
:
import { fetchSettings } from './fetchSettings';
import sinon from 'sinon';
describe('66868604', () => {
it('should return data', async () => {
const mData = {
someDataOne: 'someDataOne',
someDataTwo: 'someDataTwo',
};
const mRes = { data: sinon.stub().returns(mData) };
const mFirestore = {
collection: sinon.stub().returnsThis(),
doc: sinon.stub().returnsThis(),
get: sinon.stub().resolves(mRes),
};
const actual = await fetchSettings(mFirestore);
sinon.assert.match(actual, { dataOne: 'someDataOne', dataTwo: 'someDataTwo' });
sinon.assert.calledWithExactly(mFirestore.collection, 'someCollection');
sinon.assert.calledWithExactly(mFirestore.doc, 'someDoc');
sinon.assert.calledOnce(mFirestore.get);
sinon.assert.calledOnce(mRes.data);
});
it('should handle error', async () => {
const mErr = new Error('stackoverflow');
const mFirestore = {
collection: sinon.stub().returnsThis(),
doc: sinon.stub().returnsThis(),
get: sinon.stub().rejects(mErr),
};
const actual = await fetchSettings(mFirestore);
sinon.assert.match(actual, mErr);
sinon.assert.calledWithExactly(mFirestore.collection, 'someCollection');
sinon.assert.calledWithExactly(mFirestore.doc, 'someDoc');
sinon.assert.calledOnce(mFirestore.get);
});
});
Unit test results:
66868604
✓ should return data
Error: stackoverflow
at /Users/dulin/workspace/github.com/mrdulin/expressjs-research/src/stackoverflow/66868604/fetchSettings.test.ts:25:18
at Generator.next (<anonymous>)
at /Users/dulin/workspace/github.com/mrdulin/expressjs-research/src/stackoverflow/66868604/fetchSettings.test.ts:8:71
at new Promise (<anonymous>)
at __awaiter (/Users/dulin/workspace/github.com/mrdulin/expressjs-research/src/stackoverflow/66868604/fetchSettings.test.ts:4:12)
at Context.<anonymous> (/Users/dulin/workspace/github.com/mrdulin/expressjs-research/src/stackoverflow/66868604/fetchSettings.test.ts:24:40)
at callFn (/Users/dulin/workspace/github.com/mrdulin/expressjs-research/node_modules/mocha/lib/runnable.js:364:21)
at Test.Runnable.run (/Users/dulin/workspace/github.com/mrdulin/expressjs-research/node_modules/mocha/lib/runnable.js:352:5)
at Runner.runTest (/Users/dulin/workspace/github.com/mrdulin/expressjs-research/node_modules/mocha/lib/runner.js:677:10)
at /Users/dulin/workspace/github.com/mrdulin/expressjs-research/node_modules/mocha/lib/runner.js:801:12
at next (/Users/dulin/workspace/github.com/mrdulin/expressjs-research/node_modules/mocha/lib/runner.js:594:14)
at /Users/dulin/workspace/github.com/mrdulin/expressjs-research/node_modules/mocha/lib/runner.js:604:7
at next (/Users/dulin/workspace/github.com/mrdulin/expressjs-research/node_modules/mocha/lib/runner.js:486:14)
at Immediate.<anonymous> (/Users/dulin/workspace/github.com/mrdulin/expressjs-research/node_modules/mocha/lib/runner.js:572:5)
at processImmediate (internal/timers.js:461:21)
✓ should handle error
2 passing (10ms)
------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
------------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
fetchSettings.ts | 100 | 100 | 100 | 100 |
------------------|---------|----------|---------|---------|-------------------