-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Make assertions throw #3201
Comments
I'm thinking of trying to implement this after #3233 is merged in. Is there a preferred way of setting up options for this? The only thing I'm worried about is that it'll be difficult to make the typescript declarations swap with an option. What would you think about making a separate import for tests with throwing? It would be a bit more involved for users to make that the default in the next major version: a regex search and replace
|
I would make the breaking change now and release a new major AVA version that also targets Node.js 16. I don't think the work to make it opt-in first and duplicate a lot of code is worth it. But it's up to @novemberborn. |
There’s a couple things around errors I want to get into AVA 6, and actually I’d then drop Node.js 16 support before release. If this lands in time we can include it without a config option. Implementation wise I think failed assertions should throw a Tests already fail if an error is thrown. I think the twist is that maybe we record the |
@novemberborn, I found out something that is problematic: Assertions in typescript require explicit type annotations for all parts of the call: microsoft/TypeScript#45385, microsoft/TypeScript#33622 test('falsy', (t)=>t.assert(1)); // Typescript Error
test('falsy', (t: ExecutionContext)=>t.assert(1)); // works correctly The reason typescript complains is because From a type-definitions approach, the easiest way I can think of is that we just have a top level However, from a coding standpoint from the little I've looked at the codebase, doing that will likely break a ton of things including the ability to count how many assertions are used for |
That's too big a change. Is this not worthwhile then? Is it better in TS5.2? |
It's not better in TS5.2 (nor the nightly version). I'm not sure if they will ever relax that restriction on assertions in TS. There's been no indication of them even being interested in doing so in the issues I've looked through, so I wouldn't get my hopes up on them changing it. |
It looks like a lot of this was discussed before back in 2020: #2449 From my perspective, I think that making the assertions throw without the typescript asserts would be very counter intuitive for anyone using types. From playing around with things, I realized that it's possible to just make an import assert from 'node:assert';
//...
const value: unknown = 3;
assert(t.is(value,3));
value // is of type `3` So, by making an |
When we make assertions throw errors, function overloads that return never should do the trick without needing type assertions. |
I don't think that'll be sufficient. The It would also be unable to narrow types at all, which would realistically make worrying about the typescript entirely pointless. Example. Plus if your types don't match the specific overloads we setup correctly enough, then you'll get a ton of "unreachable code" errors. Any attempt at figuring out what |
Thanks for the example. We're not considering any changes beyond having the assertions throw. Sounds like #3233 should be reverted then? |
I don't see a need to revert it, nothing in that PR changes anything for this issue. I did some double checking with the Sadly, there's no way to make the behavior an override (without an extra parameter or something) as whichever override is defined first will apply regardless of the explicit vs inferred ExecutionContext type. I would personally prefer having to explicitly add the |
Fixes #3201. 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 `throws` and `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.
Fixes #3201. 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 `throws` and `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.
Fixes #3201. 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 `throws` and `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.
Fixes #3201. 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 `throws` and `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.
Currently, the AVA assertions don't throw. This has a lot of downsides:
Lets make them throw again. We can start by making it opt-in and then change the default in the next major version.
Related: #1485
The text was updated successfully, but these errors were encountered: