Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: avajs/ava
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v5.1.1
Choose a base ref
...
head repository: avajs/ava
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v5.2.0
Choose a head ref
  • 3 commits
  • 6 files changed
  • 2 contributors

Commits on Jan 29, 2023

  1. State that documentation assumes ES modules

    (And update the sample version number because why not.)
    novemberborn authored Jan 29, 2023
    Copy the full SHA
    8415261 View commit details

Commits on Feb 6, 2023

  1. Copy the full SHA
    7559613 View commit details
  2. 5.2.0

    novemberborn committed Feb 6, 2023
    Copy the full SHA
    1353b08 View commit details
Showing with 56 additions and 18 deletions.
  1. +2 −2 package-lock.json
  2. +1 −1 package.json
  3. +5 −2 readme.md
  4. +18 −3 test-types/import-in-cts/throws.cts
  5. +18 −3 test-types/module/throws.ts
  6. +12 −7 types/assertions.d.cts
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ava",
"version": "5.1.1",
"version": "5.2.0",
"description": "Node.js test runner that lets you develop with confidence.",
"license": "MIT",
"repository": "avajs/ava",
7 changes: 5 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -45,11 +45,12 @@ Your `package.json` will then look like this (exact version notwithstanding):
```json
{
"name": "awesome-package",
"type": "module",
"scripts": {
"test": "ava"
},
"devDependencies": {
"ava": "^1.0.0"
"ava": "^5.0.0"
}
}
```
@@ -72,7 +73,9 @@ Don't forget to configure the `test` script in your `package.json` as per above.

### Create your test file

Create a file named `test.js` in the project root directory:
Create a file named `test.js` in the project root directory.

_Note that AVA's documentation assumes you're using ES modules._

```js
import test from 'ava';
21 changes: 18 additions & 3 deletions test-types/import-in-cts/throws.cts
Original file line number Diff line number Diff line change
@@ -12,15 +12,30 @@ class CustomError extends Error {
}

test('throws', t => {
expectType<Error | undefined>(t.throws(() => {}));
const error1 = t.throws(() => {});
expectType<Error | undefined>(error1);
const error2: CustomError | undefined = t.throws(() => {});
expectType<CustomError | undefined>(error2);
expectType<CustomError | undefined>(t.throws<CustomError>(() => {}));
const error3 = t.throws(() => {}, {instanceOf: CustomError});
expectType<CustomError | undefined>(error3);
const error4 = t.throws(() => {}, {is: new CustomError()});
expectType<CustomError | undefined>(error4);
const error5 = t.throws(() => {}, {instanceOf: CustomError, is: new CustomError()});
expectType<CustomError | undefined>(error5);
});

test('throwsAsync', async t => {
expectType<Error | undefined>(await t.throwsAsync(async () => {}));
const error1 = await t.throwsAsync(async () => {});
expectType<Error | undefined>(error1);
expectType<CustomError | undefined>(await t.throwsAsync<CustomError>(async () => {}));
expectType<Error | undefined>(await t.throwsAsync(Promise.reject()));
const error2 = await t.throwsAsync(Promise.reject());
expectType<Error | undefined>(error2);
expectType<CustomError | undefined>(await t.throwsAsync<CustomError>(Promise.reject()));
const error3 = await t.throwsAsync(async () => {}, {instanceOf: CustomError});
expectType<CustomError | undefined>(error3);
const error4 = await t.throwsAsync(async () => {}, {is: new CustomError()});
expectType<CustomError | undefined>(error4);
const error5 = await t.throwsAsync(async () => {}, {instanceOf: CustomError, is: new CustomError()});
expectType<CustomError | undefined>(error5);
});
21 changes: 18 additions & 3 deletions test-types/module/throws.ts
Original file line number Diff line number Diff line change
@@ -12,15 +12,30 @@ class CustomError extends Error {
}

test('throws', t => {
expectType<Error | undefined>(t.throws(() => {}));
const error1 = t.throws(() => {});
expectType<Error | undefined>(error1);
const error2: CustomError | undefined = t.throws(() => {});
expectType<CustomError | undefined>(error2);
expectType<CustomError | undefined>(t.throws<CustomError>(() => {}));
const error3 = t.throws(() => {}, {instanceOf: CustomError});
expectType<CustomError | undefined>(error3);
const error4 = t.throws(() => {}, {is: new CustomError()});
expectType<CustomError | undefined>(error4);
const error5 = t.throws(() => {}, {instanceOf: CustomError, is: new CustomError()});
expectType<CustomError | undefined>(error5);
});

test('throwsAsync', async t => {
expectType<Error | undefined>(await t.throwsAsync(async () => {}));
const error1 = await t.throwsAsync(async () => {});
expectType<Error | undefined>(error1);
expectType<CustomError | undefined>(await t.throwsAsync<CustomError>(async () => {}));
expectType<Error | undefined>(await t.throwsAsync(Promise.reject()));
const error2 = await t.throwsAsync(Promise.reject());
expectType<Error | undefined>(error2);
expectType<CustomError | undefined>(await t.throwsAsync<CustomError>(Promise.reject()));
const error3 = await t.throwsAsync(async () => {}, {instanceOf: CustomError});
expectType<CustomError | undefined>(error3);
const error4 = await t.throwsAsync(async () => {}, {is: new CustomError()});
expectType<CustomError | undefined>(error4);
const error5 = await t.throwsAsync(async () => {}, {instanceOf: CustomError, is: new CustomError()});
expectType<CustomError | undefined>(error5);
});
19 changes: 12 additions & 7 deletions types/assertions.d.cts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
export type ErrorConstructor = new (...args: any[]) => Error;
export type ErrorConstructor<ErrorType extends Error = Error> = {
new (...args: any[]): ErrorType;
readonly prototype: ErrorType;
}

export type ThrownError<ErrorType extends ErrorConstructor | Error> = ErrorType extends ErrorConstructor ? ErrorType['prototype'] : ErrorType;

/** Specify one or more expectations the thrown error must satisfy. */
export type ThrowsExpectation = {
export type ThrowsExpectation<ErrorType extends ErrorConstructor | Error> = {
/** The thrown error must have a code that equals the given string or number. */
code?: string | number;

/** The thrown error must be an instance of this constructor. */
instanceOf?: ErrorConstructor;
instanceOf?: ErrorType extends ErrorConstructor ? ErrorType : ErrorType extends Error ? ErrorConstructor<ErrorType> : never;

/** The thrown error must be strictly equal to this value. */
is?: Error;
is?: ErrorType extends ErrorConstructor ? ErrorType['prototype'] : ErrorType;

/** The thrown error must have a message that equals the given string, or matches the regular expression. */
message?: string | RegExp | ((message: string) => boolean);
@@ -293,7 +298,7 @@ export type ThrowsAssertion = {
* Assert that the function throws [an error](https://www.npmjs.com/package/is-error). If so, returns the error value.
* The error must satisfy all expectations. Returns undefined when the assertion fails.
*/
<ThrownError extends Error>(fn: () => any, expectations?: ThrowsExpectation, message?: string): ThrownError | undefined;
<ErrorType extends ErrorConstructor | Error>(fn: () => any, expectations?: ThrowsExpectation<ErrorType>, message?: string): ThrownError<ErrorType> | undefined;

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

/**
* Assert that the promise rejects with [an error](https://www.npmjs.com/package/is-error). If so, returns the
* rejection reason. Returns undefined when the assertion fails. You must await the result. The error must satisfy all
* expectations.
*/
<ThrownError extends Error>(promise: PromiseLike<any>, expectations?: ThrowsExpectation, message?: string): Promise<ThrownError | undefined>;
<ErrorType extends ErrorConstructor | Error>(promise: PromiseLike<any>, expectations?: ThrowsExpectation<ErrorType>, message?: string): Promise<ThrownError<ErrorType> | undefined>;

/** Skip this assertion. */
skip(thrower: any, expectations?: any, message?: string): void;