Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial mocking of module under test #138

Closed
iggyzap opened this issue Aug 31, 2022 · 1 comment
Closed

Partial mocking of module under test #138

iggyzap opened this issue Aug 31, 2022 · 1 comment

Comments

@iggyzap
Copy link

iggyzap commented Aug 31, 2022

Let's consider a scenario when we want to have a number of methods within a module replaced by mocks but want to test a specific method and it's interactions and don't want to extract these helper methods into different javascript file.

Is that something achievable with current version of esmock ?

Example:

export async function handler() {
  doBark();
}

async function doBark() {
  return "Wooof!"
}

And test:

import esmock from "esmock";

const mocks = {}

mocks['./foo.js'] = {
    otherFn: mockMethod
}

const mockMethod = jest.fn()

const foo = await esmock("./foo.js", mocks)

describe("Foo", () => {
    it("Foo defined", () => {
        expect(foo).toBeDefined();
    })


    it("Mock out internal function", async () => {

        await foo.some()
        expect(mockMethod).toHaveBeenCalled()
    })

})
@iambumblehead
Copy link
Owner

I don't think it is achievable with esmock. See this discussion here #118 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants