Skip to content
Latchkey

Node AssertionError in CI - Fix the Failed Invariant

An AssertionError is thrown when an assert call fails. It marks a violated invariant: the actual value did not match what the code asserted must be true.

What this error means

A node process or test throws AssertionError [ERR_ASSERTION] with expected and actual values, then exits non-zero because an assert condition was false.

node
AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:

  2 !== 3

    at Object.<anonymous> (/home/runner/work/app/app/test/sum.test.js:4:8) {
  code: 'ERR_ASSERTION', expected: 3, actual: 2, operator: 'strictEqual'
}

Common causes

The code under test produced the wrong value

A real bug makes the actual result diverge from the asserted expectation.

The assertion encodes a stale expectation

Behavior changed intentionally but the assert still checks the old value.

How to fix it

Fix the code so the invariant holds

  1. Read the expected vs actual values in the error.
  2. Correct the logic so the actual matches the assertion.

Update the assertion if the expectation changed

  1. Confirm the new behavior is intended.
  2. Adjust the asserted value to the correct expectation.
JavaScript
assert.strictEqual(sum(1, 2), 3);

How to prevent it

  • Keep assertions in sync with intended behavior, give them clear messages, and run the assertion path locally so failed invariants surface before CI.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →