Jest: Run ESLint after tests using --watch - jestjs

Is there a way to run ESLint everytime Jest's watch script triggers? I want to automatically have the watch script lint (with --fix) my files.

You can use jest-runner-eslint plugin to do exactly what you described.
https://github.com/jest-community/jest-runner-eslint

Related

How to prevent testing-libarary running coverage on untested files every time I save my test file?

I am using the test-library for React , and everytime I edit my test file and save it, it will automatically running coverage on other untested files and gave me a report. there are hundreds of files in my project,and it could really waste time. I don't want this, I just want it run test on my current selected file.
the command I have used is
npm run test /myprojrct/myFile.test.js
is there a way to resolve it ?
Adding the --watch flag to your test script should make jest run tests only on changed files after each save.
More details and other available options can be found here: https://jestjs.io/docs/en/cli#--watch
You should add --coverage=false flag to prevent collecting coverage.
npm run test --coverage=false
Other useful flags:
npm run test --watch // watcher will re-run only the tests that depend on the changed files
npm run test --watchAll // watcher will re-run *ALL* tests when a file has changed
npm run test -t="rendering Button" // will run only tests with a name that matches the regex
npm run test fileName.js // this argument will be treated as a regular expression to match against files in your project

Use jest "watch mode" with nx test (nx CLI)

I have an app that was generated with Nx CLI. I can run tests for this app with Jest using the the command nx test myApp. This works fine. However, I would like to use Jest's "watch mode". when running my tests, and can't figure out how to achieve this. Nx's documentation says simply:
To run unit tests for your application:
nx test myapp
I have been unable to find any more detailed documentation for this command, or how to add any other options/flags to it. Is there anyway to use watch mode with nx test?
TL;DR: yarn affected:test --all --parallel --maxParallel 10 --watch should work.
nx test actually runs ng test which in its turn is using the #nrwl/jest:jest builder that has a --watch option (in addition to some other jest specific options). You can see all the options by running nx test --help
Warning: If you have multiple apps/libs in your repository, this will only watch your default project's tests.
If you really want to watch all your projects tests, you will have to run:
yarn affected:test --all --parallel --maxParallel 10 --watch
--all we need this otherwise, this will not run tests on projects that you "affect" after running the command
--parallel because you want to run all the tests in parallel, otherwise this will only watch the first project in the list
--maxParallel because the default limit is 3 so if you have more than 3 projects, it will only watch tests for the 3 first projects
A simple command to run test for your applications with watch is
nx test <packageName> --watch
If you want to run test for a specific file in watch mode.
nx test <packageName> --testFile=<fileName> --watch
If you want to clear cache
nx test <packageName> --clearCache
If all your projects are using jest, there should be no issues with simply running:
jest --watch
Running adding a script for that and running it in the root directory works pretty well for me!

Run specific jest project

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

Flow type annotations are breaking jest tests

I'm wondering if I could get some advice on how to get around this. I'm using flow and jest on a library, but the flow type annotations are causing all of my tests to fail.
I have babel flow plugin, so I can run the tests from the transcoded dist folder, however this breaks any real time testing capability and is very cumbersome.
I feel like I'm missing something obvious here, but if anyone could explain to me how to strip flow anotations at the time of testing that would be amazing!
Thanks
I think the problem you're describing can be fixed by using babel-jest, which teaches jest to first run your files through babel and then execute them.
I would try babel-jest and babel-plugin-transform-flow-strip-types.
Just install them with yarn add --dev babel-jest babel-plugin-transform-flow-strip-types and then create .babelrc file with:
{
"plugins": ["transform-flow-strip-types"]
}
Now your Jest based tests should work without any other configuration. Just execute yarn jest or whatever command you used to use to start them.

Grunt: documentation on how to run these before main program

I want to run my app like normal:
node app.js
but before that happens, I want to ensure that a particular grunt task is run first.
I am having trouble finding documentation on how to automatically run (and complete) a Grunt task before starting a node command.
To be more specific, I have TypeScript files that I want/need to compile before running my node.js application.
Is there a way to always run the compilation (with Grunt or another way) before running node? Perhaps I could do a simple synchronous call in my app.js to run Grunt, but that wouldn't be cool would it?
You could use grunt to automatically compile typescript into a folder. Then you use nodemon in another window which is watching for changes in that folder.
Then when you change the typescript files, grunt runs (if you have set up grunt-watch) and after compile nodemon restarts the application.
You can run any scenario with Grunt. Here, you can use both grunt-ts and grunt-shell tasks to run your program/server.
First one compiles/transpiles your code. Second one allows you to run any shell command, such as node my_file.js.

Resources