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

Support the Jest setting testPathIgnorePatterns in order to be able to exclude files from unit testing when in CI mode #9345

Open
svdHero opened this issue Jul 22, 2020 · 0 comments

Comments

@svdHero
Copy link

svdHero commented Jul 22, 2020

Is your proposal related to a problem?

Yes it is. npm test does not use the Jest setting watchPathIgnorePatterns when in CI mode and when the environment variable CI is set. The alternative Jest setting testPathIgnorePatterns is not supported by create-react-app.

Environment

W:\GIT_WORKSPACES\react-ci-test>npm --version
6.14.6

W:\GIT_WORKSPACES\react-ci-test>npx create-react-app --info
npx: installed 98 in 17.52s

Environment Info:

  current version of create-react-app: 3.4.1
  running from C:\Users\XXX\AppData\Roaming\npm-cache\_npx\15344\node_modules\create-react-app

  System:
    OS: Windows 8.1 6.3.9600
    CPU: (8) x64 Intel(R) Xeon(R) CPU E5-1620 v2 @ 3.70GHz
  Binaries:
    Node: 14.0.0 - C:\Program Files\nodejs\node.EXE
    Yarn: Not Found
    npm: 6.14.6 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Internet Explorer: 11.0.9600.19036
  npmPackages:
    react: ^16.13.1 => 16.13.1
    react-dom: ^16.13.1 => 16.13.1
    react-scripts: 3.4.1 => 3.4.1
  npmGlobalPackages:
    create-react-app: Not Found


W:\GIT_WORKSPACES\react-ci-test>

Steps to reproduce

Have the follwoing jest settings in package.json

  "jest": {
    "watchPathIgnorePatterns": [
      ".*\\.testcases\\.js"
    ]
  }

Run

npm test --ci --coverage

All tests succeed and files like App.testcases.ts are excluded from test.

Run

set CI=true
npm test --ci --coverage

All previous tests succeed, but the overall process fails, because files like App.testcases.ts are NOT excluded from test.
Test output:

W:\GIT_WORKSPACES\react-ci-test>npm test --ci --coverage

> [email protected] test W:\GIT_WORKSPACES\react-ci-test
> react-scripts test

 FAIL  src/__tests__/App.testcases.js
  ? Test suite failed to run

    Your test suite must contain at least one test.

      at W:/GIT_WORKSPACES/react-ci-test/node_modules/@jest/core/build/TestScheduler.js:242:24
      at asyncGeneratorStep (W:/GIT_WORKSPACES/react-ci-test/node_modules/@jest/core/build/TestScheduler.js:131:24)
      at _next (W:/GIT_WORKSPACES/react-ci-test/node_modules/@jest/core/build/TestScheduler.js:151:9)
      at W:/GIT_WORKSPACES/react-ci-test/node_modules/@jest/core/build/TestScheduler.js:156:7
      at W:/GIT_WORKSPACES/react-ci-test/node_modules/@jest/core/build/TestScheduler.js:148:12
      at onResult (W:/GIT_WORKSPACES/react-ci-test/node_modules/@jest/core/build/TestScheduler.js:271:25)

 PASS  src/__tests__/App.test.js

Test Suites: 1 failed, 1 passed, 2 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        5.035s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

When I use the jest setting

  "jest": {
    "testPathIgnorePatterns": [
      ".*\\.testcases\\.ts"
    ]
  }

instead in my package.json and run the test, I get the well-known error message

Out of the box, Create React App only supports overriding these Jest options:

   clearMocks
   collectCoverageFrom
   coveragePathIgnorePatterns
   coverageReporters
   coverageThreshold
   displayName
   extraGlobals
   globalSetup
   globalTeardown
   moduleNameMapper
   resetMocks
   resetModules
   restoreMocks
   snapshotSerializers
   transform
   transformIgnorePatterns
   watchPathIgnorePatterns.

These options in your package.json Jest configuration are not currently supported by Create React App:

   testPathIgnorePatterns

If you wish to override other Jest options, you need to eject from the default setup. You can do so by running npm run eject but remember that this is a one-way operation. You may also file an issue with Create React App to discuss supporting more options out of the box.

npm ERR! Test failed.  See above for more details.

and I don't want to eject just to make unit testing work in CI mode.

Reproducible demo

An almost out-of-the-box app:
https://github.com/svdHero/react-ci-test

Describe the solution you'd like

Support the jest setting testPathIgnorePatterns in order to be able to exclude files from unit testing when in CI mode.

Describe alternatives you've considered

Do not use the __tests__ folder pattern at all, although CRA is supporting it: https://create-react-app.dev/docs/running-tests#filename-conventions

Additional context

I like to keep my test files separate from my production code in a __tests__ folder. However, there are auxiliary files containing test values, test data generators, etc. which are closely related to the test files but do NOT contain any Jest code, i.e. any describe or it. I want to exclude these files.

@svdHero svdHero changed the title Support the jest setting testPathIgnorePatterns in order to be able to exclude files from unit testing when in CI mode Support the Jest setting testPathIgnorePatterns in order to be able to exclude files from unit testing when in CI mode Jul 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant