Last active
May 27, 2020 13:33
-
-
Save miyu4u/4f17a348bfc6b0a090ce86eca4e13511 to your computer and use it in GitHub Desktop.
mocking typescript class depedency with jest test framework
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 { FoobarService } from "./foobar.service" | |
import { FooService } from "./foo.service" | |
import { BarService } from "./bar.service" | |
jest.mock("./foo.service") | |
jest.mock("./bar.service") | |
describe("foobar service", ()=>{ | |
let service:FoobarService | |
let foo:FooService | |
let bar:BarService | |
beforeEach(()=>{ | |
foo = jest.genMockFromModule<FooService>("./foo.service") | |
bar = jest.genMockFromModule<BarService>("./bar.service") | |
service = new FoobarService(foo,bar) | |
}) | |
it('test', async ()=>{ | |
foo.print = jest.fn().mockReturnValue("Foo") | |
bar.print = jest.fn().mockReturnValue("Bar Helloworld") | |
expect(service.print()).toContain("Hello") | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment