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(core): test change should trigger failed test rerun #3773

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

btea
Copy link
Contributor

@btea btea commented Jul 15, 2023

fix #3687

@stackblitz
Copy link

stackblitz bot commented Jul 15, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@netlify
Copy link

netlify bot commented Jul 15, 2023

Deploy Preview for fastidious-cascaron-4ded94 canceled.

Name Link
🔨 Latest commit 0d017c1
🔍 Latest deploy log https://app.netlify.com/sites/fastidious-cascaron-4ded94/deploys/64b543429c07560008f0d776

Copy link
Member

@AriPerkkio AriPerkkio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Add test case to test/watch/test/stdin.test.ts
  • The implementation doesn't seem to consider the filter enabled by pressing f? This PR should only affect that use case.

@btea
Copy link
Contributor Author

btea commented Jul 15, 2023

  • The implementation doesn't seem to consider the filter enabled by pressing f? This PR should only affect that use case.

Maybe I misunderstood you guys? I thought that as long as the test content is modified, if there is a failed test case, it will automatically rerun.

I seem to understand that only after pressing f, if the content of the test file is modified, the failed test will be automatically run, is it?

Copy link
Member

@AriPerkkio AriPerkkio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I misunderstood you guys? I thought that as long as the test content is modified, if there is a failed test case, it will automatically rerun.

I seem to understand that only after pressing f, if the content of the test file is modified, the failed test will be automatically run, is it?

The issue contains two bugs:

  • When user has activated "Run only failed test"-filter in watch mode by pressing f, this filter does not preserve if files are modified
  • The "Run only failed test"-filter runs all test cases inside test files. It should run only failed test cases. Now it's only filtering the files.

@@ -27,6 +27,40 @@ test('rerun current pattern tests', async () => {
await vitest.waitForStdout('1 passed')
})

test('rerun failed tests', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need a test case that fails in main, and is fixed by these changes. This test case does not fail on main branch.

Maybe something like below?

test('rerun failed tests', async () => {
  const testPath = 'fixtures/error.test.ts'
  const failAssertion = 'expect(1).toBe(2)'
  const testCase = `// Dynamic test case
  import { test, expect } from 'vitest'

  // Failure re-run should run this
  test('failing test', () => {
    ${failAssertion}
  })

  // Failure re-run should NOT run this
  test('passing test', () => {
    expect(1).toBe(1)
  })
  `

  writeFileSync(testPath, testCase, 'utf8')
  cleanups.push(() => rmSync(testPath))

  const vitest = await runVitestCli(...cliArgs)
  await vitest.waitForStdout('Test Files  1 failed | 2 passed (3)')
  await vitest.waitForStdout('Tests  1 failed | 3 passed (4)')

  // Re-run failed tests only
  vitest.write('f')

  // Only a single test should have run
  await vitest.waitForStdout('Test Files  1 failed (1)')

  // Only a single test case should have run
  await vitest.waitForStdout('Tests  1 failed (1)')

  // Fix failing test case
  writeFileSync(testPath, testCase.replace(failAssertion, ''), 'utf8')

  // Only the previously failed test file should run
  await vitest.waitForStdout('Test Files  1 pass (1)')

  // Only the previously failed test case should run
  await vitest.waitForStdout('Tests  1 pass (1)')
})

@@ -76,14 +76,20 @@ export function registerConsoleShortcuts(ctx: Vitest) {
if (name === 'u')
return ctx.updateSnapshot()
// rerun all tests
if (name === 'a' || name === 'return')
if (name === 'a' || name === 'return') {
ctx.isFailedModel = false
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 think we should set properties of ctx outside it like this. Could this filter be toggled on/off inside the core.ts instead?

But your approach is right in the way that we indeed need to save the failure filter in state.

@btea
Copy link
Contributor Author

btea commented Jul 21, 2023

The issue contains two bugs:

  • When user has activated "Run only failed test"-filter in watch mode by pressing f, this filter does not preserve if files are modified
  • The "Run only failed test"-filter runs all test cases inside test files. It should run only failed test cases. Now it's only filtering the files.

If I didn't understand the error, it means that if a test file has test cases that fail, after pressing f, only the failed suite in the test file is re-run instead of the entire test file and all cases are re-run? (If so, filter test cases in a test file, I don't know how to do it, can you give me a hint? Thanks.)

And if the content of the file is modified, the failed test will not be filtered, and the original operation logic can be followed.

@AriPerkkio
Copy link
Member

(If so, filter test cases in a test file, I don't know how to do it, can you give me a hint? Thanks.)

I'm not 100% sure, but the changes should be made somewhere around packages/runner I think.

@btea
Copy link
Contributor Author

btea commented Jul 30, 2023

Sorry, I can't seem to find any relevant instructions in the official documentation. Maybe I'm missing something. 🤔

@sheremet-va Can you give me a little hint? Thanks!

@sheremet-va
Copy link
Member

@sheremet-va Can you give me a little hint? Thanks!

I'm not sure, but I think you can set configOverride.testNamePattern to filter tests (this is what vscode does) - maybe we should improve this somehow internally 🤷🏻

@btea btea marked this pull request as draft July 2, 2024 02:42
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.

Watch mode "f" command does not rerun failed tests
3 participants