Back to Codeceptjs

Store

docs/store.md

4.0.04.0 KB
Original Source

Store

store is a global object that holds the state of the current run — which test and step are executing, which modes are enabled, and where the project lives on disk. Listeners, plugins, and helpers read it to find out where in the lifecycle they are without threading that information through every call.

js
import { store } from 'codeceptjs'

event.dispatcher.on(event.step.before, () => {
  if (store.dryRun) return            // skip side effects on a dry run
  console.log(`running ${store.currentTest?.title}`)
})

It is a single shared object for the whole process. Read from it freely; write to it only if you are deliberately driving execution state (see Writing to the store).

Fields

Required

Set once when the runner starts and immutable afterwards. Resolved from the test root, falling back to the legacy global.codecept_dir / global.output_dir.

FieldTypeWhat it is
store.codeceptDirstringroot directory of the tests
store.outputDirstringresolved output directory for artifacts
store.workerModebooleantrue when running inside a parallel worker

Session config

Set at session start and stable for that session. They reflect how the run was invoked.

FieldTypeWhat it is
store.debugModebooleanrun started with --debug
store.timeoutsbooleantimeouts are enabled
store.autoRetriesbooleanstep auto-retries are active (the tryTo effect turns this off while it runs)
store.dryRunbooleanrun is a --dry-run; steps must not cause side effects
store.featureOnlybooleana Feature.only() was used
store.scenarioOnlybooleana Scenario.only() was used
store.maskSensitiveDataboolean | objectmask secret values in output
store.noGlobalsbooleannoGlobals mode — the user imports everything instead of relying on globals

State

Changes constantly as the run progresses. The most useful fields for plugins and listeners:

FieldTypeWhat it is
store.onPausebooleanexecution is paused inside pause()
store.currentTestTest | nullthe running test, or null between tests
store.currentStepStep | nullthe running step, or null
store.currentSuiteSuite | nullthe running suite, or null between suites
store.tsFileMappingMap | nullTypeScript source-map lookup, when running .ts tests

currentTest, currentStep, and currentSuite carry the same objects passed to lifecycle events — see the test and step object fields.

Reading the store

A typical use is gating expensive or unsafe work by run mode:

js
import { store, recorder } from 'codeceptjs'

event.dispatcher.on(event.test.before, () => {
  if (store.dryRun || store.workerMode) return

  recorder.add('seed fixture data', async () => {
    await api.post('/users', { name: 'john' })
  })
})

store.currentTest is the simplest way to attach context to the current test from anywhere — for example tagging an artifact:

js
import { store } from 'codeceptjs'

store.currentTest?.artifacts.push({ name: 'log', path: '/tmp/run.log' })

Writing to the store

CodeceptJS keeps the state fields current for you — the built-in store listener sets currentSuite and currentTest around each suite and test. You rarely need to write to it.

Write only when you are deliberately driving execution — for example, a tool that opens pause() sets store.onPause. The required fields (codeceptDir, outputDir) are locked after store.initialize() and cannot be reassigned.


See also: Architecture · Extending CodeceptJS · Events