-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
[Bug]: getState from jest-circus gets state from parellel environment #15018
Labels
Comments
this is a terrible workaround that works: import { State, getState } from "jest-circus";
type GlobalKey = keyof typeof global;
let jestStateSymbol: GlobalKey | undefined;
function findStateSymbols(): GlobalKey[] {
return Reflect.ownKeys(global).filter(
(key) => key.toString() == "Symbol(JEST_STATE_SYMBOL)"
) as GlobalKey[];
}
/** 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
*/
export function getJestState(): State {
// If we have successfully found state before, keep using the symbol.
if (jestStateSymbol) return global[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 (const symbol of findStateSymbols()) {
const state: State | undefined = global[symbol];
// undefined? keep looking
if (!state) continue;
// root block? likely the wrong one. Keep looking
if (!state.currentDescribeBlock.parent) continue;
// we found it? remember it going forward
jestStateSymbol = symbol;
return state;
}
// didn't find anything? Just return the root state (which getState always returns, because of the bug).
return getState();
} |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: