Check Jest dependencies in CI
A Jest preset names packages, such as transformers and watch plugins, that your project has to install. When one is
missing, Jest fails, or it finds a copy that another package pulled in and the next install removes.
buddy check-deps exits 1 in that case, so it can stop a build.
check-deps is a command you run. It is not an install hook.
-
Install
repobuddy.Terminal window pnpm add -D repobuddy -
Run the check once and install what it reports.
Terminal window pnpm exec buddy check-depsmissing dependencies: 2 used by jest.config.mjs but not declared in package.jsonjest-esm-transformer-2 preset(@repobuddy/jest/presets/ts) transformts-jest preset(@repobuddy/jest/presets/ts) transforminstall: npm i -D jest-esm-transformer-2 ts-jestThe install line uses the
packageManagerfield ofpackage.json. Add that field to get apnpm add -D,yarn add -D, orbun add -dline. -
Run it again until it prints
all declared in package.json. A preset can ask for different packages once others are installed. -
Add a script for it in
package.json.{"scripts": {"check-deps": "buddy check-deps"}} -
Run the script in CI, before the tests.
- run: pnpm install --frozen-lockfile- run: pnpm check-deps- run: pnpm test -
In a workspace, check each package that has a Jest config.
- run: pnpm exec buddy check-deps --cwd packages/app- run: pnpm exec buddy check-deps --cwd packages/libA package with no Jest config passes with
dependencies: no jest configuration found, nothing to check.
Run it before tests on your machine
Section titled “Run it before tests on your machine”To run it before every local test run, name it pretest instead of check-deps. npm runs a pretest script before
test. Check that your package manager runs pre scripts before you rely on this.
Verify
Section titled “Verify”- Remove a package that the check reported from
devDependencies. - Run
pnpm check-deps. It lists the package and exits1. - Put the package back. The check prints
all declared in package.jsonand exits0.