Skip to content

feat(jest): add ng-add schematic (#22)#2240

Closed
just-jeb wants to merge 2 commits into
masterfrom
feat/jest-schematics-ng-add
Closed

feat(jest): add ng-add schematic (#22)#2240
just-jeb wants to merge 2 commits into
masterfrom
feat/jest-schematics-ng-add

Conversation

@just-jeb
Copy link
Copy Markdown
Owner

@just-jeb just-jeb commented May 17, 2026

PR Checklist

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

Users must manually configure @angular-builders/jest by editing angular.json, swapping the karma builder for @angular-builders/jest:run, removing karma/jasmine devDependencies, deleting karma.conf.js / src/test.ts, and updating tsconfig.spec.json types. There is no ng add support.

Issue Number: #22

What is the new behavior?

Running ng add @angular-builders/jest now:

  1. Rewrites architect.test.builder to @angular-builders/jest:run (clears karma-specific options when the previous builder was karma).
  2. Removes the standard karma/jasmine devDependencies (karma, karma-chrome-launcher, karma-coverage, karma-jasmine, karma-jasmine-html-reporter, jasmine-core, @types/jasmine).
  3. Adds @angular-builders/jest to devDependencies pinned to its own version.
  4. Deletes karma.conf.js and src/test.ts when present.
  5. Updates tsconfig.spec.json so types swaps jasminejest and the files: ["src/test.ts"] entry is dropped (textual swap so JSONC comments survive).
  6. Schedules a NodePackageInstallTask (skippable via --skip-install).

The Rule is exported as default from dist/schematics/ng-add/index.js, and package.json declares "schematics": "./dist/schematics/collection.json".

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

Scope: intentionally narrow — @angular-builders/jest only, ng-add only. Other packages and version migrations are follow-up PRs (see #2241).

Build wiring: schematics compile via a dedicated tsconfig.schematics.json (rootDir src/schematics, outDir dist/schematics), wired into the package build script alongside tsc -p tsconfig.lib.json. collection.json lives in src/schematics/ and is copied to dist/ via copy:schematics; the factory path is ./ng-add/index#default. The Rule uses only @angular-devkit/schematics + @angular-devkit/core — no cross-package source imports.

Tests: a SchematicTestRunner-based spec is included (ng-add/index.spec.ts). Because @angular-devkit/schematics/testing transitively imports ora (ESM-only), the spec runs under ts-jest via a local jest-ora-mock.cjs stub wired through moduleNameMapper.

Verification: yarn workspace @angular-builders/jest build produces dist/schematics/collection.json, dist/schematics/ng-add/index.js (default export = Rule factory), and dist/schematics/ng-add/schema.json; existing unit tests pass.

Introduces an `ng add @angular-builders/jest` schematic that swaps a project's
test builder from `@angular-devkit/build-angular:karma` to `@angular-builders/jest:run`,
removes karma/jasmine devDependencies, deletes karma.conf.js and src/test.ts,
updates tsconfig.spec.json types from jasmine to jest, and schedules a package
install.

Scope is intentionally narrow: jest package only, ng-add only. Migrations and
schematics for the other builder packages will land in follow-up PRs.

Structural fixes vs. the closed #2229:
- Schematics compile via a dedicated `tsconfig.schematics.json` (rootDir
  src/schematics, outDir dist/schematics) wired into the package build script,
  so JS output actually ships.
- `collection.json` lives under `src/schematics/` and is copied to dist via a
  new `copy:schematics` script. Factory path is `./ng-add/index#default`,
  relative to the dist'd collection.json (not the source tree).
- `package.json` `schematics` field points at `./dist/schematics/collection.json`.
- No cross-package source-tree imports; the ng-add Rule uses only
  `@angular-devkit/schematics` + `@angular-devkit/core` (already direct deps).
- Default export is the Rule factory, as Angular's schematics runner expects.

Test coverage is deferred to a follow-up because
`@angular-devkit/schematics/testing` transitively imports `ora@9` (ESM-only)
which the repo's current ts-jest config cannot load. The follow-up will adjust
`transformIgnorePatterns` and add `SchematicTestRunner`-based specs. A TODO
in ng-add/index.ts enumerates the assertions to add.
Add index.spec.ts covering all 5 assertions from the deferred TODO:
builder rewrite, karma dep removal, file deletion, tsconfig update,
and NodePackageInstallTask scheduling (with skipInstall guard).

Fix the ora ESM blocker by adding a CJS no-op moduleNameMapper entry
in jest-common.config.js and a jest-ora-mock.cjs stub at repo root.
@just-jeb
Copy link
Copy Markdown
Owner Author

just-jeb commented Jun 3, 2026

Superseded by #2267, which consolidates ng-add/ng-update schematics for all builders (jest, custom-esbuild, custom-webpack) in a single PR targeting release/v22.

@just-jeb just-jeb closed this Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant