How to get a code coverage report with react-create-app? - reactjs

If I run
npm test --coverage
the tests pass, but the coverage is not run.
When I change package.json to have
react-scripts test --coverage
Then, when I do npm test and a it runs the tests but not the coverage
The tests run along with the coverage report but the coverage shows all zeroes for coverage
PASS src/components/Header/SubHeader.test.js
The <SubHeader /> component
✓ should render (4ms)
✓ should render with a goback button (1ms)
✓ should render
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 0 | 0 | 0 | 0 | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 3 passed, 3 total
Time: 1.077s
Ran all test suites.
Finally, I realized that I can do
npm run test .
Question: Why don't the other methods work?

After revisiting this question i figured out that if you run this you will trigger coverage command and get all results.
SOLUTION 1
npm run test -- --coverage .
When you are passing arguments to npm script, you need to add -- before adding arguments like --coverage. This is just how npm works. See this answer for passing arguments to npm
SOLUTION 2
yarn test --coverage .
EDIT:
I asked a question regarding . being passed down the command and answer is pretty much simple. . is passed because it is a positional parameter, in contrast to --coverage that is option for npm test command: Why is . passed as argument to npm without -- delimiter?
---- Problem analysis ----
PROBLEM 1
npm test --coverage you are not passing --coverage argument to test script.
In the other case when you edit script to be:
react-scripts test --coverage and run npm test here you actually add argument --coverage, but in script itself, not from npm command
PROBLEM 2
When you do npm run test . you are passing . that means you want to include all files in this command.
For some reason . is passed to the npm run test script but not --coverage.
On the other side yarn passes all arguments without --.

Related

Jest complains about Typescript syntax

When I run yarn test Jest throws an error that says
'Const declarations' require an initialization value.
Which is not what I'm doing!
> 4 | const config: HardhatUserConfig = {
| ^
5 | solidity: "0.8.9",
6 | paths: {
7 | artifacts: "./src/artifects",
I think it is obvious that jest is having problem with TS syntax.
How can I fix that?
You need to use ts-jest.
Install ts-jest: npm install ts-jest -D
Update your jest config to use: preset: 'ts-jest'
You still run your tests the same way - just run jest.

Jest should only run on changed files for pre-commit hook

I have a pre-commit hook set up using jest and the --only-changed flag. However, sometimes my entire test suite will still run (800 tests!) even if I made a change in a single file.
I looked into some other jest flags like
--lastCommit Run all tests affected by file changes in
the last commit made. Behaves similarly to
`--onlyChanged`.
--findRelatedTests Find related tests for a list of source
files that were passed in as arguments.
Useful for pre-commit hook integration to
run the minimal amount of tests necessary.
--changedSince Runs tests related to the changes since the
provided branch. If the current branch has
diverged from the given branch, then only
changes made locally will be tested. Behaves
similarly to `--onlyChanged`. [string]
Yet they all have the same problem. When doing some digging, I learned that
under the hood "If the found file is a test file, Jest runs it, simple enough. If the found file is a source file, call it found-file.js, then any test files that import found-file.js and the test files that import any of the source files that themselves import found-file.js will be run."
I'm working on a project that's relatively new to me. I'm wondering if it's possible for me to get my pre-commit hook to ONLY run the edited test, not all affected tests, or if there is a way for me to track down this tree of "transitive inverse dependencies" and try to solve the problem with different imports or something.
Here is an example of some output from trying --find-related-tests
Test Suites: 2 failed, 309 passed, 311 total
Tests: 2 failed, 803 passed, 805 total
Snapshots: 308 passed, 308 total
Time: 102.366 s
Ran all test suites related to files matching /\/Users\/me\/repo\/project\/src\/modules\/dogs\/components\/spots\/SpotsSpotter.tsx/i.
> #dogsapp/project#1.0.0 test:staged
> jest --findRelatedTests --passWithNoTests "/Users/me/repo/project/src/modules/dogs/components/spots/SpotsSpotter.tsx"
ERROR: "husky:lint-staged" exited with 1.
husky - pre-commit hook exited with code 1 (error)
It's taking WAY too long when I just made a simple change in one file. Anyone know how I can track down why this is happening?
It seems like something similar was addressed here for the --watch flag: https://www.gitmemory.com/issue/facebook/jest/8276/483695303

events.js:288 Error: write EPIPE when executing tests with Jest

I have a problem when setting React Testing Library into a new project, which is a project that generates common UI components for several projects. In one of the projects it is correctly setup, but when I added React Testing Library and updated dependencies in this project, I am able to run a test but not all of them.
If I do yarn test this is the result:
Test Suites: 8 passed, 8 of 33 total
Tests: 84 passed, 84 total
Snapshots: 0 total
Time: 28 sevents.js:288
throw er; // Unhandled 'error' event
^
Error: write EPIPE
at ChildProcess.target._send (internal/child_process.js:806:20)
at ChildProcess.target.send (internal/child_process.js:677:19)
at ChildProcessWorker.send (C:\Users\user\workspace\project\node_modules\jest-worker\build\workers\ChildProcessWorker.js:291:17)
at WorkerPool.send (C:\Users\user\workspace\project\node_modules\jest-worker\build\WorkerPool.js:32:34)
at Farm._process (C:\Users\user\workspace\project\node_modules\jest-worker\build\Farm.js:129:10)
at Farm._enqueue (C:\Users\user\workspace\project\node_modules\jest-worker\build\Farm.js:152:10)
at Farm._push (C:\Users\user\workspace\project\node_modules\jest-worker\build\Farm.js:159:12)
at C:\Users\user\workspace\project\node_modules\jest-worker\build\Farm.js:90:14
at new Promise (<anonymous>)
at Farm.doWork (C:\Users\user\workspace\project\node_modules\jest-worker\build\Farm.js:56:12)
Emitted 'error' event on ChildProcess instance at:
at internal/child_process.js:810:39
at processTicksAndRejections (internal/process/task_queues.js:79:11) {
errno: 'EPIPE',
code: 'EPIPE',
syscall: 'write'
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
I tried doing rm -rf node_modules and then cleaning cache, and didn't work.
Any help?
It turns out that by changing some libraries in package.json the error dissapears:
Before (KO):
"babel-jest": "^24.9.0",
"jest": "^26.0.1",
After (OK):
"babel-jest": "^26.0.1",
"jest": "^25.5.4",
By doing yarn cache clean solved my problem. If you met pipe error similar like image below. I believe it is related to memory leaks.
In my case was the version of node. I had the version 15 (from Latest Features) and changed it to the stable version 14.17.3 (from LTS).
To know which version you have use node -v in a terminal.

tape/jest not recognizing test case

Created an app using create-react-app.
I set up a test file with the suggested convention. But when I run npm test
seems, my test case is not recognized.
FAIL src\_tests_\saga.test.js
? Test suite failed to run
Your test suite must contain at least one test.
at onResult (node_modules\jest\node_modules\jest-cli\build\TestRunner.js:1
89:18)
at process._tickCallback (internal\process\next_tick.js:103:7)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.941s
Ran all test suites.
Watch Usage
> Press o to only run tests related to changed files.
> Press p to filter by a filename regex pattern.
> Press q to quit watch mode.
> Press Enter to trigger a test run.
TAP version 13
# testflow Saga test
ok 1 take testflow
ok 2 test state should be false
ok 3 Must call bye

Why the tests are not running ? ( Golang ) - goapp test - bug?

I'm trying to run GAE tests on multiple packages. My app (testapp ) looks as below:
testapp>
README.md package1 package2
each package has two go files. One is the package itself the other is the 'test' package.
package1
$ls package1
package1.go package1_test.go
package2
$ls package2
package2.go package2_test.go
To run the tests I use
goapp test -v ./...
Output:
warning: building out-of-date packages:
github.com/mihai/API
installing these packages with 'go test -i ./...' will speed future tests.
=== RUN TestGetDiskFile
codelistgobfile.gob
codelist.gob written successfully
--- PASS: TestGetDiskFile (0.00 seconds)
PASS
ok testapp/package1 0.010s
However as you can see above it seems to run only the first test ( TestGetDiskFile ) from package1. After that it gets stuck. I get no kind of output. If I go in each package ( cd package 1 ) and run goapp test all the tests (about 20 tests) run successfully
Any idea how I can fix / run all the tests without getting stuck or at least how I can debug it further? is this a goapp bug?
I've tried on two different machines ( Mac osx , and ubuntu ), the result is same.
To debug, strip things down to a minimal test case. For example, the following is a minimal test case for go test -v ./.... Try something similar for goapp test -v ./....
$ dir
package1 package2
$ tree ../packages
../packages
├── package1
│   └── package1_test.go
└── package2
└── package2_test.go
2 directories, 2 files
$ go test -v ./...
=== RUN TestPackage1
--- PASS: TestPackage1 (0.00 seconds)
PASS
ok packages/package1 0.004s
=== RUN TestPackage2
--- PASS: TestPackage2 (0.00 seconds)
PASS
ok packages/package2 0.004s
$
File: package1_test.go:
package package1
import "testing"
func TestPackage1(t *testing.T) {}
File: package2_test.go:
package package2
import "testing"
func TestPackage2(t *testing.T) {}

Resources