Skip to content

Flaky RuleEngine test on CI: ReferenceError: window is not defined #1187

Description

@gciotola

Describe the bug

CI randomly fails with an unhandled rejection while running
src/ui/forms/RuleEngine/index.test.tsx. Re-running the job usually makes it
pass.

https://github.com/commercelayer/app-elements/actions/runs/30795961354/job/91630433426

Unhandled Rejection
ReferenceError: window is not defined
 ❯ resolveUpdatePriority react-dom-client.development.js:1308:7
 ❯ dispatchSetState react-dom-client.development.js:9126:14
 ❯ src/ui/forms/RuleEngine/RuleEngineComponent.tsx:101:9

This error originated in "src/ui/forms/RuleEngine/index.test.tsx".
This error was caught after test environment was torn down.

Cause

RuleEngine calls fetchJsonSchema in an effect, which performs a real
network request
to https://core.commercelayer.io/api/public/schemas/order_rules:
there is no MSW handler for it, and the URL is explicitly excluded from the
onUnhandledRequest warning in src/mocks/setup.ts. Depending on network
latency the promise can resolve after the test file has finished, so
setOptionsConfig runs against an already torn-down jsdom environment.

Expected

Tests must not depend on network timing. The schema request should be mocked,
and the component should not update state after unmount.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions