Skip to content

Commit

Permalink
fix: storybook mocks should be in testSetupAfterEnv to make sure test…
Browse files Browse the repository at this point in the history
…ing-library is cleanup properly
  • Loading branch information
christophehurpeau committed Sep 13, 2023
1 parent a8d8a63 commit af1152c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

'use strict';

if (!global.afterAll) {
throw new Error(
'Missing afterAll global in mock @storybook/react-native. Please check your jest test-setup and make sure you use testSetupAfterEnv.',
);
}

const decorateStory = (storyFn, decorators) =>
// eslint-disable-next-line unicorn/no-array-reduce
decorators.reduce(
Expand Down Expand Up @@ -79,7 +85,6 @@ exports.storiesOf = (groupName) => {
const rtlApi = render(story(context), { wrapper: WrappingComponent });
if (waitForExpectation) await waitFor(() => waitForExpectation(rtlApi, expect, { parameters }));
expect(rtlApi.toJSON()).toMatchSnapshot();
rtlApi.unmount();
});
});

Expand Down
9 changes: 7 additions & 2 deletions @ornikar/jest-config-react/__mocks__/@storybook/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

'use strict';

if (!global.afterAll) {
throw new Error(
'Missing afterAll global in mock @storybook/react. Please check your jest test-setup and make sure you use testSetupAfterEnv.',
);
}

const decorateStory = (storyFn, decorators) =>
// eslint-disable-next-line unicorn/no-array-reduce
decorators.reduce(
Expand Down Expand Up @@ -76,12 +82,11 @@ exports.storiesOf = (groupName) => {
: ({ children }) => decorateStory(() => children, [...localDecorators, ...globalDecorators])(context);

const rtlApi = render(story(context), { wrapper: wrappingComponent });
const { unmount, asFragment } = rtlApi;
const { asFragment } = rtlApi;
if (waitForExpectation) {
await waitFor(() => waitForExpectation(rtlApi, expect, { parameters }));
}
expect(asFragment()).toMatchSnapshot();
unmount();
});
});

Expand Down

0 comments on commit af1152c

Please sign in to comment.