Skip to content

Instantly share code, notes, and snippets.

@miyu4u
Last active May 27, 2020 13:33
Show Gist options
  • Save miyu4u/4f17a348bfc6b0a090ce86eca4e13511 to your computer and use it in GitHub Desktop.
Save miyu4u/4f17a348bfc6b0a090ce86eca4e13511 to your computer and use it in GitHub Desktop.
mocking typescript class depedency with jest test framework
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