Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer thrown error from expectations #3156

Merged

Conversation

tao-cumplido
Copy link
Contributor

Updates type definitions to infer thrown error type from given expectations.

Writing const error = t.throws<CustomError>(fn, { instanceOf: CustomError }); felt redundant and I thought there must be a way to infer the error type from the given instanceOf expectation. The tricky part was in making inference work for both instanceOf and is expectations.

I worked it out in this playground. It also shows that something like const error = t.throws<CustomError>(() => {}, { instanceOf: Error }); is rejected by TS (if CustomError is structurally different to Error).

@novemberborn
Copy link
Member

Nice work @tao-cumplido!

@sindresorhus how does this look to you? Or do you know somebody we could ask to double check?

@sindresorhus
Copy link
Member

Looks correct to me.

@novemberborn novemberborn merged commit 7559613 into avajs:main Feb 6, 2023
@novemberborn
Copy link
Member

Thanks @tao-cumplido!

gr2m referenced this pull request in gr2m/github-project Dec 5, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [ava](https://avajs.dev) ([source](https://togithub.com/avajs/ava)) |
[`^5.0.0` ->
`^6.0.0`](https://renovatebot.com/diffs/npm/ava/5.0.1/6.0.0) |
[![age](https://developer.mend.io/api/mc/badges/age/npm/ava/6.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/ava/6.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/ava/5.0.1/6.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/ava/5.0.1/6.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>avajs/ava (ava)</summary>

### [`v6.0.0`](https://togithub.com/avajs/ava/releases/tag/v6.0.0)

[Compare Source](https://togithub.com/avajs/ava/compare/v5.3.1...v6.0.0)

#### Breaking Changes

- AVA now requires Node.js versions 18.18, 20.8 or 21. Versions 14 and
16 are no longer supported.
[#&#8203;3251](https://togithub.com/avajs/ava/issues/3251)
[#&#8203;3216](https://togithub.com/avajs/ava/issues/3216)

- When tests finish, worker threads or child processes are no longer
exited through `proces.exit()`. If your test file does not exit on its
own, the test run will time out.
[#&#8203;3260](https://togithub.com/avajs/ava/issues/3260)

- Changes to watch mode
[#&#8203;3218](https://togithub.com/avajs/ava/issues/3218):
- Watch mode can no longer be started via the `ava.config.*` or
`package.json` configuration.
- The `ignoredByWatcher` configuration has moved to the `watchMode`
object, under the `ignoreChanges` key.
- Watch mode now uses the built-in
[`fs.watch()`](https://nodejs.org/api/fs.html#fswatchfilename-options-listener)
in recursive mode. This is supported on Linux in Node.js 20 or newer,
and MacOS and Windows in Node.js 18 as well. There are
[caveats](https://nodejs.org/api/fs.html#caveats) to keep in mind.

- Failed assertions now throw, meaning that any subsequent code is not
executed. This also impacts the type definitions.
[#&#8203;3246](https://togithub.com/avajs/ava/issues/3246)

- [Only native
errors](https://nodejs.org/api/util.html#utiltypesisnativeerrorvalue)
are now considered errors by the `t.throws()` and `t.throwsAsync()`
assertions. [`Object.create(Error.prototype)` is **not** a native
error](Object.create\(Error.prototype\)).
[#&#8203;3229](https://togithub.com/avajs/ava/issues/3229)

- Changes to modules loaded through the `require` configuration
[#&#8203;3184](https://togithub.com/avajs/ava/issues/3184):
- If such modules export a default function, this function is now
invoked.
    -   Local files are loaded through `@ava/typescript` if necessary.

#### Improvements

##### Rewritten watcher

The watcher has been rewritten. It’s now built on
[`fs.watch()`](https://nodejs.org/api/fs.html#fswatchfilename-options-listener)
in recursive mode.

[`@vercel/nft`](https://togithub.com/vercel/nft) is used to perform
static dependency analysis, supporting ESM and CJS imports for
JavaScript & TypeScript source files. This is a huge improvement over
the previous runtime tracking of CJS imports, which did not support ESM.

Integration with
[`@ava/typescript`](https://togithub.com/avajs/typescript) has been
improved. The watcher can now detect a change to a TypeScript source
file, then wait for the corresponding build output to change before
re-running tests.

The ignoredByWatcher configuration has moved to the watchMode object,
under the ignoreChanges key.

See [#&#8203;3218](https://togithub.com/avajs/ava/issues/3218) and
[#&#8203;3257](https://togithub.com/avajs/ava/issues/3257).

##### Failed assertions now throw

Assertions now throw a `TestFailure` error when they fail. This error is
not exported or documented and should not be used or thrown manually.
You cannot catch this error in order to recover from a failure, use
`t.try()` instead.

All assertions except for `t.throws()` and `t.throwsAsync()` now return
`true` when they pass. This is useful for some of the assertions in
TypeScript where they can be used as a type guard.

Committing a failed `t.try()` result now also throws.

See [#&#8203;3246](https://togithub.com/avajs/ava/issues/3246).

##### `t.throws()` and `t.throwsAsync()` can now expect any error

By default, the thrown error (or rejection reason) must be a native
error. You can change the assertion to expect any kind of error by
setting `any: true` in the expectation object:

```js
t.throws(() => { throw 'error' }, {any: true})
```

See [#&#8203;3245](https://togithub.com/avajs/ava/issues/3245) by
[@&#8203;adiSuper94](https://togithub.com/adiSuper94).

##### The `require` configuration is now more powerful

It now loads ES modules.

Local files are loaded through `@ava/typescript` if necessary, so you
can also write these in TypeScript.

If there is a default export function, it is invoked after loading. The
function is awaited so it can do asynchronous setup before further
modules are loaded. Arguments from the configuration can be passed to
the function (as a \[[structured
clone](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone)]\(https://developer.mozilla.org/en-US/docs/Web/API/structuredClone)).

See [#&#8203;3184](https://togithub.com/avajs/ava/issues/3184) by
[@&#8203;sculpt0r](https://togithub.com/sculpt0r).

##### Other changes worth noting

- Internal events can now be observed (experimentally). See
[#&#8203;3247](https://togithub.com/avajs/ava/issues/3247) by
[@&#8203;codetheweb](https://togithub.com/codetheweb). It’s experimental
and undocumented.
- You can now use `t.timeout.clear()` to restore a previous
`t.timeout()`.
[#&#8203;3221](https://togithub.com/avajs/ava/issues/3221)
- Code coverage is flushed to disk at opportune moments.
[#&#8203;3220](https://togithub.com/avajs/ava/issues/3220)

#### New Contributors

- [@&#8203;sculpt0r](https://togithub.com/sculpt0r) made their first
contribution in
[https://github.com/avajs/ava/pull/3184](https://togithub.com/avajs/ava/pull/3184)
- [@&#8203;ZachHaber](https://togithub.com/ZachHaber) made their first
contribution in
[https://github.com/avajs/ava/pull/3233](https://togithub.com/avajs/ava/pull/3233)
- [@&#8203;adiSuper94](https://togithub.com/adiSuper94) made their first
contribution in
[https://github.com/avajs/ava/pull/3245](https://togithub.com/avajs/ava/pull/3245)
- [@&#8203;bricker](https://togithub.com/bricker) made their first
contribution in
[https://github.com/avajs/ava/pull/3250](https://togithub.com/avajs/ava/pull/3250)

**Full Changelog**: avajs/ava@v5.3.1...v6.0.0

### [`v5.3.1`](https://togithub.com/avajs/ava/releases/tag/v5.3.1)

[Compare Source](https://togithub.com/avajs/ava/compare/v5.3.0...v5.3.1)

#### What's Changed

- Update `t.like()` to support Symbol keys and ignore non-enumerable
properties by [@&#8203;gibson042](https://togithub.com/gibson042) in
[https://github.com/avajs/ava/pull/3209](https://togithub.com/avajs/ava/pull/3209)
- Fix circular selector detection in `t.like()` by
[@&#8203;novemberborn](https://togithub.com/novemberborn) in
[https://github.com/avajs/ava/pull/3212](https://togithub.com/avajs/ava/pull/3212)

**Full Changelog**: avajs/ava@v5.3.0...v5.3.1

### [`v5.3.0`](https://togithub.com/avajs/ava/releases/tag/v5.3.0)

[Compare Source](https://togithub.com/avajs/ava/compare/v5.2.0...v5.3.0)

#### What's Changed

- Support arrays in `t.like()` assertions by
[@&#8203;tommy-mitchell](https://togithub.com/tommy-mitchell) in
[https://github.com/avajs/ava/pull/3185](https://togithub.com/avajs/ava/pull/3185)
- Recognize typical assertion errors (`expect` and `assert`) and use
their formatting by [@&#8203;Irvenae](https://togithub.com/Irvenae) in
[https://github.com/avajs/ava/pull/3187](https://togithub.com/avajs/ava/pull/3187)

#### New Contributors

- [@&#8203;MartynasZilinskas](https://togithub.com/MartynasZilinskas)
made their first contribution in
[https://github.com/avajs/ava/pull/3172](https://togithub.com/avajs/ava/pull/3172)
- [@&#8203;flovogt](https://togithub.com/flovogt) made their first
contribution in
[https://github.com/avajs/ava/pull/3194](https://togithub.com/avajs/ava/pull/3194)
- [@&#8203;ondreian](https://togithub.com/ondreian) made their first
contribution in
[https://github.com/avajs/ava/pull/3192](https://togithub.com/avajs/ava/pull/3192)
- [@&#8203;tommy-mitchell](https://togithub.com/tommy-mitchell) made
their first contribution in
[https://github.com/avajs/ava/pull/3185](https://togithub.com/avajs/ava/pull/3185)
- [@&#8203;craigahobbs](https://togithub.com/craigahobbs) made their
first contribution in
[https://github.com/avajs/ava/pull/3198](https://togithub.com/avajs/ava/pull/3198)
- [@&#8203;Irvenae](https://togithub.com/Irvenae) made their first
contribution in
[https://github.com/avajs/ava/pull/3187](https://togithub.com/avajs/ava/pull/3187)

**Full Changelog**: avajs/ava@v5.2.0...v5.3.0

### [`v5.2.0`](https://togithub.com/avajs/ava/releases/tag/v5.2.0)

[Compare Source](https://togithub.com/avajs/ava/compare/v5.1.1...v5.2.0)

#### What's Changed

- Infer thrown error from expectations by
[@&#8203;tao-cumplido](https://togithub.com/tao-cumplido) in
[https://github.com/avajs/ava/pull/3156](https://togithub.com/avajs/ava/pull/3156)

#### New Contributors

- [@&#8203;tao-cumplido](https://togithub.com/tao-cumplido) made their
first contribution in
[https://github.com/avajs/ava/pull/3156](https://togithub.com/avajs/ava/pull/3156)

**Full Changelog**: avajs/ava@v5.1.1...v5.2.0

### [`v5.1.1`](https://togithub.com/avajs/ava/releases/tag/v5.1.1)

[Compare Source](https://togithub.com/avajs/ava/compare/v5.1.0...v5.1.1)

#### What's Changed

- Fix de-registration of shared workers to ensure AVA exits correctly,
by [@&#8203;codetheweb](https://togithub.com/codetheweb) in
[https://github.com/avajs/ava/pull/3149](https://togithub.com/avajs/ava/pull/3149)
&
[https://github.com/avajs/ava/pull/3151](https://togithub.com/avajs/ava/pull/3151)

**Full Changelog**: avajs/ava@v5.1.0...v5.1.1

### [`v5.1.0`](https://togithub.com/avajs/ava/releases/tag/v5.1.0)

[Compare Source](https://togithub.com/avajs/ava/compare/v5.0.1...v5.1.0)

##### What's Changed

- Output logs for tests that remain pending when AVA exits by
[@&#8203;kevo1ution](https://togithub.com/kevo1ution) in
[https://github.com/avajs/ava/pull/3125](https://togithub.com/avajs/ava/pull/3125)
- Check for --config file extensions after they fail to load, allowing
custom loaders by [@&#8203;panva](https://togithub.com/panva) in
[https://github.com/avajs/ava/pull/3135](https://togithub.com/avajs/ava/pull/3135)

##### New Contributors

- [@&#8203;kevo1ution](https://togithub.com/kevo1ution) made their first
contribution in
[https://github.com/avajs/ava/pull/3125](https://togithub.com/avajs/ava/pull/3125)
- [@&#8203;panva](https://togithub.com/panva) made their first
contribution in
[https://github.com/avajs/ava/pull/3135](https://togithub.com/avajs/ava/pull/3135)

**Full Changelog**: avajs/ava@v5.0.1...v5.1.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/gr2m/github-project).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy44MS4zIiwidXBkYXRlZEluVmVyIjoiMzcuODEuMyIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants