使用jest.mock与Storybook

evrscar2  于 6个月前  发布在  Jest
关注(0)|答案(1)|浏览(86)

我需要你帮我写故事书
我需要在我的故事书文件中使用一个mock jest.mock

export default {
    title: 'MyStory',
    component: MyComponent,
    decorators: [withKnobs],
};
jest.mock('@my-custom-package/lib', () => mockFactory());

export function Demo() {
 
    return (
        <MyComponent
            defaultValue={defaultValue}
            value={value}
            onChange={setValue}
        />
    );
}

字符串
当我运行npm run storybook时,我得到了这个错误:jest is not defined
这是我的主.ts文件

const Dotenv = require('dotenv-webpack');

module.exports = {
    features: {
        babelModeV7: true,
    },
    stories: [
        '../../../my-project/src/**/*.stories.@(js|jsx|ts|tsx)'
    ],
    addons: ['@storybook/addon-links', '@storybook/addon-essentials', '@storybook/addon-knobs'],
    framework: {
        name: '@storybook/react-webpack5',
        options: { fastRefresh: true },
    },
    webpackFinal: (config: any) => {
        config.module.rules.push({
            test: /\.scss$/u,
            use: ['style-loader', 'css-loader', 'sass-loader'],
        });
        config.plugins.push(new Dotenv());
        // Return the altered config
        return config;
    },
};


我不知道我应该在哪里添加一些配置的笑话。

0tdrvxhp

0tdrvxhp1#

您需要单独导入jest对象的mock对象,以便在浏览器环境中使用它。

import jestMock from "jest-mock"

const fn = jestMock.fn()

字符串

相关问题