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

Jest config projects array can't handle only one item #7496

Closed
theneva opened this issue Dec 11, 2018 · 4 comments · Fixed by #7498
Closed

Jest config projects array can't handle only one item #7496

theneva opened this issue Dec 11, 2018 · 4 comments · Fixed by #7498
Labels

Comments

@theneva
Copy link
Contributor

theneva commented Dec 11, 2018

🐛 Bug Report

Jest attempts to run all JS, JSON, and snapshot files as tests (ignoring the test config in the project folder) when only one project is specified in the projects array in the root project config.

Interestingly, it works as expected if the projects array includes any other project, even if that project has no Jest config and no tests. See the linked repo's README for an example.

This is a real use case for us, as we have several packages that are not required in all builds, but have lots of dependencies and/or take a long time to build. To avoid wasting time and money during the build, our build process for any <package> (starting with a copy of the project structure inside a Docker container) goes like this:

  1. Determine which workspace packages are required to build <package>
  2. rm -rf all other packages
  3. yarn install
  4. yarn build (which builds all packages in order of dependencies)
  5. yarn test (which runs Jest for all packages, configured with "projects": [ "packages/*" ])

If package is a library or app with no dependencies on other workspace packages, we end up with exactly one package left in packages/, so packages/* resolves to exactly one package, which in turn triggers this bug.

To Reproduce

Create a repo with the following structure:

repo
`- package.json
   packages
   `- a
      `- package.json
         test.js

Add the following Jest config to repo/package.json:

  "jest": {
    "testMatch": [],
    "projects": [
      "packages/a"
    ]
  }

Add any Jest config to repo/packages/a/package.json:

  "jest": {
    "displayName": "a"
  }

Add any dummy test to repo/packages/a/test.js, such as:

test('1+1', () => {
    expect(1).toBe(1);
});

Run Jest and verify that it ignores the project config, and instead attempts to run the package.json files in both root and packages/a as tests (which fail), in addition to test.js (which passes):

$ yarn jest

yarn run v1.12.3
$ /Users/theneva/code/jest-project-stuff/node_modules/.bin/jest
 PASS  packages/a/test.js
 FAIL  ./package.json
  ● Test suite failed to run

    Your test suite must contain at least one test.

      at node_modules/jest-cli/build/TestScheduler.js:256:22

 FAIL  packages/a/package.json
  ● Test suite failed to run

    Your test suite must contain at least one test.

      at node_modules/jest-cli/build/TestScheduler.js:256:22

Test Suites: 3 failed, 1 passed, 4 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.113s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Expected behavior

The tests for the specified project should be executed with the project's Jest config, the way it would be executed if there were multiple projects.

Link to repl or repo (highly encouraged)

Please see README (and code that reflects the reproduction steps above) at https://github.com/theneva/jest-project-stuff

Run npx envinfo --preset jest

Paste the results here:

npx envinfo --preset jest
npx: installed 1 in 1.739s

  System:
    OS: macOS 10.14.1
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
  Binaries:
    Node: 10.13.0 - ~/.nvm/versions/node/v10.13.0/bin/node
    Yarn: 1.12.3 - /usr/local/bin/yarn
    npm: 6.4.1 - ~/.nvm/versions/node/v10.13.0/bin/npm
  npmPackages:
    jest: ^24.0.0-alpha.6 => 24.0.0-alpha.6
@SimenB
Copy link
Member

SimenB commented Dec 11, 2018

I wonder if this is as simple as setting this to > 0?

https://github.com/facebook/jest/blob/509272d773568e61ba60db845cec67ff2f1418c7/packages/jest-config/src/index.js#L280

Untested, but mind giving it a whirl? It's been that way since the original PR though: https://github.com/facebook/jest/pull/3400/files#diff-8006e39142c24851115752df17e9c1c0R109

@cpojer @rickhanlonii thoughts?

@theneva
Copy link
Contributor Author

theneva commented Dec 11, 2018

@SimenB that fixes the issue in the repo I linked:

diff --git a/packages/jest-config/src/index.js b/packages/jest-config/src/index.js
index b1832b94a..79719e464 100644
--- a/packages/jest-config/src/index.js
+++ b/packages/jest-config/src/index.js
@@ -277,7 +277,7 @@ export function readConfigs(
     }
   }

-  if (projects.length > 1) {
+  if (projects.length > 0) {
     const parsedConfigs = projects
       .filter(root => {
         // Ignore globbed files that cannot be `require`d.

… followed byyarn build, then yarn link in packages/jest-config and yarn link jest-config in my repro repo…

$ ls -la node_modules/jest-config
lrwxr-xr-x  1 theneva  staff  38 Dec 11 11:54 node_modules/jest-config -> ../../../.config/yarn/link/jest-config

$ ls -la ~/.config/yarn/link/jest-config
lrwxr-xr-x  1 theneva  staff  39 Dec 11 11:54 /Users/theneva/.config/yarn/link/jest-config -> ../../../code/jest/packages/jest-config

$ yarn jest
yarn run v1.12.3
$ /Users/theneva/code/jest-project-stuff/node_modules/.bin/jest
 PASS   a  packages/a/test.js
  ✓ 1+1 (3ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.712s, estimated 1s
Ran all test suites.
✨  Done in 1.15s.

@rickhanlonii
Copy link
Member

That's great, @theneva thanks for checking!

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants