Run specific jest project - jestjs

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

Related

How do I generate code coverage for some Node.JS code without tests?

I have a build/ folder that gets autogenerated by a babel process in a package.json. It has several .js files, including in sub-folders. In the root is a file, main.js, which is something of a demo / testbed for the project, that instantiates various ES6 classes and tries out various functions. It currently runs without crashing.
Call it a poor man's end-2-end test. I'm trying to move quickly with what could be throw away code.
I don't have any formal tests. I don't want to write any formal tests for this codebase. But I am interested in knowing how much of the code in build/ is being touched currently by my demo, main.js.
How can I generate a code coverage report for this scenario, using nyc?
If that's not actually easy (all tutorials I see seem to involve instrumenting pre-existing unit tests from a mainstream testing framework), what nyc alternative would make this easy?
I tried
npm install nyc --save-dev
npx nyc node build/main.js
but it claimed 0 lines/files.
With thanks to What is instrumentation in nyc istanbul?, it was actually simple. From the root, where my package.json and build/ folder were:
npx nyc instrument build coverage
npx nyc --reporter=text --report-dir=./nyc_output node build/main.js
all necessary folders (coverage, nyc_output) were auto-created (though it made .nyc_output/ for some reason)

Jest: Run ESLint after tests using --watch

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

Setup jest multi projects with create-react-app and node

My projects uses a /server folder for my backend code and /react-ui for my client side code. There's a package.json in each folder. I can run test separately in the cmd line, but I would like to run both at the same time. I know about the multi projects feature of jest, but it doesn't seem to work with create-react-app. I'm trying to setup jest with babel as if I was not using create-react-app, but it seems like the wrong approach considering jest is already setup in CRA.
My current setup runs from the /server jest installation. With projects: ['<rootDir>', '<rootDir>/../react-ui']. The jest documentation isn't clear how I could direct it to run npm test in /react-ui
My only goal is to be able to watch both at the same time and I would like to not eject from CRA.
You can have (gulp) scripts on the main level that run each tests separately, but using same configs.

mocha sidebar is not showing tests

I'm developing a very simple typescript project and I added a very tiny test with mocha. I installed mocha sidebar on VS Code and all it's dependencies, but test are not showing in the left panel.
The strange is that when I press debug button, my test run without problem (?)
I want to distribute this package on NPM, and only distribute .js, .d.ts, js.map and d.ts.map files.
I think the problem is in my project.json, but I cannot figure what is it. The source code is here
The only change I needed was in the VS code workspace settings: Under extensions I pointed the 'mocha glob files'-setting to my test scripts
One thing that I notice from your repo is you have package.json, test folder and another src folder inside type-exception/src.
I can run the mocha sidebar plugin successfully.
My solution is to open the project in vscode from the type-exception/src folder not type-exception folder.
My file structure
My mocha sidebar
I also saw that you have correct vscode workspace settings that set mocha files to
{
"mocha.files.glob": "lib/test/**/*.js"
}
Hope it helps
I had the Testing view showing Jest tests and realised that for Mocha I needed to install another VSCode Extension called "Mocha Test Explorer": https://marketplace.visualstudio.com/items?itemName=hbenl.vscode-mocha-test-adapter
Once installed I created/edited a .vscode/settings.json file in my project folder and added the following clues to tell Mocha to find my tests under the tests folder:
{
"mochaExplorer.files": "tests/**/*.ts"
}

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.

Resources