Skip to content

Commit 0ea5763

Browse files
committed
Only treat native errors as errors
* Remove is-error dependency * Document edge case where `error instanceof Error` can be true, yet AVA does not recognize `error` as an error See also #2911 for an earlier attempt.
1 parent beff6b2 commit 0ea5763

File tree

6 files changed

+23
-12
lines changed

6 files changed

+23
-12
lines changed

docs/08-common-pitfalls.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ Translations: [Français](https://github.com/avajs/ava-docs/blob/main/fr_FR/docs
44

55
If you use [ESLint](https://eslint.org), you can install [eslint-plugin-ava](https://github.com/avajs/eslint-plugin-ava). It will help you use AVA correctly and avoid some common pitfalls.
66

7+
## Error edge cases
8+
9+
The `throws()` and `throwsAsync()` assertions use the Node.js built-in [`isNativeError()`](https://nodejs.org/api/util.html#utiltypesisnativeerrorvalue) to determine whether something is an error. This only recognizes actual instances of `Error` (and subclasses).
10+
11+
Note that the following is not a native error:
12+
13+
```js
14+
const error = Object.create(Error.prototype);
15+
```
16+
17+
This can be surprising, since `error instanceof Error` returns `true`.
18+
719
## AVA in Docker
820

921
If you run AVA in Docker as part of your CI, you need to fix the appropriate environment variables. Specifically, adding `-e CI=true` in the `docker exec` command. See [#751](https://github.com/avajs/ava/issues/751).

lib/assert.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import {isNativeError} from 'node:util/types';
2+
13
import concordance from 'concordance';
2-
import isError from 'is-error';
34
import isPromise from 'is-promise';
45

56
import concordanceOptions from './concordance-options.js';
@@ -163,7 +164,7 @@ function validateExpectations(assertion, expectations, numberArgs) { // eslint-d
163164
// Note: this function *must* throw exceptions, since it can be used
164165
// as part of a pending assertion for promises.
165166
function assertExpectations({assertion, actual, expectations, message, prefix, savedError}) {
166-
if (!isError(actual)) {
167+
if (!isNativeError(actual)) {
167168
throw new AssertionError({
168169
assertion,
169170
message,

lib/serialize-error.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {isNativeError} from 'node:util/types';
55

66
import cleanYamlObject from 'clean-yaml-object';
77
import concordance from 'concordance';
8-
import isError from 'is-error';
98
import StackUtils from 'stack-utils';
109

1110
import {AssertionError} from './assert.js';
@@ -160,7 +159,7 @@ export function tagWorkerError(error) {
160159
const isWorkerError = error => workerErrors.has(error);
161160

162161
export default function serializeError(origin, shouldBeautifyStack, error, testFile) {
163-
if (!isError(error) && !isWorkerError(error)) {
162+
if (!isNativeError(error) && !isWorkerError(error)) {
164163
return {
165164
avaAssertionError: false,
166165
nonErrorObject: true,

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@
104104
"globby": "^13.2.1",
105105
"ignore-by-default": "^2.1.0",
106106
"indent-string": "^5.0.0",
107-
"is-error": "^2.2.2",
108107
"is-plain-object": "^5.0.0",
109108
"is-promise": "^4.0.0",
110109
"matcher": "^5.0.0",

types/assertions.d.cts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ export type Assertions = {
103103
snapshot: SnapshotAssertion;
104104

105105
/**
106-
* Assert that the function throws [an error](https://www.npmjs.com/package/is-error). If so, returns the error value.
106+
* Assert that the function throws a native error. If so, returns the error value.
107107
*/
108108
throws: ThrowsAssertion;
109109

110110
/**
111-
* Assert that the async function throws [an error](https://www.npmjs.com/package/is-error), or the promise rejects
111+
* Assert that the async function throws a native error, or the promise rejects
112112
* with one. If so, returns a promise for the error value, which must be awaited.
113113
*/
114114
throwsAsync: ThrowsAsyncAssertion;
@@ -295,7 +295,7 @@ export type SnapshotAssertion = {
295295

296296
export type ThrowsAssertion = {
297297
/**
298-
* Assert that the function throws [an error](https://www.npmjs.com/package/is-error). If so, returns the error value.
298+
* Assert that the function throws a native error. If so, returns the error value.
299299
* The error must satisfy all expectations. Returns undefined when the assertion fails.
300300
*/
301301
<ErrorType extends ErrorConstructor | Error>(fn: () => any, expectations?: ThrowsExpectation<ErrorType>, message?: string): ThrownError<ErrorType> | undefined;
@@ -306,13 +306,13 @@ export type ThrowsAssertion = {
306306

307307
export type ThrowsAsyncAssertion = {
308308
/**
309-
* Assert that the async function throws [an error](https://www.npmjs.com/package/is-error). If so, returns the error
309+
* Assert that the async function throws a native error. If so, returns the error
310310
* value. Returns undefined when the assertion fails. You must await the result. The error must satisfy all expectations.
311311
*/
312312
<ErrorType extends ErrorConstructor | Error>(fn: () => PromiseLike<any>, expectations?: ThrowsExpectation<ErrorType>, message?: string): Promise<ThrownError<ErrorType> | undefined>;
313313

314314
/**
315-
* Assert that the promise rejects with [an error](https://www.npmjs.com/package/is-error). If so, returns the
315+
* Assert that the promise rejects with a native error. If so, returns the
316316
* rejection reason. Returns undefined when the assertion fails. You must await the result. The error must satisfy all
317317
* expectations.
318318
*/

0 commit comments

Comments
 (0)