Skip to content

Feature: Add a mode to watch with the ability to update an expected literal #4442

@orta

Description

@orta

Do you want to request a feature or report a bug?

Feature

What is the current behavior?

it("is an incorrect test", () => {
  expect(4).toEqual(5)
})

With yarn jest -- --watch

 FAIL  src/_tests/_wrong.test.tsx
  ● is an incorrect test

    expect(received).toEqual(expected)

    Expected value to equal:
      5
    Received:
      4

      at Object.<anonymous>.it (src/_tests/_wrong.test.tsx:2:13)

  ✕ is an incorrect test (94ms)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        1.147s
Ran all test suites related to changed files.

Watch Usage
 › Press a to run all tests.
 › Press p to filter by a filename regex pattern.
 › Press t to filter by a test name regex pattern.
 › Press q to quit watch mode.
 › Press Enter to trigger a test run.

What is the potential behavior?

What if there was an extra watch mode:

Watch Usage
 › Press a to run all tests.
 › Press u to re-run tests and update failed number literals.
 › Press p to filter by a filename regex pattern.
 › Press t to filter by a test name regex pattern..
 › Press q to quit watch mode.
 › Press Enter to trigger a test run.

Where after pressing u it re-runs with whatever lasts triggered the test run ( so maybe the watchman announcement of a file being saved ) and outputs something like:

 UPDATE  src/_tests/_wrong.test.tsx
  ● is an incorrect test

    Expected value to equal:
      5
    Received:
      4

   Updated test code:

      it("is an incorrect test", () => {
--      expect(4).toEqual(5)
++      expect(4).toEqual(4)
      })

      at Object.<anonymous>.it (src/_tests/_wrong.test.tsx:2:13)

  ✕ is an incorrect test (94ms)

Which updates the file src/_tests/_wrong.test.tsx. Messaging to the watcher should have the same feeling as a snapshot update.

Implementation

This could be an incrementally built out feature:

  • v1: number literals: 4 -> 5
  • v2: string literals: "four" -> "five"
  • v3: object literals: { four: true } -> { five: true }

As number literals have the least amount of potential variants ( vs the multiple types of string literals ) that makes sense as a nice place to start.

I had initially wondered if this could be built out as its own plugin, but once I came to the conclusion about the user experience side of it - it really needs work inside the watcher, though it may be feasible to have the work of AST transformation happen inside a new package ( which could make to easy for dev tools to use it too)

Jest could pass the AST of the it block that failed to the new package, and then it's the job of the package to read through the AST and make the transformation. I don't know if there's an easy way to go from an AST literal back to the LOC/character index (As I've not done too much there) but if prettier/codemods can do it, so can this package.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions