Skip to content

Instantly share code, notes, and snippets.

@tkhduracell
Last active September 29, 2020 07:40
Show Gist options
  • Save tkhduracell/099d0ceceba835cf182bf317e080dbab to your computer and use it in GitHub Desktop.
Save tkhduracell/099d0ceceba835cf182bf317e080dbab to your computer and use it in GitHub Desktop.
Typescript: Testing class with jest mock and typescript-ioc container save/restore
import { Container, Snapshot } from 'typescript-ioc'
import { MyController } from '../../src/controllers/my'
import { MyService } from '../../src/services/my'
describe('MyController()', () => {
let snapshot!: Snapshot;
beforeEach(function () {
snapshot = Container.snapshot();
});
afterEach(function () {
snapshot.restore();
});
it('it return a ...', async () => {
const expected = { ... }
Container.bind(MyService).factory(jest.fn(() => ({
get: () => expected
})))
const service = new MyController()
const actual = await service.get()
expect(actual).toStrictEqual(expected)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment