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

fix(runner): mark tests as failed when beforeAll/afterAll failed #4799

Closed
wants to merge 4 commits into from

Conversation

hi-ogawa
Copy link
Contributor

@hi-ogawa hi-ogawa commented Dec 23, 2023

Description

Closes #4516

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

Copy link

netlify bot commented Dec 23, 2023

Deploy Preview for fastidious-cascaron-4ded94 canceled.

Name Link
🔨 Latest commit 8a3dad6
🔍 Latest deploy log https://app.netlify.com/sites/fastidious-cascaron-4ded94/deploys/65865837d35cb20008288da5

@hi-ogawa hi-ogawa changed the title fix(runner): mark tests as failed when beforeAll/afterAll failed fix(runner): mark tests as failed when beforeAll/afterAll failed Dec 23, 2023
@hi-ogawa hi-ogawa changed the title fix(runner): mark tests as failed when beforeAll/afterAll failed fix(runner)!: mark tests as failed when beforeAll/afterAll failed Dec 23, 2023
@hi-ogawa hi-ogawa changed the title fix(runner)!: mark tests as failed when beforeAll/afterAll failed fix(runner): mark tests as failed when beforeAll/afterAll failed Dec 23, 2023
@hi-ogawa hi-ogawa marked this pull request as ready for review December 23, 2023 03:58
}

function markTasksAsFailed(suite: Suite, errors: ErrorWithDiff[], runner: VitestRunner) {
for (const t of getTests(suite)) {
Copy link
Member

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. We have a suite.result and it's enough. I think reporters should handle this case if they don't distinguish between suite fails and test fails

Screenshot 2023-12-28 at 11 20 33

Copy link
Contributor Author

@hi-ogawa hi-ogawa Dec 28, 2023

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

Vitest UI screenshot

image

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:

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.

Copy link
Member

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 with beforeEach/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.

Copy link
Member

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

Copy link
Contributor Author

@hi-ogawa hi-ogawa Dec 28, 2023

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!

@hi-ogawa
Copy link
Contributor Author

I'll closed this PR in favor of #4819.


Within the discussion, you mentioned #4799 (comment)

What I would expect is for tests to have skip state if beforeAll failed (because we don't run any test)

and I agree that makes sense. I'll raise a dedicated issue for this, so it'll be discussed and handled separately.

@hi-ogawa hi-ogawa closed this Dec 29, 2023
@hi-ogawa hi-ogawa deleted the fix-suite-hook-error branch December 29, 2023 07:40
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.

JUNIT reporter doesn't mark test as failed if the beforeAll throws error
2 participants