smrq / babel-plugin-require-context-hook

Babel plugin to replicate Webpack require.context
47 stars 14 forks source link

__requireContext is not defined. Babel 7/6 storybook #4

Closed eddiemonge closed 6 years ago

eddiemonge commented 6 years ago
    ReferenceError: __requireContext is not defined

       5 | import { withKnobs } from '@storybook/addon-knobs'
       6 |
    >  7 | const req = require.context('../components', true, /.?stories.jsx?$/)
         |          ^
       8 | function loadStories() {
       9 |   req.keys().forEach(filename => req(filename))
      10 | }

context

eddiemonge commented 6 years ago

I had to add

if (process.env.NODE_ENV === 'test') {
  require('babel-plugin-require-context-hook/register')()
}

to the storybook/config file as having it in jest/config didn't work.

I don't know if that is a bug here or in Jest or in Babel or a bug at all.

bob76828 commented 6 years ago

I have the same problem for this. Does anyone can help us?

bob76828 commented 6 years ago

@eddiemonge I solved it. I created a jest_setup.js in my .storybook folder for jest and wrote the code below as

import registerRequireContextHook from 'babel-plugin-require-context-hook/register';
registerRequireContextHook();

And last, I added the config

setupFiles: [
    './.storybook/jest_setup.js'
  ]

in my jest.config.js

eddiemonge commented 6 years ago

@bob76828 That worked. That's the part I was not understanding. when I read put it in jests setup file, I thought it meant its config, not its actual setupFiles