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

Running the dev script concurrently with the Jest test script causes Jest to re-run the tests on page compilation #32650

Closed
sfiquet opened this issue Dec 19, 2021 · 3 comments · Fixed by #32659
Labels
Testing Related to testing with Next.js.

Comments

@sfiquet
Copy link

sfiquet commented Dec 19, 2021

What version of Next.js are you using?

12.0.4

What version of Node.js are you using?

14.18.2

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Vercel

Describe the Bug

jest --watch re-runs the test suites every time the dev server compiles a page. This bug is present on both the babel and swc versions of the jest config.


I found out that the Jest watcher is watching the .next/ folder. It used to ignore dot files and folders but that behaviour was changed last year (jestjs/jest#10075). It is now necessary to specify the dot folder in watchPathIgnorePatterns. I created a PR with Jest to change the documentation: jestjs/jest#12161

Expected Behavior

I expect Jest in watch mode to only re-run the test suites when source files or test files have changed, not when the dev server writes to .next/.

To Reproduce

Step 1: Set up a project with any of the Jest configurations (Babel or Swc). The project needs to have several pages to be able to navigate between them.

Step 2: run yarn dev in a terminal. In another terminal, run yarn test. The scripts are standard:

  "scripts": {
    "dev": "next dev",
    "test": "jest --watch"
  }

Step 3: in the jest terminal, press A to run all tests.
My original case was to add a page and a related test file. But running all tests requires less work to show the same result.

Step 4: In the browser, navigate between pages.

Result:

  • Each time a page is compiled, you get something like this in the dev server terminal (expected):
wait  - compiling /about...
event - compiled successfully in 190 ms (221 modules)
  • At the same time, in the jest terminal, the test suites are re-rerun, in spite of there being no change to the source or test files.
@sfiquet sfiquet added the bug Issue was opened via the bug report template. label Dec 19, 2021
@balazsorban44 balazsorban44 added Testing Related to testing with Next.js. kind: bug and removed bug Issue was opened via the bug report template. labels Dec 19, 2021
@balazsorban44
Copy link
Member

Appreciate the research! I opened #32659 that should fix this.

For now, simply adding this to your jest config is enough:

watchPathIgnorePatterns: [
 '/.next/',
],

@kodiakhq kodiakhq bot closed this as completed in #32659 Dec 19, 2021
kodiakhq bot pushed a commit that referenced this issue Dec 19, 2021
`jest --watch` looks for [changes in dotfiles and folders by default](jestjs/jest#10075), which resulted in the tests being re-run when navigating between pages with `next dev`.

Fixes #32650

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
@sfiquet
Copy link
Author

sfiquet commented Dec 22, 2021

Thanks, I actually used '<rootDir>/\\.next/'. You might want to escape the dot in the config otherwise it matches any character since it's a regex.

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 27, 2022
natew pushed a commit to natew/next.js that referenced this issue Feb 16, 2022
`jest --watch` looks for [changes in dotfiles and folders by default](jestjs/jest#10075), which resulted in the tests being re-run when navigating between pages with `next dev`.

Fixes vercel#32650

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Testing Related to testing with Next.js.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants