admin管理员组文章数量:1327975
I am using @storybook/react
6 in a create-react-app
project.
I have thousands of tests and mocks for all my stores/objects and so on.
These mocks make use of jest.fn()
.
I would like to reuse these mocks in my storybook stories but it says jest is not defined
which makes sense.
I am wondering if there is an easy way to make jest globally available when running storybook.
Currently I am adding import jest from 'jest-mock'
in every mock file that I use but I am wondering if there is a better solution.
Example:
test.stories.tsx
import React from 'react';
export default {
title: 'Test'
};
export Test = () => <button onClick={jest.fn()}>Test</button>;
Will pile but won't run -> jest is not defined
error
import React from 'react';
import jest from 'jest-mock';
export default {
title: 'Test'
};
export Test = () => <button onClick={jest.fn()}>Test</button>;
Works fine but I would like to avoid having to import jest-mock
everywhere.
I am using @storybook/react
6 in a create-react-app
project.
I have thousands of tests and mocks for all my stores/objects and so on.
These mocks make use of jest.fn()
.
I would like to reuse these mocks in my storybook stories but it says jest is not defined
which makes sense.
I am wondering if there is an easy way to make jest globally available when running storybook.
Currently I am adding import jest from 'jest-mock'
in every mock file that I use but I am wondering if there is a better solution.
Example:
test.stories.tsx
import React from 'react';
export default {
title: 'Test'
};
export Test = () => <button onClick={jest.fn()}>Test</button>;
Will pile but won't run -> jest is not defined
error
import React from 'react';
import jest from 'jest-mock';
export default {
title: 'Test'
};
export Test = () => <button onClick={jest.fn()}>Test</button>;
Works fine but I would like to avoid having to import jest-mock
everywhere.
2 Answers
Reset to default 5You can ensure the mock will be available in all your stories by adding the following lines to the preview.js
file in your project's .storybook
folder:
import jest from 'jest-mock';
window.jest = jest;
From the Storybook docs:
Use preview.js for global code (such as CSS imports or JavaScript mocks) that applies to all stories.
Import jest from storybook
import {jest} from '@storybook/jest'
const mockFunction = jest.fn()
...
本文标签: javascriptMake jest globally available in storybookStack Overflow
版权声明:本文标题:javascript - Make jest globally available in storybook - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742251483a2440845.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论