jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.23k stars 6.46k forks source link

[Bug]: AssertionError is not instanceof Error #15269

Closed tqhoughton closed 1 week ago

tqhoughton commented 2 months ago

Version

29.7.0

Steps to reproduce

Use the following jest.config.js:

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node'
}

Simply create the following test in your node env project:

import { AssertionError } from 'assert'

describe('AssertionError', () =>
  it('should extend the base Error class', () => {
    const foo = new AssertionError({})
    expect(foo instanceof Error).toBeTruthy()
    expect(foo).toBeInstanceOf(AssertionError)
  })
})

Both assertions will fail even though in NodeJS v20 LTS the condition (foo instanceof Error) will resolve to true

Expected behavior

I expect AssertionErrors to extend the standard Error class as they do in the latest LTS version of NodeJS and pass instanceof checks.

Actual behavior

AssertionErrors do not extend the base Error class in NodeJS when running in a jest context.

Additional context

No response

Environment

System:
    OS: macOS 14.6.1
    CPU: (8) arm64 Apple M1 Pro
  Binaries:
    Node: 20.11.1 - ~/.nvm/versions/node/v20.11.1/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v20.11.1/bin/yarn
    npm: 10.2.4 - ~/.nvm/versions/node/v20.11.1/bin/npm
  npmPackages:
    jest: ^29.7.0 => 29.7.0
soren121 commented 2 months ago

Duplicate of #2549

github-actions[bot] commented 1 month ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] commented 1 week ago

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.