how to use testNamePattern of jest within package.json scripts field? - javascript

Honestly, jest of facebook has a bad document.
I want to use jest -t xxx to update my snapshots.
Here is my test, but no one works correctly.
jest -u -t='test.tsx'
jest -u -t 'test.tsx'
jest -u -t /test.tsx/
jest -u -t=/test.tsx/
I want to match and update snapshots test files which filename pattern like *.test.tsx.
How can I do that with jest testNamePattern?

First check jest --showConfig to see that you even have the option of --testNamePattern. I faced the same issue, and did not find this option in jest config.
So I changed my directory structure and used --testPathPattern option with regex. This works.
jest --testPathPattern='(tests)/spec/./snapshot-tests/.'

Related

React / Jest get coverage from a single component not all - current approach not working

I have tried this answer to get coverage for a single component but it is not working. The test runs only for that component but no coverage?
Is there some other configuration that needs to be done anywhere?
I have tried:
npm test src/components/component1/my-component.test.tsx --coverage --collectCoverageFrom=src/components/component1/my-component.test.tsx
Anything I am doing wrong?
The collectCoverageFrom takes in glob patterns as argument, so you need adjust that path string with "".
I have successfully run this command with code coverage from one file with
node_modules/.bin/jest ComponentTest.test.ts --coverage --collectCoverageFrom="path/Component.tsx"
It also it may be that your "test" cmd you are running from package.json is obscuring the additional arguments, try directly calling jest from modules to see if thats the case.
This worked to me with create-react-app from windows cmd:
npx react-scripts test src/components/common/__test__/Dropdown.test.tsx --coverage --collectCoverageFrom=src/components/common/Dropdown.tsx

Update jest snapshot with testPathPattern

I use the following command to run my tests located in utils file.
npm t -- --testPathPattern=utils.spec.js
If i want to update ALL my snapshots i use
npm t -- -u
But how can i update only snapshot in utils.spec.js file?
Thanks.
Following the docs
Use this flag to re-record every snapshot that fails during this test
run. Can be used together with a test suite pattern or with
--testNamePattern to re-record snapshots.
it should be
npm t -- --testPathPattern=utils.spec.js -u

How can I use Mocha without removing Ava?

One of my co-workers added this Ava package to our setup, and it's done something I've never seen a Node package do before: interfere with other packages! Now when I try to run Mocha I get:
$ node_modules/mocha/bin/mocha test/
Test files must be run with the AVA CLI:
$ ava node_modules/mocha/bin/_mocha
I get that Ava would like to run my Mocha tests, but if I wanted that I'd run ava mocha not mocha. And because a co-worker is using it I can't simply uninstall the package.
Is there any way I can run plain Mocha tests on a machine with Ava installed?
One of the files in test/ imports ava and the imported code will recognise that it's not being run with the correct tooling and throw an error.
Might be worth subdividing your test/ directory to keep tests associated with their respective runners.
test/
ava/
SomeAvaTests.js
mocha/
SomeMochaTests.js
This way you can safely run mocha test/mocha/ and vice versa without worrying about treading on each other's toes.

How to run specific tests with frisby?

We are using frisby to run our integration tests and while developing them, it would be handy to execute one specific one or a group of tests, without having run all of them and see extra noise. Right now I am commenting out all the ones I don't want to run, which is getting tedious.
Is there a way, from the command line, to run specific frisby tests?
So basically, instead of
npm test
I want to be able to say
npm test --name: posts
Or something like that. I found this post about jasmine-only, but I'm not sure it will satisfy my needs.
Thanks so much!
I'm not sure if you're still looking for answer, but this is pretty simple.
Firstly install latest version of jasmine-node from command line: npm install jasmine-node -g
Then to run particular test use: jasmine-node --coffee putTestNameHere
Install jasmine-node module. Execute one file at a time - you can group your test cases in specific file:
jasmin-node moduleTestCases_spec.js
Also, if you want to specify exact test case name to be executed, you can make use of sequenty module. It is a nodejs module, which you can specify the order(and thus the exact test cases to execute).
To run specific test in Frishby just run:
npm test ./folder/filename.js
So lets assume you have an folder say test under that you have a file called api.spec.js
then you will execute like this:
npm test ./test/api.spec.js
Parallely don't forget to specify these below things into your package.json file
"scripts": {
"test": "mocha" }

How to get the code coverage report using Jest?

Is there a way to have code coverage in the JavaScript Jest testing framework, which is built on top of Jasmine?
The internal framework does not print out the code coverage it gets. I've also tried using Istanbul, blanket, and JSCover, but none of them work.
When using Jest 21.2.1, I can see code coverage at the command line and create a coverage directory by passing --coverage to the Jest script. Below are some examples:
I tend to install Jest locally, in which case the command might look like this:
npx jest --coverage
I assume (though haven't confirmed), that this would also work if I installed Jest globally:
jest --coverage
The very sparse docs are here
When I navigated into the coverage/lcov-report directory I found an index.html file that could be loaded into a browser. It included the information printed at the command line, plus additional information and some graphical output.
UPDATE: 7/20/2018 - Added links and updated name for coverageReporters.
UPDATE: 8/14/2017 - This answer is totally outdated. Just look at the Jest docs now. They have official support and documentation about how to do this.
#hankhsiao has got a forked repo where Istanbul is working with Jest. Add this to your dev dependencies
"devDependencies": {
"jest-cli": "git://github.com/hankhsiao/jest.git"
}
Also make sure coverage is enabled in your package.json jest entry and you can also specify formats you want. (The html is pretty bad ass).
"jest": {
"collectCoverage": true,
"coverageReporters": ["json", "html"],
}
See Jest documentation for coverageReporters (default is ["json", "lcov", "text"])
Or add --coverage when you invoke jest.
Jan 2019: Jest version 23.6
For anyone looking into this question recently especially if testing using npm or yarn directly
Currently, you don't have to change the configuration options
As per Jest official website, you can do the following to generate coverage reports:
1- For npm:
You must put -- before passing the --coverage argument of Jest
npm test -- --coverage
if you try invoking the --coverage directly without the -- it won't work
2- For yarn:
You can pass the --coverage argument of jest directly
yarn test --coverage
This works for me:
"jest": {
"collectCoverage": true,
"coverageReporters": ["json", "html"]
},
"scripts": {
"test": "jest --coverage"
},
Run:
yarn/npm test
You can run npx jest --coverage -- path/to/your/file.spec.js
that will show coverage for affected files
If you want to view this in browser you can do as follows,
Go to Browser and CMD+O.
Navigate to your repo and search for coverage/lcov-report/index.html
Then you can visually see all the coverage areas.
You can also refer to this link below, for more information
https://dev.to/stevescruz/awesome-jest-tip-coverage-report-h5j
Check the latest Jest (v 0.22): https://github.com/facebook/jest
The Facebook team adds the Istanbul code coverage output as part of the coverage report and you can use it directly.
After executing Jest, you can get a coverage report in the console and under the root folder set by Jest, you will find the coverage report in JSON and HTML format.
FYI, if you install from npm, you might not get the latest version; so try the GitHub first and make sure the coverage is what you need.
If you are having trouble with --coverage not working it may also be due to having coverageReporters enabled without 'text' or 'text-summary' being added.
From the docs: "Note: Setting this option overwrites the default values. Add "text" or "text-summary" to see a coverage summary in the console output." Source
Configure your package.json file
"test": "jest --coverage",
Now run:
yarn test
All the test will start running and you will get the report.
I had the same issue and I fixed it as below.
install yarn npm install --save-dev yarn
install jest-cli npm install --save-dev jest-cli
add this to the package.json "jest-coverage": "yarn run jest -- --coverage"
After you write the tests, run the command npm run jest-coverage. This will create a coverage folder in the root directory. /coverage/icov-report/index.html has the HTML view of the code coverage.

Categories

Resources