Skip to main content

Jest example

In this guide, you'll learn how to integrate Jest into moon.

Begin by installing jest in your root. We suggest using the same version across the entire repository.

yarn add --dev jest


Since testing is a universal workflow, add a test task to .moon/tasks/node.yml with the following parameters.

- 'jest'
# Always run code coverage
- '--coverage'
# Dont fail if a project has no tests
- '--passWithNoTests'
# Source and test files
- 'src/**/*'
- 'tests/**/*'
# Project configs, any format
- 'jest.config.*'

Projects can extend this task and provide additional parameters if need be, for example.

# Disable caching for this project
- '--no-cache'



A root-level Jest config is not required and should be avoided, instead, use a preset to share configuration.


A project-level Jest config can be utilized by creating a jest.config.<js|ts|cjs|mjs> in the project root. This is optional, but necessary when defining project specific settings.

module.exports = {
// Project specific settings
testEnvironment: 'node',


To share configuration across projects, you can utilize Jest's built-in preset functionality. If you're utilizing package workspaces, create a local package with the following content, otherwise publish the npm package for consumption.

module.exports = {
testEnvironment: 'jsdom',
watchman: true,

Within your project-level Jest config, you can extend the preset to inherit the settings.

module.exports = {
preset: 'company-jest-preset',

You can take this a step further by passing the --preset option in the task above, so that all projects inherit the preset by default.


How to test a single file or folder?

You can filter tests by passing a file name, folder name, glob, or regex pattern after --. Any passed files are relative from the project's root, regardless of where the moon command is being ran.

$ moon run <project>:test -- filename

How to use projects?

With moon, there's no reason to use projects as the test task is ran per project. If you'd like to test multiple projects, use moon run :test.