Storybook: Storyshots fails to run, testStorySnapshots is intended only to be used inside jest

Created on 12 Oct 2017  路  5Comments  路  Source: storybookjs/storybook

After updating to latest version of jest initStoryshots() fails:

Test suite failed to run
testStorySnapshots is intended only to be used inside jest
at testStorySnapshots (node_modules/@storybook/addon-storyshots/dist/index.js:114:11)

I'm not sure if this is due to jest update since we went from version 19.x to 21.x.

From package.json:

"@storybook/addon-storyshots": "^3.2.12"
"@storybook/react": "^3.2.12"
"babel-jest": "^21.2.0"
"jest": "^21.2.1"

__ test __/storyshots.test.js

import initStoryshots from '@storybook/addon-storyshots';
initStoryshots();

When I check inside index.js:114:11 the _global is empty object {}.

storyshots question / support

Most helpful comment

I too had this with react native.
Anyone have any ideas?

My fix was to jest.mock global so the require global in storyshots got the real jest global.

import initStoryshots, {
  multiSnapshotWithOptions,
} from '@storybook/addon-storyshots';

jest.mock('global', () => global);

initStoryshots({
  configPath: 'src/test/Storybook',
  test: multiSnapshotWithOptions({}),
});

All 5 comments

Not sure if this helps but if I add

_global.describe = global.describe;
_global.it = global.it;
_global.window = global.window;

before

if (typeof _global.describe !== 'function') {
    throw new Error('testStorySnapshots is intended only to be used inside jest');
}

it works.

Hi,
So the reason is that in our project there is a file called global at the root of our project.
This root is defined in webpack resolve so we can import it without writing relative paths.
storyshots is relying on a library called global. Webpack imports our global file instead of the library.

Sorry not webpack resolve but jest equivalent modulePaths

As mentioned by @florian-bd it was due to a configuration issue in our project, and as we tried setting up a minimal project with webpack, jest, storybook and storyshots it worked well. So, closing this issue.

I too had this with react native.
Anyone have any ideas?

My fix was to jest.mock global so the require global in storyshots got the real jest global.

import initStoryshots, {
  multiSnapshotWithOptions,
} from '@storybook/addon-storyshots';

jest.mock('global', () => global);

initStoryshots({
  configPath: 'src/test/Storybook',
  test: multiSnapshotWithOptions({}),
});
Was this page helpful?
0 / 5 - 0 ratings