feat(jest): add ng-add schematic (#22)#2240
Open
just-jeb wants to merge 2 commits into
Open
Conversation
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.
2 tasks
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Users must manually configure
@angular-builders/jestby editingangular.json, swapping the karma builder for@angular-builders/jest:run, removing karma/jasmine devDependencies, deletingkarma.conf.js/src/test.ts, and updatingtsconfig.spec.jsontypes. There is nong addsupport.Issue Number: #22
What is the new behavior?
Running
ng add @angular-builders/jestnow:architect.test.builderto@angular-builders/jest:run(clears karma-specific options when the previous builder was karma).karma,karma-chrome-launcher,karma-coverage,karma-jasmine,karma-jasmine-html-reporter,jasmine-core,@types/jasmine).@angular-builders/jesttodevDependenciespinned to its own version.karma.conf.jsandsrc/test.tswhen present.tsconfig.spec.jsonsotypesswapsjasmine→jestand thefiles: ["src/test.ts"]entry is dropped (textual swap so JSONC comments survive).NodePackageInstallTask(skippable via--skip-install).The Rule is exported as
defaultfromdist/schematics/ng-add/index.js, andpackage.jsondeclares"schematics": "./dist/schematics/collection.json".Does this PR introduce a breaking change?
Other information
Scope: intentionally narrow —
@angular-builders/jestonly,ng-addonly. Other packages and version migrations are follow-up PRs.Structural lessons from the closed #2229: that PR had four blockers, all addressed here:
dist/(not in anytsconfiginclude)tsconfig.schematics.json(rootDirsrc/schematics, outDirdist/schematics) wired into the packagebuildscript alongside the existingtsc -p tsconfig.lib.json.collection.jsonreferenced paths like./schematics/ng-add/indexthat don't resolve after compilecollection.jsonlives insrc/schematics/and is copied viacopy:schematics; factory path is./ng-add/index#default, correct relative to the dist'dcollection.json.../../../common/schematics/...)@angular-devkit/schematics+@angular-devkit/core(already direct deps). No source-tree imports across packages.SchematicTestRunner-based spec is staged out in code comments. See "Test deferral" below.Test deferral:
@angular-devkit/schematics/testingtransitively importsora@9(ESM-only). The repo's currentts-jestconfig (used for unit tests) rejects it with "Cannot use import statement outside a module". A follow-up PR will adjusttransformIgnorePatterns/moduleNameMapperand add the spec — the assertions it should cover are enumerated in a TODO at the top ofng-add/index.ts.Verification:
yarn workspace @angular-builders/jest buildproduces:dist/schematics/collection.jsondist/schematics/ng-add/index.js(default export = Rule factory, verified viarequire())dist/schematics/ng-add/schema.jsonand the existing 35 unit tests still pass.
Follow-ups (separate PRs):
ng-addschematics for@angular-builders/custom-webpackand@angular-builders/custom-esbuildfollowing the same structural pattern.SchematicTestRunnerunit spec for jest ng-add (requires Jest ESM transform config fix).