I want to run a coverage report for all files in my code repo, including those that currently don't have any tests.
I'm using this command:
jest --coverage --collectCoverageFrom='src/features/**/*.{ts,tsx}
But there are other folders i want to cover.
Is it not possible to "tell" Jest to look at all .ts and .tsx files across all folders, including nested folders?
The command you have should generate coverage report for all .ts and .jxs (was that meant to be jsx?) inside the the folders in src/features folder.
If you want to include other folders, for example everything inside /src then use
jest --coverage --collectCoverageFrom='src/**/*.{ts,jxs}'
Related
I have files tests/foo.js tests/bar.js. When I run jest tests/foo.js or jest tests/*.js, no test is run. How do I run test on files not ending in .test.js?
You can define the patterns jest uses to detect test files through the testRegex or testMatch options.
Jest uses testMatch to find tests:
By default it looks for .js, .jsx, .ts and .tsx files inside of __tests__ folders, as well as any files with a suffix of .test or .spec (e.g. Component.test.js or Component.spec.js). It will also find files called test.js or spec.js.
In your case you can set testMatch to the following:
testMatch: [ "**/tests/**/*.[jt]s?(x)" ]
...and it should find the tests you have created in your tests folder.
I'm setting up a lerna monorepo with jest, I'm using jest's projects like so: projects: ['<rootDir>/packages/*'].
Running tests work as expected, however, I'm not sure how can I run a specific project? Say I have:
/packages
jest.config.js
/core
jest.config.js
/blog
jest.config.js
Currently jest runs tests in both packages using their specific configs, however, I'm not sure how can I tell jest to just run tests in one of those packages?
Assuming you want to do this with Jest's projects property:
As of Jest v26.1.0, you can now run selected projects with Jest by doing the following:
jest --selectProjects myproj
This will find any "project" in your jest.config.js by it's displayName value.
See:
https://github.com/facebook/jest/issues/7542
https://github.com/facebook/jest/pull/8612
https://github.com/facebook/jest/releases/tag/v26.1.0
You can call jest with the name of a test that you want to run. You can also use just parts of the path to the test, or even a regular expression. So in your case, you could run tests in the core package like this:
jest packages/core
There is currently no clean way of doing it from the CLI (see https://github.com/facebook/jest/issues/6189), but you can use https://github.com/rogeliog/jest-watch-select-projects to achieve it in watch mode
I'm migrating from Mocha/Chai to Jest, and I have a lot of test files. The file names are in the format componentTest.js and are all in subdirectories of /test. Jest doesn't seem to like that. Running jest /test doesn't find anything. Running jest /test/components/componentTest.js also says no tests found. Changing the /test director name doesn't work either. It's only when I change the name of the file to component.test.js that it finds the tests, but that's not going to work for my case without a script. What am I missing?
Jest will find any tests that are located inside a __tests__ folder as well.
And then to run your tests, just run: jest
I currently have my NodeJS source files in src and test suites in test, e.g.:
/src/bar/baz/foo.js
/test/bar/baz/foo.spec.js
This leads to awkward require statements like var foo = require('../../../src/bar/baz/foo'). And it's hard to see at a glance which source files are missing tests. I would like to instead keep my test suites in the same directory as the relevant source files:
/src/bar/baz/foo.js
/src/bar/baz/foo.spec.js
But now running mocha --recursive src causes errors as Mocha tries to run my source files as tests.
I've seen suggestions of using find or gulp to filter the file list but I find it surprising that this can't be done with plain Mocha. What's the recommended way of organising files this way?
Just pass the pattern of your test files to mocha, like:
mocha "src/**/*.spec.js"
This is going to run the .spec.js files in all subdirectories of src.
I'm using jasmine-node to test my Meteor application and I want to use the auto-test feature so I don't have to rerun the tests all the time by myself.
My meteor application folder structure is like this:
server
foo.coffee
tests
foo.spec.coffee
And with the spec file I want to test code which is located in foo.coffee. I start jasmine-node with this args:
jasmine-node ./ --autotest --coffee --test-dir tests
And now I would assume that the autotest feature will react on all changes in the root folder but it just reacts on changes in the test folder. And I can't start it in the root folder because I get an error in the .meteor files (and I don't want to have jasmine testing/including the meteor code anyway).
So I want to have jasmine rerun the tests even if I change code in the server folder. How can I achieve that?
Use the --watch parameter along with --autotest and specify the directories that contain whatever files you want to have watched.