#jest 6 articles
TextEncoder is not defined before your first test runs — a dependency needs it at import time and jsdom does not have it gotchas / tooling jsdom has no Encoding API and a dependency that builds new TextEncoder() at module level dies on import, before beforeAll; import a polyfill module first or use setupFiles, and make the dependency lazy A component from a linked package throws "Invalid hook call" in Jest — the test loaded a second copy of React gotchas / tooling Jest resolves a file:/junction package by its real path, so the package's require('react') finds a second copy; map react and react-dom to the app's copy (the SPFx build is unaffected — React is a platform component) A deleted or moved test keeps running from stale lib-commonjs — the SPFx rig's --clean never touches that folder gotchas / spfx In SPFx projects built with Heft, Jest runs the compiled tests in lib-commonjs, but the rig's clean step deletes dist, lib, release, generated .scss.ts/.resx.ts files and parts of temp (and jest-output for the test… In a Heft/SPFx project jest.mock() doesn't hoist — and sp-http drags in a missing MS-internal module gotchas / spfx Tests run over pre-compiled lib-commonjs without Babel, so a mock written below the imports lands after require; write it above the imports or use moduleNameMapper (and why only some sp-http suites die on… Jest's 5-second default timeout fails render tests under load, and the timed-out test breaks the next one gotchas / tooling The first jsdom + React test of a suite pays for the cold module load; on a busy build machine it passes 5 s, Jest abandons it while its async render keeps running, and the following test fails with "button not found"… Setting process.env.TZ inside Jest does not change the time zone gotchas / tooling A test, setupFiles and setupFilesAfterEnv write into the sandbox's copy of process.env, so day-boundary tests pass only on machines already in the right zone; pin it in globalSetup, assert the offset, and run once…