How to fix AssertionError: Assertion failed (assert.strictEqual failed) in Node.js

Node.jsBEGINNERMEDIUMUpdated Dec 9, 2025· 99 views

This error occurs when a test assertion fails using Node.js assert.strictEqual(), indicating that the actual value does not match the expected value using strict equality (===).

Quick fix

Fixes ERR_ASSERTION

AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
+ actual - expected

+ 'hello'
- 'Hello'
  1. Examine the error output to identify the mismatch
  2. Verify both values have the same type
  3. Use deepStrictEqual for objects and arrays
  4. Fix the underlying logic or update expectations
node.js — error
AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
+ actual - expected
+ 'hello'
- 'Hello'
AssertionError: Assertion failed (assert.strictEqual failed)

What this error means

The AssertionError is thrown by Node.js's built-in assert module when an assertion test fails. When using assert.strictEqual(), this error indicates that two values are not strictly equal according to the === operator. Unlike assert.equal() which uses loose equality (==), assert.strictEqual() performs strict equality checking, meaning both the value AND type must match. This makes it more precise for testing and helps catch subtle bugs where values appear equal but have different types. The error object includes helpful debugging properties: err.actual (the actual value received), err.expected (the expected value), err.operator ("strictEqual"), and err.code ("ERR_ASSERTION"). These properties help you quickly identify what went wrong in your test.

Typical symptoms

  • Test suite fails with "AssertionError [ERR_ASSERTION]" message
  • Error message shows "Expected values to be strictly equal" with actual and expected values
  • CI/CD pipeline fails during test execution
  • Error displays visual diff showing the mismatch between values
  • Tests that worked with assert.equal() fail when switched to assert.strictEqual()
  • Comparing objects or arrays shows "Values identical but not reference-equal"

Common causes

  1. Comparing values with different types (e.g., number 1 vs string "1")
  2. Actual test result differs from expected value due to logic errors
  3. Using strictEqual to compare objects or arrays by reference instead of by content
  4. Floating point precision issues causing numeric comparisons to fail
  5. Comparing undefined vs null values
  6. Whitespace or formatting differences in string comparisons
  7. Test expectations are outdated after code changes
  8. Async operations not completing before assertion runs

How to fix "AssertionError: Assertion failed (assert.strictEqual failed)"

1Examine the error output to identify the mismatch

Read the AssertionError message carefully. It shows both the actual and expected values:

bash
AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
+ actual - expected

+ 'hello'
- 'Hello'

The + line shows what you got (actual), and the - line shows what you expected. Look for differences in:
- Value content
- Data types
- Whitespace
- Case sensitivity

2Verify both values have the same type

Use typeof or console.log to check the types of both values:

javascript
const actual = functionUnderTest();
const expected = 42;

console.log('Actual:', actual, typeof actual);    // "42" string
console.log('Expected:', expected, typeof expected);  // 42 number

// This will fail:
assert.strictEqual(actual, expected);

// Fix: Convert to same type
assert.strictEqual(Number(actual), expected);
// OR
assert.strictEqual(actual, String(expected));

Common type mismatches include number vs string, null vs undefined, and boolean vs truthy values.

3Use deepStrictEqual for objects and arrays

For comparing objects or arrays, use assert.deepStrictEqual() instead:

javascript
// This FAILS with strictEqual (compares references):
const actual = [1, 2, 3];
const expected = [1, 2, 3];
assert.strictEqual(actual, expected);  // AssertionError

// This PASSES with deepStrictEqual (compares content):
assert.deepStrictEqual(actual, expected);  // ✓

// Also works for objects:
const user = { name: 'Alice', age: 30 };
const expectedUser = { name: 'Alice', age: 30 };
assert.deepStrictEqual(user, expectedUser);  // ✓

Use strictEqual only for primitives (strings, numbers, booleans) and reference equality checks.

4Fix the underlying logic or update expectations

If the assertion correctly identifies a mismatch, fix your code or update the test:

javascript
// Example: Function returns wrong value
function calculateTotal(items) {
  return items.reduce((sum, item) => sum + item.price, 0);
}

// Test fails because function doesn't handle empty arrays
const result = calculateTotal([]);
assert.strictEqual(result, 0);  // Passes

// But if function had a bug:
function calculateTotalBuggy(items) {
  return items.reduce((sum, item) => sum + item.price);  // No initial value!
}

const buggyResult = calculateTotalBuggy([]);
assert.strictEqual(buggyResult, 0);  // FAILS - returns undefined

// Fix: Add initial value or handle empty case

Sometimes the test expectation is wrong and needs updating after legitimate code changes.

5Handle async operations properly

Ensure async operations complete before assertions run:

javascript
// WRONG: Assertion runs before async operation completes
function testAsync() {
  let result;
  fetchData().then(data => result = data);
  assert.strictEqual(result, 'expected');  // FAILS - result is undefined
}

// CORRECT: Use async/await
async function testAsyncCorrect() {
  const result = await fetchData();
  assert.strictEqual(result, 'expected');  // Waits for data
}

// OR: Use callbacks/promises properly
function testAsyncCallback(done) {
  fetchData().then(data => {
    assert.strictEqual(data, 'expected');
    done();
  });
}

Most test frameworks (Jest, Mocha) support async tests natively.

Advanced notes

Floating Point Comparisons

When comparing floating point numbers, use a tolerance-based approach instead of strict equality:

javascript
function assertAlmostEqual(actual, expected, tolerance = 1e-10) {
  assert.ok(Math.abs(actual - expected) < tolerance,
    `Expected ${actual} to be close to ${expected}`);
}

assertAlmostEqual(0.1 + 0.2, 0.3);  // Handles floating point precision

Custom Error Messages

Provide descriptive custom messages to make failures easier to debug:

javascript
assert.strictEqual(
  user.status,
  'active',
  `User ${user.id} should be active but is ${user.status}`
);

Strict Mode vs Legacy Mode

Always use strict assertion methods (strictEqual, deepStrictEqual, throws, etc.) instead of legacy methods (equal, deepEqual). Strict mode is recommended by Node.js and provides:
- More accurate comparisons
- Better error messages with visual diffs
- Consistency with modern JavaScript practices

Object.is() Semantics

assert.strictEqual() uses Object.is() internally, which treats NaN === NaN as true and +0 === -0 as false, differing slightly from ===:

javascript
assert.strictEqual(NaN, NaN);     // Passes (Object.is)
assert.strictEqual(+0, -0);       // Fails (Object.is distinguishes)
assert.strictEqual(0 === -0);     // true (regular ===)

Frequently asked questions

What does "ERR_ASSERTION" mean?
js's built-in assert module when an assertion test fails. strictEqual(), this error indicates that two values are not strictly equal according to the === operator. strictEqual() performs strict equality checking, meaning both the value AND type must match.
How do I fix "ERR_ASSERTION"?
This error occurs when a test assertion fails using Node.js assert.strictEqual(), indicating that the actual value does not match the expected value using strict equality (===).
What causes "ERR_ASSERTION"?
Comparing values with different types (e.g., number 1 vs string "1") Actual test result differs from expected value due to logic errors Using strictEqual to compare objects or arrays by reference instead of by content
Is the "ERR_ASSERTION" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading