I am trying to integrate Jest flow runner with my app created with create-react-app as I want to run my flow type checks along with the test cases.
I followed the steps given in the Jest platforms video and added:-
{
"jest": {
"runner": "jest-runner-flowtype"
}
}
in my package.json but got this message:-
Out of the box, Create React App only supports overriding these Jest
options:
• collectCoverageFrom
• coverageReporters
• coverageThreshold
• snapshotSerializers.
These options in your package.json Jest configuration are not currently supported by Create React App:
• runner
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.
Is there a way to use both jest flow runner & test runner at the same time with jest?
I read on create-react-app testing guide but couldn't find a way to add one runner.
Update:
I tried ejecting from the create-react-app and added projects option:-
"projects": [
{
"displayName": "flow",
"runner": "jest-runner-flowtype",
"testMatch": ["<rootDir>/**/*.js"]
}
]
and tried running yarn jest --env=jsdom still no flow types are errors are being displayed.
Related
I have a react project in this git repo created from scratch without using create-react-app, so I am using jest.config.js and jest.setup.js files as suggested by msw doc for mocking network request for testing. I get the below error when I executing the test.
(node:12207) ExperimentalWarning: The fs.promises API is experimental
console.error
Error: Error: connect ECONNREFUSED 127.0.0.1:5000
The same test code works fine when I create a new project using create-react-app containing setupTests.js, but I want to mock api request in a react project which was created manually from scratch.
Any suggestion?
In the tutorial for msw, the mock server is configured in setupTests.js.
Because CRA (create-react-app) already configure jest for you and CRA will load setupTests.js when you run test with react-scripts test.
If you want to create a new project from scratch (supposed you already installed react, react-dom, ... ).
You have to config jest your self (refer this docs), steps needed:
Change test scripts in package.json to jest
Install dependencies (babel, preset for jest, etc)
Config jest with jest.config.js (like you did, i also add jsdom as test environment)
Setup jest.setup.js to load mock server
I created a PR that did above steps for you, to test just run
npm run test App.test.js
Output:
I am building a react project which has 2 separate Jest configs for my frontend and backend tests. My frontend tests use React Testing Library which requires the "jsdom" test environment and my backend tests use mongoose which requires the "node" test environment. I am trying to use the jest "projects" config in my package.json in order to run the two testing suites simultaneously with their respective environments like so:
{
"jest": {
"projects": [
"backend/config/jest.config.js",
"src/jest.config.js"
]
}
}
However, when I try to run the tests, I get this 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:
• projects
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.
I'm not quite sure what do to here. Any way to make the jest "projects" option work the create-react-app?
I am using Jest + Enzyme for unit testing of react application, and I am also using my company owned dependent libraries as node_modules , those are in Javascript (ES6) but my application and my test scripts are in TypeScript, so here while running tests Jest is throwing type errors from those libraries ( node_modules) as you see in below picture, Abstract.jsx is coming from dependent node_modules
I am using below setting in jest config which is not helping, how to fix this continuous type errors. please help
"globals": {
"ts-jest": {
"diagnostics": false
}
},
, Abstract.jsx is coming from dependent node_modules
The module should come with compiled .js files. It seems that it doesn't
Options
Pick one:
Ask the module author to publish built .js files (recommended)
Build them yourself. Add a .jsx to .js build workflow into your setup.
I am getting my head around create-react-app and just tried the eject option. After 'yarn eject' I am trying to debug via the terminal:
> react-scripts --inspect-brk test --runInBand
Debugger listening on ws://127.0.0.1:9229/45316de1-972e-47ab-9ce8-7ce2f183a378
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
when clicking on the inspect link in the chrome://inspect/#devices, the console opens and says:
Out of the box, Create React App only supports overriding these Jest
options: • collectCoverageFrom • coverageReporters •
coverageThreshold • globalSetup • globalTeardown • resetMocks •
resetModules • snapshotSerializers • watchPathIgnorePatterns.These
options in your package.json Jest configuration are not currently
supported by Create React App: • resolver • setupFiles • testMatch
• testEnvironment • testURL • transform • transformIgnorePatterns
• moduleNameMapper • moduleFileExtensionsIf 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.
That is strange because I thought I already ran the eject?
react-scripts is the executable that unejected create-react-app runs, so it will produce this message, no matter whether the project was ejected or not.
npm run eject replaces react-scripts in ejected project scripts with actual commands. In ejected project, it is expected that npm run test executes jest directly instead of react-scripts test.
If the intention is to provide additional arguments to Node, it could be something like:
node --inspect-brk node_modules/.bin/jest test --runInBand
I have problem with configuration sonarqube to work properly with React + Jest.
My configuration:
my_moudle.sonar.projectBaseDir=front_app
my_module.sonar.javascript.file.suffixes=.js,.jsx
my_module.sonar.tests=src
my_module.sonar.test.inclusions=**/__tests__/** my_module.sonar.javascript.lcov.reportPaths=coverage/lcov.info
Currently I point to the src folder as tests folder because I have set of tests for each component in tests folder at the same project folder.
Thanks to SonarJS I have proper coverege for my project, but I don't know why I can't see amount of unit tests in coverage measures metrics.
Any checked configuration will be appreciate.
Thanks,
Barb
SonarJS is not responsible for Unit test reports, (Relevant FAQ entry)
It is the responsibility of SonarQube to import the Unit test report.
I have managed to do it with the following setup.
First we need to convert the jest results into sonar consumable format. For this, I used the following npm module: jest-sonar-reporter.
Snippet of my package.json:
"devDependencies": {
...
"jest": "^21.1.0",
"jest-sonar-reporter": "^1.3.0",
...
},
"jestSonar": {
"sonar56x": true,
"reportPath": "testResults",
"reportFile": "sonar-report.xml",
"indent": 4
}
You can will now need to tell jest to use this module as a processor and tell sonar scanner to use the file produced by this module. The instructions are in the official documentation.
If you are using create-react-app/react-scripts though, you may need a few additional steps, as all configuration is not exposed by them.
You will need to modify your entry for test in scripts block in package.json to:
"test": "react-scripts test --env=jsdom --testResultsProcessor ./node_modules/jest-sonar-reporter/index.js