Skip to content

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

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

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

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tests for the changes have been added (for bug fixes / features) — deferred (see Other information)
  • Docs have been added / updated (for bug fixes / features) — README update can land alongside follow-up packages

PR Type

What kind of change does this PR introduce?

[ ] 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.

Structural lessons from the closed #2229: that PR had four blockers, all addressed here:

Issue in #2229 Fix in this PR
Schematics TS sources never compiled to dist/ (not in any tsconfig include) New tsconfig.schematics.json (rootDir src/schematics, outDir dist/schematics) wired into the package build script alongside the existing tsc -p tsconfig.lib.json.
collection.json referenced paths like ./schematics/ng-add/index that don't resolve after compile collection.json lives in src/schematics/ and is copied via copy:schematics; factory path is ./ng-add/index#default, correct relative to the dist'd collection.json.
Cross-package source-tree imports (../../../common/schematics/...) Rule uses only @angular-devkit/schematics + @angular-devkit/core (already direct deps). No source-tree imports across packages.
No test coverage committed A SchematicTestRunner-based spec is staged out in code comments. See "Test deferral" below.

Test deferral: @angular-devkit/schematics/testing transitively imports ora@9 (ESM-only). The repo's current ts-jest config (used for unit tests) rejects it with "Cannot use import statement outside a module". A follow-up PR will adjust transformIgnorePatterns / moduleNameMapper and add the spec — the assertions it should cover are enumerated in a TODO at the top of ng-add/index.ts.

Verification: yarn workspace @angular-builders/jest build produces:

  • dist/schematics/collection.json
  • dist/schematics/ng-add/index.js (default export = Rule factory, verified via require())
  • dist/schematics/ng-add/schema.json

and the existing 35 unit tests still pass.

Follow-ups (separate PRs):

  • Add ng-add schematics for @angular-builders/custom-webpack and @angular-builders/custom-esbuild following the same structural pattern.
  • Add the SchematicTestRunner unit spec for jest ng-add (requires Jest ESM transform config fix).
  • Version migrations (e.g. v18 → v21) — out of scope here.

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.
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