Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'STORYBOOK_HOOKS_CONTEXT' of undefined #8083

Closed
loic-lopez opened this issue Sep 15, 2019 · 7 comments
Closed

Comments

@loic-lopez
Copy link

Describe the bug
When running initStoryshots from import initStoryshots from "@storybook/addon-storyshots"
an error occurs when storyshots starts.

Code snippets

Storyshot code:

import initStoryshots from "@storybook/addon-storyshots"

describe("storybook tests", () => {
  initStoryshots({
    configPath: "./storybook",
    framework: "react-native"
  })
})

Error from console:

 ● storybook tests › Storyshots › Header › Behavior

    TypeError: Cannot read property 'STORYBOOK_HOOKS_CONTEXT' of undefined

      at node_modules/@storybook/addons/dist/hooks.js:236:38
      at node_modules/@storybook/client-api/dist/client_api.js:129:14
      at node_modules/@storybook/addons/dist/hooks.js:266:20
      at Object.storyFn (node_modules/@storybook/client-api/dist/story_store.js:363:30)
      at getRenderedTree (node_modules/@storybook/addon-storyshots/dist/frameworks/react/renderTree.js:22:28)
      at node_modules/@storybook/addon-storyshots/dist/test-bodies.js:21:18
      at Object.<anonymous> (node_modules/@storybook/addon-storyshots/dist/api/snapshotsTestsTemplate.js:44:47)

System:

Environment Info:

  System:
    OS: macOS 10.15
    CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
  Binaries:
    Node: 12.10.0 - /usr/local/bin/node
    Yarn: 1.17.3 - /usr/local/bin/yarn
    npm: 6.11.3 - /usr/local/bin/npm
  Browsers:
    Chrome: 76.0.3809.132
    Safari: 13.0.1
  npmPackages:
    @storybook/addon-storyshots: ^5.1.11 => 5.2.0 
    @storybook/react-native: ^5.1.11 => 5.2.0 
    @storybook/theming: ^5.1.11 => 5.2.0
@shilman
Copy link
Member

shilman commented Sep 16, 2019

@Hypnosphi any ideas here?

@brunoebstein
Copy link

brunoebstein commented Sep 16, 2019

there is a reference to window in

const prevContext = window.STORYBOOK_HOOKS_CONTEXT;

but window does not exist in react native

infinitered-circleci pushed a commit to infinitered/ignite-bowser that referenced this issue Sep 16, 2019
## [4.6.3](v4.6.2...v4.6.3) (2019-09-16)

### Bug Fixes

* **deps:** Locking storybook version to avoid Storybook issue storybookjs/storybook#8083 ([3aa472a](3aa472a))
* **windows:** Move postInstall commands to their own script; add command to kill metro ([#247](#247) by [@bryanstearns](https://github.com/bryanstearns)) ([d5e878d](d5e878d))
@Hypnosphi Hypnosphi self-assigned this Sep 16, 2019
@loic-lopez
Copy link
Author

As a temporary workaround you can do:

import initStoryshots from "@storybook/addon-storyshots"
import { merge } from "lodash-es"

jest.mock("global", () => merge(global, { window: { STORYBOOK_HOOKS_CONTEXT: "" } }));

describe("storybook tests", () => {
  initStoryshots({
    configPath: "./storybook",
    framework: "react-native"
  })
})

@vladstart1
Copy link

vladstart1 commented Sep 19, 2019

@loic-lopez when I tried your code(to solve this problem) I got this error message in console:

babel-plugin-jest-hoist: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables.
Invalid variable access: merge
Whitelisted objects: Array, ArrayBuffer, Boolean, DataView, Date, Error, EvalError, Float32Array, Float64Array, Function, Generator, GeneratorFunction, Infinity, Int16Array, Int32Array, Int8Array, InternalError, Intl, JSON, Map, Math, NaN, Number, Object, Promise, Proxy, RangeError, ReferenceError, Reflect, RegExp, Set, String, Symbol, SyntaxError, TypeError, URIError, Uint16Array, Uint32Array, Uint8Array, Uint8ClampedArray, WeakMap, WeakSet, arguments, console, expect, isNaN, jest, parseFloat, parseInt, require, undefined, BigUint64Array, BigInt64Array, decodeURI, decodeURIComponent, encodeURI, encodeURIComponent, escape, unescape, eval, isFinite, SharedArrayBuffer, Atomics, BigInt, WebAssembly, DTRACE_NET_SERVER_CONNECTION, DTRACE_NET_STREAM_END, DTRACE_HTTP_SERVER_REQUEST, DTRACE_HTTP_SERVER_RESPONSE, DTRACE_HTTP_CLIENT_REQUEST, DTRACE_HTTP_CLIENT_RESPONSE, global, process, GLOBAL, root, Buffer, clearImmediate, clearInterval, clearTimeout, setImmediate, setInterval, setTimeout, URL, URLSearchParams.
Note: This is a precaution to guard against uninitialized mock variables. If it is ensured that the mock is required lazily, variable names prefixed with `mock` (case insensitive) are permitted.

Could you please help me, what I did wrong?

@loic-lopez
Copy link
Author

loic-lopez commented Sep 19, 2019

@vladstart1 I have imported merge function from lodash

Your error:

Invalid variable access: merge

You can do:

jest.mock("global", () => Object.assign(global, { window: { STORYBOOK_HOOKS_CONTEXT: "" } }));

@ndelangen ndelangen mentioned this issue Sep 23, 2019
@Hypnosphi Hypnosphi assigned ndelangen and unassigned Hypnosphi Sep 23, 2019
@shilman
Copy link
Member

shilman commented Sep 24, 2019

Olé!! I just released https://github.com/storybookjs/storybook/releases/tag/v5.3.0-alpha.1 containing PR #8163 that references this issue. Upgrade today to try it out!

You can find this prerelease on the @next NPM tag.

Closing this issue. Please re-open if you think there's still more to do.

@shilman shilman closed this as completed Sep 24, 2019
@shilman
Copy link
Member

shilman commented Oct 7, 2019

Yee-haw!! I just released https://github.com/storybookjs/storybook/releases/tag/v5.2.2 containing PR #8163 that references this issue. Upgrade today to try it out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants