You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I expect getState to return the state that I'm in the "lol" describe block, not the root block
Actual behavior
getState returns wrong state object, because jest-circus gets loaded twice, and the state symbol for JEST_STATE_SYMBOL is instantiated with Symbol("JEST_STATE_SYMBOL") instead of Symbol.for("JEST_STATE_SYMBOL")`.
Additional context
Here's the log output from when I run the test in my repro project:
$ jest src/__tests__/add.test.js
console.log
{
type: 'describeBlock',
children: [],
hooks: [],
mode: undefined,
name: 'ROOT_DESCRIBE_BLOCK',
parent: undefined,
tests: []
}
at log (src/__tests__/add.test.js:8:11)
console.log
[ Symbol(JEST_STATE_SYMBOL), Symbol(JEST_STATE_SYMBOL) ]
at log (src/__tests__/add.test.js:11:11)
console.log
{
stateFromGlobal: [
{
type: 'describeBlock',
children: [],
hooks: [],
mode: undefined,
name: 'lol',
parent: [Object],
tests: []
},
{
type: 'describeBlock',
children: [],
hooks: [],
mode: undefined,
name: 'ROOT_DESCRIBE_BLOCK',
parent: undefined,
tests: []
}
]
}
at log (src/__tests__/add.test.js:12:11)
PASS src/__tests__/add.test.js
lol
✓ adds 1 + 2 to equal 3 (1 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.28 s, estimated 1 s
Ran all test suites matching /src\/__tests__\/add.test.js/i.
import{State,getState}from"jest-circus";typeGlobalKey=keyoftypeofglobal;letjestStateSymbol: GlobalKey|undefined;functionfindStateSymbols(): GlobalKey[]{returnReflect.ownKeys(global).filter((key)=>key.toString()=="Symbol(JEST_STATE_SYMBOL)")asGlobalKey[];}/** jest-circus state handling is a clown town * See https://github.com/jestjs/jest/issues/15018 for more information * * This method is a work-around which grabs the correct state object */exportfunctiongetJestState(): State{// If we have successfully found state before, keep using the symbol.if(jestStateSymbol)returnglobal[jestStateSymbol];/* there can be 0-2 state symbols: 0: state not yet initialized 1: only `describe` invocations has led to updated / accessed state, OR, getState was called by another module 2: both `describe` and another module has accessed state via getState. */for(constsymboloffindStateSymbols()){conststate: State|undefined=global[symbol];// undefined? keep lookingif(!state)continue;// root block? likely the wrong one. Keep lookingif(!state.currentDescribeBlock.parent)continue;// we found it? remember it going forwardjestStateSymbol=symbol;returnstate;}// didn't find anything? Just return the root state (which getState always returns, because of the bug).returngetState();}
Version
29.7.0
Steps to reproduce
jest src/__tests__/add.test.js
Expected behavior
I expect
getState
to return the state that I'm in the "lol" describe block, not the root blockActual behavior
getState
returns wrong state object, becausejest-circus
gets loaded twice, and the state symbol for JEST_STATE_SYMBOL is instantiated withSymbol("JEST_STATE_SYMBOL") instead of
Symbol.for("JEST_STATE_SYMBOL")`.Additional context
Here's the log output from when I run the test in my repro project:
Environment
The text was updated successfully, but these errors were encountered: