If you need to simulate the ./database/orm
module, you can use link seams.
In this case, since it's the CommonJS version, we will utilize proxyquire to create our seams.
For example:
main.ts
:
import { Countdown } from './database/orm';
export class PersistentTimer {
protected constructor(entity: Countdown) {}
public static async create() {
const entity = new Countdown();
await entity.save();
return new PersistentTimer(entity);
}
}
main.test.ts
:
import sinon from 'sinon';
import proxyquire from 'proxyquire';
import { expect } from 'chai';
describe('65399764', () => {
it('should pass', async () => {
const countdownInstanceStub = {
save: sinon.stub(),
};
const CountdownStub = sinon.stub().returns(countdownInstanceStub);
const { PersistentTimer } = proxyquire('./main.ts', {
'./database/orm': {
Countdown: CountdownStub,
},
});
const persistentTimer = await PersistentTimer.create();
sinon.assert.calledOnce(CountdownStub);
sinon.assert.calledOnce(countdownInstanceStub.save);
expect(persistentTimer).to.be.instanceOf(PersistentTimer);
});
});
unit test result:
65399764
✓ should pass (1848ms)
1 passing (2s)
-------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------------|---------|----------|---------|---------|-------------------
All files | 85.71 | 100 | 66.67 | 85.71 |
65399764 | 100 | 100 | 100 | 100 |
main.ts | 100 | 100 | 100 | 100 |
65399764/database | 50 | 100 | 0 | 50 |
orm.ts | 50 | 100 | 0 | 50 | 3
-------------------|---------|----------|---------|---------|-------------------