Jest testPathPattern only matching first file - jestjs

I have following tests
libs/app1/e2e/my-e2e-tests.spec.ts
libs/app1/uat/my-uat-test.spec.ts
libs/app2/e2e/my-e2e-tests.spec.ts
libs/app2/uat/my-uat-test.spec.ts
I want a command to run all uat and e2e tests under libs/app1. Tried the following
jest --testPathPattern=libs/app1/**/*.spec.ts
But above command only run the first match which is 'libs/app1/e2e/my-e2e-tests.spec.ts'.
I also tried to provide a regular expression
jest --testPathPattern=/libs\/app1\/[a-z]*\/[a-z]*.spec.ts/ig

testPathPattern uses a regexp pattern, not a glob pattern. Remove the /**/*.spec.ts part.
Try (macOS):
$ npx jest --testPathPattern=libs/app1
PASS libs/app1/e2e/my-e2e-tests.spec.ts
PASS libs/app1/uat/my-uat-test.spec.ts
Test Suites: 2 passed, 2 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 1.685 s, estimated 20 s
Ran all test suites matching /libs\/app1/i.

Related

Use jq to print only the percentages of coverage from a json-summary file to markdown in github actions

I want to pretty print my json-coverage jest results to github actions. I have a ci.yml file with the following:
- name: Run Unit Tests
run: |
yarn test:next:unit --runInBand --coverage --coverageReporters="text-summary"
- name: Echo Coverage
run: |
cat coverage/coverage-summary.json | head -1 > coverage-testing/coverage.json
This prints the first line of jest's coverage-summary in the job terminal and it looks like this:
{"total": {"lines":{"total":18326,"covered":11,"skipped":0,"pct":0.06},"statements":{"total":62625,"covered":11,"skipped":0,"pct":0.01},"functions":{"total":19047,"covered":4,"skipped":0,"pct":0.02},"branches":{"total":60202,"covered":11,"skipped":0,"pct":0.01},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}
}
I can print this directly to my github action run in the UI by adding this to the ci.yml:
- name: Print to github
run: cat coverage-testing/coverage.json >> $GITHUB_STEP_SUMMARY
and it shows up in github action run under annotations
jest-next summary
{"total": {"lines":{"total":52,"covered":11,"skipped":0,"pct":21.15},"statements":{"total":54,"covered":11,"skipped":0,"pct":20.37},"functions":{"total":16,"covered":4,"skipped":0,"pct":25},"branches":{"total":18,"covered":11,"skipped":0,"pct":61.11},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}
Job summary generated at run-time
Now for the pretty printing. I've tried some various jq commands including iterating with to_entries in a way that would make the github action job summary look more like markdown. I got errors because the array was nested and I got those errors to go away by changing -r to -R and adding try, but then nothing printed to github. Since these attempts are all in CI and the jest tests are flaky, changing one thing and waiting is getting cumbersome. Does anyone happen to have a script that does this already? Not picky about how it's formatted, I'd just like it to look less like json and more like markdown.
You can pretty quickly present it as a list of path/value pairs by streaming in the input. You could then join the paths to a more readable form and dump it.
$ jq -r --stream 'select(length == 2)|"\(.[0]|join("_")): \t\(.[1])"' input.json
total_lines_total: 18326
total_lines_covered: 11
total_lines_skipped: 0
total_lines_pct: 0.06
total_statements_total: 62625
total_statements_covered: 11
total_statements_skipped: 0
total_statements_pct: 0.01
total_functions_total: 19047
total_functions_covered: 4
total_functions_skipped: 0
total_functions_pct: 0.02
total_branches_total: 60202
total_branches_covered: 11
total_branches_skipped: 0
total_branches_pct: 0.01
total_branchesTrue_total: 0
total_branchesTrue_covered: 0
total_branchesTrue_skipped: 0
total_branchesTrue_pct: 100
If you want to retain some structure, you'll need to decide on that first...

Jest "No tests found" with folder special characters

When I prefix test folders with special characters - e.g. /src/tests/+something/a.spec.ts or /src/tests/$something/a.test.ts - the tests are not discovered. I tried to adjust testMatch to wildcard anything but Jest still does not find the tests.
Is there anything particular that needs to be set to support folder with special characters with Jest?
❯ npm run test /Users/david/app/src/tests/\\+public/app.services.spec.ts
dapp#2.0.3 test
TZ=UTC jest /Users/david/app/src/tests/\+public/app.services.spec.ts
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In /Users/david/app/
1015 files checked.
testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 260 matches
testPathIgnorePatterns: /node_modules/ - 1015 matches
testRegex: - 0 matches
Pattern: /Users/david/app/src/tests/\+public/app.services.spec.ts - 0 matches

How to run an individual test with Stack and Haskell Test.Framework?

I'm cloning the following repository and making one change to the stack.yaml by adding at the end:
docker:
enable: true
To run all the tests for haskoin-core I'm using
stack test haskoin-core:test-haskoin-core
What I want to do is run just one test. If this were HSpec (which it is not) I'd be running something like:
stack test --test-arguments -m "Network.Haskoin.Network.Units"
Now what I could do is modify the file haskcoin-core/test/Main.hs and comment out all the tests I don't want to run. But ya know - there should be a simpler way to run it with just command line parameters. (Mutating the file system goes agains the whole functional grain of Haskell).
I'd also be open to running it with stack ghci somehow.
My question is: How to run an individual test with Stack and Haskell Test.Framework?
As of mid 2019, I think stack has changed.
See --help for options:
stack test --test-arguments "--help"
Do a --dry-run to see what tests will run:
stack test --test-arguments "--dry-run"
Select tests with --match. Note, this alleges glob patterns will work but they don't seem to for me:
stack test --test-arguments "--match=foobar"
AFAICT, "foobar" is interpreted like a glob of *foobar*, but I can't be explicit about it.
Thanks to #sjakobi for this answer.
The process is - list the available test commands:
stack test --test-arguments "--help" haskoin-core:test-haskoin-core
This gives the following result:
haskoin-core-0.4.2: test (suite: test-haskoin-core, args: --help)
Usage: test-haskoin-core [OPTIONS]
--help show this help message
-j NUMBER --threads=NUMBER number of threads to use to run tests
--test-seed=NUMBER|random default seed for test random number generator
-a NUMBER --maximum-generated-tests=NUMBER how many automated tests something like QuickCheck should try, by default
--maximum-unsuitable-generated-tests=NUMBER how many unsuitable candidate tests something like QuickCheck should endure before giving up, by default
-s NUMBER --maximum-test-size=NUMBER to what size something like QuickCheck should test the properties, by default
-d NUMBER --maximum-test-depth=NUMBER to what depth something like SmallCheck should test the properties, by default
-o NUMBER --timeout=NUMBER how many seconds a test should be run for before giving up, by default
--no-timeout specifies that tests should be run without a timeout, by default
-l --list-tests list available tests but don't run any; useful to guide subsequent --select-tests
-t TEST-PATTERN --select-tests=TEST-PATTERN only tests that match at least one glob pattern given by an instance of this argument will be run
--jxml=FILE write a JUnit XML summary of the output to FILE
--jxml-nested use nested testsuites to represent groups in JUnit XML (not standards compliant)
--plain do not use any ANSI terminal features to display the test run
--color use ANSI terminal features to display the test run
--hide-successes hide sucessful tests, and only show failures
Test suite failure for package haskoin-core-0.4.2
test-haskoin-core: exited with: ExitFailure 1
Logs printed to console
From that we can build a command to list the tests:
stack test --test-arguments "--list-tests" haskoin-core:test-haskoin-core
From there we can use this command to glob for a particular test
stack test --test-arguments=--select-tests=Bloom*Filter haskoin-core:test-haskoin-core
Note the * in place of the space, there seems to be some discussion about how to handle spaces in this scenario.
Now this selects the tests we want to run:
haskoin-core-0.4.2: test (suite: test-haskoin-core, args: --select-tests=Bloom*Filter)
Binary encoding and decoding of bloom types:
BloomFilter: [OK, passed 100 tests]
Bloom Filters:
Bloom Filter Vector 1: [OK]
Bloom Filter Vector 2: [OK]
Bloom Filter Vector 3: [OK]
Properties Test Cases Total
Passed 1 3 4
Failed 0 0 0
Total 1 3 4

Explanation of `stack test --test-arguments "-m "<filter>""`

On other forum pages, I've seen that I can run specific hspec tests with the command
stack test --test-arguments "-m "<filter>""
where <filter> is set to match it's textual descriptor for the intended tests. I frequently use this command to run single tests of interest without having to run the entire test suite.
But I do not understand how exactly this command specifies that. For instance, what kind of argument is "-m "<filter>"", and what does that "-m" mean? What other arguments can be specified as the "-m "<filter>"" is here?
About --test-arguments, stack test --help | less says only
--ta,--test-arguments TEST_ARGS
Arguments passed in to the test suite program
which is the only relevant documentation I have found on how to specify stack to run a subset of tests.

Tell Mocha where to look for test files

How can I tell Mocha where to look for my test files? My test files dont reside in the standard project root ./test folder. My test sit in Modules/.*/Tests/.
I've attempted to use the --grep command line argument but I have a feeling this argument looks for test NAMES not for test FILE NAMES.
mocha --grep Modules\/.*\/Tests
The above command gives the error:
Warning: Could not find any test files matching pattern: test
No test files found
You have to match not only directory but also its files . Besides, do no need grep , use only -- :
mocha -- Modules/**/Tests/*.js
// or `mocha -- Modules/Tests/*.js` (I am not sure because you didn't give the project hierarchy )

Resources