Invalid testPattern *-long.spec.js supplied. Running all tests instead [duplicate] - jestjs

I have in packadge.json
"scripts": {
"test": "cross-env NODE_ENV=test jest"
},
I want to test all files match pattern
front/**/*.test.js
but if
"scripts": {
"test": "cross-env NODE_ENV=test jest \"front/**/*.test.js\""
},
I've got an error
Invalid testPattern front/**/*.test.js supplied. Running all tests instead.
So, how could I pass file names pattern to jest?
Thanks.

According to the documentation you are supposed to use a regex for your pattern. So in your case you probably would write something like this:
"scripts": {
"test": "cross-env NODE_ENV=test jest \"front/.*\\.test\\.js\""
}

Use the testRegex parameter in your jest config to set a filename pattern:
testRegex: 'front/.*\.test\.js$'
Have a look at the documentation here.

Related

how to change the path in dotenv with various script commands (in package.json) such as : start , test , etc

well my problem is when I want to switch my script command in package.json like from "start" to "test" for running my Jest test which its commands like :
"scripts": {
"start": "nodemon express/***",
"serve": "node express/***",
"dev": "node express/***",
"test": "jest --watch"
},
and I call dotenv in my project like this
require("dotenv").config({
path: "express/config/.env",
});
The code above, help my to using my environment file like .env
but the problem is that when I want to test my project and I want to switch my script command (in package.json) from like "start" to "test" and change the main path of dotenv environment to something like test.env
You could pass the environment type as an environment variable into your program like so. Note: you will need to use cross-env if you require multi-platform support.
Unix version:
"scripts": {
"start": "NODE_ENV=production nodemon express/***",
"serve": "NODE_ENV=production node express/***",
"dev": "NODE_ENV=dev node express/***",
"test": "NODE_ENV=test jest --watch"
}
cross-env version:
"scripts": {
"start": "cross-env NODE_ENV=production nodemon express/***",
"serve": "cross-env NODE_ENV=production node express/***",
"dev": "cross-env NODE_ENV=dev node express/***",
"test": "cross-env NODE_ENV=test jest --watch"
}
And then access them using the normal method of process.env.NODE_ENV
const envVariablePaths = {
"production": "/path/here",
"dev": "path/here",
"test": "path/here",
}
require("dotenv").config({
path: envVariablePaths[process.env.NODE_ENV],
})
More documentation can be found here

Is it possible to set a dotenv variable from package.json script?

I am using dotenv in my project to get variables from a .env file. I would like to be able to set a value in the .env(dotenv) file from package.json when running npm test. Something similar to this:
"scripts": {
"test": "set ENV=test mocha './test/**/*.spec.js'",
"start": "node app"
}
my .env file is simply this:
ENV = development
I'm using windows.
Thanks in advance for any help!
You can use
"test": "ENV=test mocha './test/**/*.spec.js'"
in your npm script on Linux, and use
"test": "set ENV=test&&mocha './test/**/*.spec.js'"
on Windows.
BTW, if you need to cross platform, use cross-env so that your code can run on both Windows and Linux.
You can also use require.
For example, you have a ./test/test.env.js
// test/test.env.js
process.env.ENV = 'test';
and use require in your npm script like this:
"test": "mocha --require 'test/test.env.js' './test/**/*.spec.js'"
Yes you can do that, you can pass environment variables through commands too.
i.e. with your scripts:-
"scripts": {
"test": "set ENV=test mocha './test/**/*.spec.js'",
"start": "ENV=test node app.js" //NOTICE I HAVE CHANGED THIS.
}
Just by using, I don't know how to help about the test script, I don't understand that, but I believe it must be something like this
"test": "ENV=test mocha './test/**/*.spec.js'"

Jest test coverage does not fail when threshold is not met

Utilizing create-react-app, when running tests in my CI pipeline, if the code coverage thresholds are not met, I expect the console to return a non-zero response.
package.json
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"test:coverage": "npm run test -- --coverage --watchAll=false",
},
"jest": {
"collectCoverageFrom": [
"src/components/**/*.js",
"src/state/**/*.js",
"src/templates/**/*.js",
"src/routes/**/*.js"
],
"coverageThreshold": {
"global": {
"branches": 80,
"functions": 80,
"lines": 80,
"statements": 80
}
}
}
When running test:coverage the console reports that thresholds were not met, but still returns 0. My understanding from the Jest documentation is that an error should be returned when coverage thresholds are not met.
https://jestjs.io/docs/en/configuration#coveragethreshold-object
Specifically...
If thresholds aren't met, jest will fail.
Is anyone familiar with this issue? I have been through Jest and CRA github issues with mixed results and most findings are related to outdated versions.
To stop further execution when command fails:
command || exit 0
{
"test:coverage": "npm run test -- --coverage --watchAll=false || exit 0"
}
ref: don't fail jenkins build if execute shell fails
Add "collectCoverage": true, in your jest configuration. After that jest run operation will fail, if the coverage is not met.
Defining the config in the packjage.json file did not fail the jest command when coverage threshold was not met. But it worked when I defined it through the jest.config.js file!
This will make the test fail if the conditions are not met, but please note that this will slow down your tests:
"scripts": {
"test": "jest --collectCoverage=true",
"coverage" : "jest --coverage"
},

npm test -- --coverage never exits

I am using create-react-app to create a react application. When I executes npm test -- --coverage the test never exists. npm test actually runs react-scripts test. Any Idea?
-- --coverage part won't work, and should use one of the commands below to set CI to true.
By default npm test runs the watcher with interactive CLI. However, you can force it to run tests once and finish the process by setting an environment variable called CI.
source: React docs
Windows (cmd.exe)
set CI=true && npm test
set CI=true && npm run build
Windows (Powershell)
($env:CI = "true") -and (npm test)
($env:CI = "true") -and (npm run build)
Linux, macOS (Bash)
CI=true npm test
CI=true npm run build
NOT included in the docs
For Docker (node and react):
docker run -e CI=true [myImage] npm run test
Coverage won't work with Jest in watch mode.
Because "react-scripts test --env=jsdom" works in watch mode by default, the watch mode has to be switched off while generating the coverage output.
The following excerpt from the package.json contains a line "coverage" for illustration, how code coverage can be achieved within an app which was bootet by create-react-app.
It's just the modified "test" script, where the options --watchAll=false and --coverage are added in combination:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"coverage": "react-scripts test --env=jsdom --watchAll=false --coverage",
"eject": "react-scripts eject"
}
Please note that it is obsolete to use standalone double-dash -- .
Most of the time this issue can be occur because of following reasons.
Not mentioning the required npm-script arguments in the
package.json file. If you use create-react-app to create your
react application, then it will not accept any command line
arguments. To resolve this problem, add following line under the
script tag in your package.json.
"test": "react-scripts test --coverage --watchAll", //mark --watchAll=false if you want.
Not mentioning the required jest configuration arguments in
the package.json or jest.config.js files. You should mention the files
which needed to include in your test coverage under the jest
configurations. Add following configurations in your
package.json.
package.json
"jest": {
"collectCoverageFrom": [
"src/**/*.js",
"!src/index.js", // files you need to avoid in test coverage
"!src/hooks/*.js",
"!src/context/*.js"
],
"coverageThreshold": {
"global": {
"branches": 90,
"functions": 90,
"lines": 90,
"statements": 90
}
},
"coverageReporters": [
"html",
"text"
]
},
Specifying a directory worked in my case
"test:cover": "react-scripts test --coverage src"
I tried all the solutions above, and for me it was still hanging with the message: Ran all test suites..
But this little hack helped:
"test:ci": "cross-env CI=true react-scripts test --forceExit --detectOpenHandles",
Explanation: The problem was coming from Jest not being able to close all processes. The above is a quick workaround. Ideally you should track the process that's stopping Jest from exiting.
In my case just added a new script "test:coverage": "react-scripts test --coverage"
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"test:coverage": "react-scripts test --coverage",
"eject": "react-scripts eject"
},

Tell Node script to execute 2 commands

I start my app with node server.js command
And this works to run my tests:
"scripts": {
"test": "node node_modules/karma/bin/karma start test/karma.conf.js",
}
Now now, when I run the test script, I want it to first execute server.js before starting the tests.
But this doesn't work:
"scripts": {
"test": "node server.js node node_modules/karma/bin/karma start test/karma.conf.js",
}
How can I get my test script to do both?
You forgot the semi-colon after the first command:
"scripts": {
"test": "node server.js; node node_modules/karma/bin/karma start test/karma.conf.js"
}

Resources