Last active
September 29, 2020 07:40
-
-
Save tkhduracell/099d0ceceba835cf182bf317e080dbab to your computer and use it in GitHub Desktop.
Typescript: Testing class with jest mock and typescript-ioc container save/restore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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