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): Ensure test.each print -0 and -NaN properly #5806

Merged
merged 6 commits into from
Jun 3, 2024

Conversation

pengooseDev
Copy link
Contributor

Description

Fixes issue #5744
Prev PR(closed)

This bug fix ensures that when using test.each, the descriptive text for -0 and -NaN is correctly printed as -0 and -NaN instead of 0 and NaN.


Context

The logic to resolve this issue was moved from format (utils) to the formatTitle(runner.ts) function. The function now checks if the value of %f in the template is -0 or -NaN using an ArrayBuffer and formats it appropriately.

However, despite various attempts, it was not possible to write a proper regression test for the formatTitle function in suite.ts without exporting the function. Additionally, no test suite for formatTitle was found.

To get feedback on the test, I am creating this PR with the following considerations:

  1. export formatTitle for testing purposes (which I believe might be a bit risky).
  2. Manually verifying without adding a test suite.
  3. Any other alternatives.
  • Below is the screenshot for reference regarding the second approach. (Code Didn't added)

Tests

  describe('edge cases', () => {
    describe('check for single values', () => {
      test.each([[0], [-0], [Number.NaN], [-Number.NaN], [Number.POSITIVE_INFINITY], [Number.NEGATIVE_INFINITY]] as const)(
        '%f',
        () => {
          throw new Error('test')
        },
      )
    })

    describe('check for multiple values(2)', () => {
      test.each([
        [0, Number.NaN],
        [-0, Number.NaN],
        [Number.NaN, -Number.NaN],
        [-0, -Number.NaN],
      ] as const)('%f %f', () => {
        throw new Error('test')
      })
    })

    describe('check for multiple values(3)', () => {
      test.each([
        [0, Number.NaN, 0],
        [0, -Number.NaN, 0],
        [-0, Number.NaN, 0],
        [-0, -Number.NaN, 0],
        [0, Number.NaN, -0],
        [0, -Number.NaN, -0],
        [-0, Number.NaN, -0],
        [-0, -Number.NaN, -0],
        [Number.NaN, -Number.NaN, 1],
        [-0, -Number.NaN, 1],
      ] as const)('%f %f %f', () => {
        throw new Error('test')
      })
    })

    describe('check for multiple values(4)', () => {
      test.each([
        [0, Number.NaN, 0, 0],
        [0, -Number.NaN, 0, 0],
        [-0, Number.NaN, 0, 0],
        [-0, -Number.NaN, 0, 0],
        [0, Number.NaN, -0, 0],
        [0, -Number.NaN, -0, 0],
        [-0, Number.NaN, -0, 0],
        [-0, -Number.NaN, -0, 0],
        [Number.NaN, -Number.NaN, -Number.NaN, 0],
      ] as const)(' %f %f %f %f', () => {
        throw new Error('test')
      })
    })
  })

check for single values: ✓

image

check for multiple values(2): ✓

image

check for multiple values(3): ✓

image

check for multiple values(4): ✓

image


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:.

@sheremet-va sheremet-va merged commit 9ac8ff9 into vitest-dev:main Jun 3, 2024
16 checks passed
@injae-kim
Copy link

Nice improvement :)

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.

test.each does not print -0 and -NaN correctly
3 participants