CodeVetter · Repo Unpack · Behavior traces
jestjs/jest
Three concrete flows were followed through implementation: CLI startup and execution, module loading, and snapshot persistence. The execution trace includes the distinction between suite runners and the in-environment framework. Module loading branches on mocks, registry state, file type, and VM capabilities. Snapshot persistence includes retry rollback and cleanup of unchecked keys.
- Startup: bin/jest.js first attempts importLocal; otherwise it defaults NODE_ENV to test, calls run(), then buildArgv → getProjectListFromCLIArgs → runCLI → readConfigs.
- One-shot orchestration: runCLI → _run10000 → buildContextsAndHasteMaps; each project creates a cache directory, calls Runtime.createHasteMap().build(), and constructs a context before runWithoutWatch.
- Primary execution: runJest selects tests, shards before sorting, applies onlyFailures, runs globalSetup, schedules tests, caches results, runs globalTeardown, then processes results; listTests and collectTests return through earlier branches.
- Per-file execution: runTest → runTestInternal reads source/docblocks, loads environment and framework, constructs Runtime, executes setupFiles, calls environment.setup(), then invokes testFramework; runtime and environment teardown occur on the guarded completion path.
- Circus execution: jestAdapter loads jestAdapterInit through requireInternalModule, calls initialize, registers reset/mock cleanup in beforeEach, loads setupFilesAfterEnv and the test module, then calls runAndTransformResultsToJestFormat → run.
- CJS loading: Runtime.requireModuleOrMock checks teardown and mock decisions, then dispatches to a mock, framework singleton, or requireModule; CjsLoader resolves paths, returns registry hits, pre-registers fresh modules for cycles, and removes failed loads.
- ESM loading: EsmLoader.loadAndEvaluate resolves an ESM path, calls loadEsmModule, then linkAndEvaluateModule; synchronous loading requires VM and resolver capabilities, while the asynchronous path links imports, validates attributes, and shares evaluation promises.
- Snapshot persistence: initialize resolves the snapshot path and constructs SnapshotState; _toMatchSnapshot calls match, retry events call clear(test), and jestAdapter's _addSnapshotData protects failed/skipped tests, removes eligible unchecked keys, calls save, and transfers counters into TestResult.