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

Review mocking solutions #4700

Closed
fregante opened this issue Nov 24, 2022 · 3 comments
Closed

Review mocking solutions #4700

fregante opened this issue Nov 24, 2022 · 3 comments

Comments

@fregante
Copy link
Contributor

I see a few issues with the current mocking situation:

  • Transparent mocking via __mocks__ is picked up by both Jest and Storybook
    • This avoids config, but if a mock uses jest.fn, then Storybook fails
    • Such files have to export everything we used, not just whatever we want to override
  • Inline mocking via jest.mock + callback
    • This allows partial mocks, but is relatively verbose both in declaration and in usage (import module, call jest.mock, then use (module.fn as any).mockResolvedValue() every time)

jest.mock("@/background/messenger/api", () => {
const actual = jest.requireActual("@/background/messenger/api");
return {
...actual,
getLoggingConfig: jest.fn().mockResolvedValue({
logValues: true,
}),
};
});
beforeEach(() => {
blockRegistry.clear();
blockRegistry.register(echoBlock, contextBlock, throwBlock);
(traces.addEntry as any).mockReset();
(traces.addExit as any).mockReset();
});

Possible solutions (discuss)

  • Avoid or limit Storybook mock sharing
  • Use ts-jest-mock to simplify mock typing
  • 🤔
@twschiller
Copy link
Contributor

There might also be ways to allow jest.fn in Storybook:

Given the popularity of Jest/Storybook, I suspect some people have found the "right way" to do this

@fregante
Copy link
Contributor Author

Another current issue is that automatic mocking only works if the import in the source starts with @/:

  • import '@/utils/file' will be mocked but import './file' won't

I think this can be solved by adjusting the config to map "full paths" as well.

@fregante
Copy link
Contributor Author

I noticed that you started using jest.mocked, which was the intent of this issue

@fungairino fungairino mentioned this issue Feb 8, 2024
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants