website/docs/guides/examples/jest.mdx
import AddDepsTabs from '@site/src/components/AddDepsTabs'; import HeadingApiLink from '@site/src/components/Docs/HeadingApiLink';
<HeadingApiLink to="https://github.com/moonrepo/examples/blob/master/.moon/tasks/node.yml#L83" />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.
Since testing is a universal workflow, add a test task to .moon/tasks/**/*
with the following parameters.
tasks:
test:
command:
- 'jest'
# Always run code coverage
- '--coverage'
# Dont fail if a project has no tests
- '--passWithNoTests'
inputs:
# 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.
tasks:
test:
args:
# 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
--presetoption in the task above, so that all projects inherit the preset by default.
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
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.