Skip to content

Assertion code parsing does not occur using TestContext's assert. #54760

@avivkeller

Description

@avivkeller

The following code snippet:

import test from 'node:test';
import assert from 'node:assert';

await test('foo', t => {
  assert.ok("Hello" === "World");
});

Produces the following output:

✖ foo (27.753865ms)
  AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
  
    assert.ok("Hello" === "World")
  
      at TestContext.<anonymous> (file:///repro.mjs:5:10)
      at Test.runInAsyncScope (node:async_hooks:211:14)
      at Test.run (node:internal/test_runner/test:887:25)
      at Test.start (node:internal/test_runner/test:786:17)
      at startSubtestAfterBootstrap (node:internal/test_runner/harness:280:17)
      at async file:///repro.mjs:4:1 {
    generatedMessage: true,
    code: 'ERR_ASSERTION',
    actual: false,
    expected: true,
    operator: '=='
  }

ℹ tests 1
ℹ suites 0
ℹ pass 0
ℹ fail 1
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 41.578387

✖ failing tests:

test at repro.mjs:4:7
✖ foo (27.753865ms)
  AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
  
    assert.ok("Hello" === "World")
  
      at TestContext.<anonymous> (file:///repro.mjs:5:10)
      at Test.runInAsyncScope (node:async_hooks:211:14)
      at Test.run (node:internal/test_runner/test:887:25)
      at Test.start (node:internal/test_runner/test:786:17)
      at startSubtestAfterBootstrap (node:internal/test_runner/harness:280:17)
      at async file:///repro.mjs:4:1 {
    generatedMessage: true,
    code: 'ERR_ASSERTION',
    actual: false,
    expected: true,
    operator: '=='
  }

Notice how the error message includes the failing assertion assert.ok("Hello" === "World").

In contrast, the following code snippet:

import test from 'node:test';
import assert from 'node:assert';

await test('foo', t => {
  t.assert.ok("Hello" === "World");
});

Results in a much less descriptive error:

✖ foo (9.450763ms)
  AssertionError [ERR_ASSERTION]: false == true
      at assert.<computed> [as ok] (node:internal/test_runner/test:263:18)
      at TestContext.<anonymous> (file:///repro.mjs:5:12)
      at Test.runInAsyncScope (node:async_hooks:211:14)
      at Test.run (node:internal/test_runner/test:887:25)
      at Test.start (node:internal/test_runner/test:786:17)
      at startSubtestAfterBootstrap (node:internal/test_runner/harness:280:17)
      at async file:///repro.mjs:4:1 {
    generatedMessage: true,
    code: 'ERR_ASSERTION',
    actual: false,
    expected: true,
    operator: '=='
  }

ℹ tests 1
ℹ suites 0
ℹ pass 0
ℹ fail 1
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 37.600526

✖ failing tests:

test at repro.mjs:4:7
✖ foo (9.450763ms)
  AssertionError [ERR_ASSERTION]: false == true
      at assert.<computed> [as ok] (node:internal/test_runner/test:263:18)
      at TestContext.<anonymous> (file:///repro.mjs:5:12)
      at Test.runInAsyncScope (node:async_hooks:211:14)
      at Test.run (node:internal/test_runner/test:887:25)
      at Test.start (node:internal/test_runner/test:786:17)
      at startSubtestAfterBootstrap (node:internal/test_runner/harness:280:17)
      at async file:///repro.mjs:4:1 {
    generatedMessage: true,
    code: 'ERR_ASSERTION',
    actual: false,
    expected: true,
    operator: '=='
  }

Notice that this output lacks the detailed error message with the specific failing assertion.

Ref: nodejs/help#4461

Metadata

Metadata

Assignees

No one assigned

    Labels

    assertIssues and PRs related to the assert subsystem.test_runnerIssues and PRs related to the test runner subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions