question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

addon-interactions: jest-mock global is not defined

See original GitHub issue

My storybook setup works great until i add Interactions (with @storybook/jest):

Browser console fatal error (nothing loads):

Uncaught SyntaxError: The requested module '/node_modules/jest-mock/build/index.js?v=de720b8b' does not provide an export named 'fn'

However, adding /register seemed to work (based on https://stackoverflow.com/questions/68360359/storybook-addon-value-should-end-in-register-or-it-should-be-a-valid-preset):

  "addons": [
    "@storybook/addon-links",
    "@storybook/addon-essentials",
    '@storybook/addon-interactions/register'
  ],

BUT trying to actually use @storybook/jest in a story doesn’t work:

import { expect } from '@storybook/jest';

Gives you this error in the browser:

Uncaught ReferenceError: global is not defined at node_modules/jest-mock/build/index.js

Commenting out this in main works with a normal Interactions install:

"core": {
    "builder": "storybook-builder-vite"
  },

TL;DR: So default webpack works for Interactions; Vite builder doesn’t. (Other add-ons seem to work fine with vite builder btw)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
mrauhucommented, Dec 25, 2021

@peteromano here the guide:

How to enable Storybook Interactions addon with Vite builder

.storybook/main.js

module.exports = {
  addons: [
    '@storybook/addon-interactions',
    // ...
  ],
  // Change to a front-end framework of your choice
  framework: '@storybook/vue3',
  features: {
    // Optional, for fastest build
    storyStoreV7: true,
  },
  core: {
    builder: 'storybook-builder-vite',
  },
  /**
   * Extend Vite config
   * @param {import('vite').UserConfig} config
   * @param {'DEVELOPMENT'|'PRODUCTION'} [configType]
   * @returns {Promise<*>}
   */
  async viteFinal(config, { configType }) {
    // Needed only for development mode: `npm run storybook`
    config.optimizeDeps = configType === 'PRODUCTION' ? config.optimizeDeps : {
      ...(config.optimizeDeps || {}),
      include: [
        ...(config?.optimizeDeps?.include || []),
        // Fix: `@storybook/addon-interactions` exports is not defined or `jest-mock` does not provide an export named 'fn'
        'jest-mock',
        // Optional, but prevents error flashing in the Storybook component preview iframe:
        // Fix: failed to fetch dynamically import module, avoid cache miss for dependencies on the first load 
        '@storybook/components',
        '@storybook/store',
        // Add all addons that imported in the `preview.js` or `preview.ts` file and used in exported constants
        '@storybook/addon-links'
        '@storybook/theming',
      ],
    },
    // ...
    return config;
  };
}

.storybook/preview.js

import * as jest from '@storybook/jest';

// Fix: fn() is not defined, see: https://github.com/storybookjs/storybook/issues/15391
window.jest = jest;

.storybook/preview-head.html

<script>
  // Fix: add `jest` to global
  window.global = window;
</script>

@IanVS unfortunately the Storybook Interactions addon is sometimes checks for arguments of a story on the Docs page and failed with warning:

An update to Story inside a test was not wrapped in act(...)

Source of the error in the @storybook/theming package.

I think it’s because the core Storybook addons it’s written in React and the @storybook/addon-interactions using Jest inside to do testing.

0reactions
IanVScommented, Apr 4, 2022

New storybook projects created with npx sb init will automatically add a preview-head.html to avoid this issue. There’s not much else that can be done until jest@28 is used by storybook.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vite + Storybook 6.3 + Jest: "global is not defined" #15391
Describe the bug I am trying to get Vite + Storybook 6.3 working with some existing stories. These stories use jest.fn() as mocks....
Read more >
Jest: ReferenceError: global is not defined - Stack Overflow
After I added "jest-environment-jsdom": "^27.0.6" as a dev dependency, that error went away.
Read more >
@storybook/addon-interactions | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
How to fix the ReferenceError: global is not defined error in ...
I spent hours trying to figure this out and it was really difficult to find the exact information on the web since this...
Read more >
@storybook/addon-interactions - npm package | Snyk
Automate, test and debug user interactions For more information about how to use this package see README · Ensure you're using the healthiest...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found