-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
fix(runner): mark tests as failed when beforeAll/afterAll
failed
#4799
Conversation
✅ Deploy Preview for fastidious-cascaron-4ded94 canceled.
|
beforeAll/afterAll
failed
beforeAll/afterAll
failedbeforeAll/afterAll
failed
beforeAll/afterAll
failedbeforeAll/afterAll
failed
} | ||
|
||
function markTasksAsFailed(suite: Suite, errors: ErrorWithDiff[], runner: VitestRunner) { | ||
for (const t of getTests(suite)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like doing this for every test because the terminal seems bloated
That sounds fair. To explain my perspective, the reason I went with this is that I thought beforeAll/afterAll
failures should be more aligned with beforeEach/afterEach
failures.
I experimented with current behavior on stackblitz:
https://stackblitz.com/edit/vitest-dev-vitest-m7bw4w?file=test%2Frepro.test.ts
For example, currently afterAll
failure would keep tests as success and I thought that's not an intended behavior.
To avoid cluttering terminal with a single error appearing in all tests, I think I could do something like this while still flagging tests as "fail":
t.result = {
...t.result,
// flag as "fail"
state: 'fail',
// but don't have to copy errors
// errors: [...t.result?.errors ?? [], ...errors],
}
What do you think?
EDIT: actually I did this first, then I realized currently junit reporter checks task.result.errors
to generate <failure>
tags. So, if we went with this, we still require modifying around this code:
vitest/packages/vitest/src/node/reporters/junit.ts
Lines 185 to 188 in 039814b
if (task.result?.state === 'fail') { | |
const errors = task.result.errors || [] | |
for (const error of errors) { | |
await this.writeElement('failure', { |
Okay, I'll also experiment with approach to fix only on junit reporter side.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To explain my perspective, the reason I went with this is that I thought
beforeAll
/afterAll
failures should be more aligned withbeforeEach
/afterEach
failures.
afterAll
/beforeAll
are not bound to any test, they are suite hooks. beforeEach
/afterEach
are test hooks. What I would expect is for tests to have skip
state if beforeAll
failed (because we don't run any test), and any state if afterAll
failed (only the suite is marked as failed)
For example, currently afterAll failure would keep tests as success and I thought that's not an intended behavior.
afterAll
fails only a suite which seems correct to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
afterAll fails only a suite which seems correct to me.
This is consistent with mocha
for example:
import { test, after } from 'mocha'
after(() => {
throw new Error('after')
})
test('test', () => {})
Although in mocha afterEach
doesn't fail the test while in Vitest it does.
If we are making it consistent, then 1) this is definitely a breaking change, 2) we need to decide what is consistent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
afterAll/beforeAll are not bound to any test, they are suite hooks. beforeEach/afterEach are test hooks.
Totally good point. I didn't have that understanding.
Okay, I'll see what I can do with junit reporter only first. Probably I'll close this PR and create a new one.
Thanks again for the review!
I'll closed this PR in favor of #4819. Within the discussion, you mentioned #4799 (comment)
and I agree that makes sense. I'll raise a dedicated issue for this, so it'll be discussed and handled separately. |
Description
Closes #4516
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
pnpm-lock.yaml
unless you introduce a new test example.Tests
pnpm test:ci
.Documentation
pnpm run docs
command.Changesets
feat:
,fix:
,perf:
,docs:
, orchore:
.