test-runner: [Bug]ReferenceError: __test is not defined
I seem to get this error seemingly at random when running the tests. Following the stack trace just takes me to the very bottom of my story file.
FAIL browser: chromium src/Tag.stories.tsx (26.779 s)
● Base Components/Tag › Variants › smoke-test
page.evaluate: ReferenceError: __test is not defined
at eval (eval at evaluate (:3:2389), <anonymous>:4:17)
at t.default.evaluate (<anonymous>:3:2412)
at t.default.<anonymous> (<anonymous>:1:44)
at testFn (src/Tag.stories.tsx:84:37)
at Object.<anonymous> (src/Tag.stories.tsx:99:17)
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 10
- Comments: 27 (10 by maintainers)
Commits related to this issue
- fix: Flaky "__test is not defined" issue fixes #68 — committed to storybookjs/test-runner by deleted user 2 years ago
- fix: Flaky "__test is not defined" issue fixes #68 — committed to storybookjs/test-runner by deleted user 2 years ago
It seems the race condition is resolved by running that initialization script not with
evaluate, but withaddInitScript.Hey there @throw5way thanks for the suggestion! I made a canary with the fix at #445, please try it out and give some feedback if you can. Thanks!
Looking at the source code, this smells like a race condition to me.
The
__testinvocation is defined here:https://github.com/storybookjs/test-runner/blob/6624c5e6b7958e05f7c9dbeb57b581e374202c2b/src/playwright/transformPlaywright.ts#L28-L30
And the
__testdefinition is insetup-page-script.ts: https://github.com/storybookjs/test-runner/blob/6624c5e6b7958e05f7c9dbeb57b581e374202c2b/src/setup-page-script.ts#L266Which in turn is loaded in
setupPage: https://github.com/storybookjs/test-runner/blob/6624c5e6b7958e05f7c9dbeb57b581e374202c2b/src/setup-page.ts#L64-L75Could the explanation be that there is a race between the invocation and loading of the setup page?
For me, this is caused when I navigate to a different URL trying to capture snapshots during
postRender(). Probably the test runner injects something into the page that is lost by navigating to another URL.(The use case for navigating for me, is to put different arg values in the URL and as such capture different permutations of the Story).
These errors were definitely mixed with other ones, but I’m not sure in which order they came.
I’ll see if I have time to get some more info about this when back at work on Monday.