From 8d98aed1d5f44062609b4637241af8e9629b6adc Mon Sep 17 00:00:00 2001 From: Corinne PAULVE Date: Tue, 17 Dec 2024 12:40:41 +0100 Subject: [PATCH] fix: the tsconfig spec generated for library contains several issues --- .../components/schematics/index.it.spec.ts | 13 +++- .../configuration/schematics/index.it.spec.ts | 15 +++- packages/@o3r/testing/package.json | 3 + .../@o3r/testing/schematics/ng-add/index.ts | 5 +- .../workspace/tsconfig.jest.json.template | 11 +++ packages/@o3r/workspace/package.json | 1 + .../workspace/schematics/index.it.spec.ts | 2 + .../schematics/library/index.spec.ts | 53 +++++++++++-- .../schematics/library/rules/rules.ng.ts | 78 +++++++++++++++++-- .../schematics/library/rules/rules.nx.ts | 14 +++- .../schematics/library/rules/shared.ts | 21 +++-- .../{ng => jest}/jest.config.js.template | 2 +- .../testing/setup-jest.ts.template | 0 .../ng/{ => common}/.gitignore.template | 0 .../ng/{ => common}/.npmignore.template | 0 .../tsconfig.builders.json.template | 0 .../ng/{ => common}/tsconfig.json.template | 0 .../tsconfig.build.composite.json.template | 6 ++ .../ng/{ => jest}/tsconfig.spec.json.template | 2 +- .../templates/nx/jest.config.js.template | 38 --------- .../nx/testing/setup-jest.ts.template | 2 - 21 files changed, 199 insertions(+), 67 deletions(-) create mode 100644 packages/@o3r/testing/schematics/ng-add/templates/workspace/tsconfig.jest.json.template rename packages/@o3r/workspace/schematics/library/templates/{ng => jest}/jest.config.js.template (98%) rename packages/@o3r/workspace/schematics/library/templates/{ng => jest}/testing/setup-jest.ts.template (100%) rename packages/@o3r/workspace/schematics/library/templates/ng/{ => common}/.gitignore.template (100%) rename packages/@o3r/workspace/schematics/library/templates/ng/{ => common}/.npmignore.template (100%) rename packages/@o3r/workspace/schematics/library/templates/ng/{ => common}/tsconfig.builders.json.template (100%) rename packages/@o3r/workspace/schematics/library/templates/ng/{ => common}/tsconfig.json.template (100%) create mode 100644 packages/@o3r/workspace/schematics/library/templates/ng/jest/tsconfig.build.composite.json.template rename packages/@o3r/workspace/schematics/library/templates/ng/{ => jest}/tsconfig.spec.json.template (92%) delete mode 100644 packages/@o3r/workspace/schematics/library/templates/nx/jest.config.js.template delete mode 100644 packages/@o3r/workspace/schematics/library/templates/nx/testing/setup-jest.ts.template diff --git a/packages/@o3r/components/schematics/index.it.spec.ts b/packages/@o3r/components/schematics/index.it.spec.ts index 72f10d9b12..8a5d73521d 100644 --- a/packages/@o3r/components/schematics/index.it.spec.ts +++ b/packages/@o3r/components/schematics/index.it.spec.ts @@ -5,6 +5,7 @@ */ const o3rEnvironment = globalThis.o3rEnvironment; +import fs from 'node:fs'; import * as path from 'node:path'; import { getDefaultExecSyncOptions, @@ -50,7 +51,17 @@ describe('ng add components', () => { expect(diff.modified).toContain('package.json'); expect(diff.modified).toContain('angular.json'); expect(diff.modified).toContain('libs/test-lib/package.json'); - expect(diff.modified.length).toBe(8); + const vscodeContent = fs.readFileSync(`${workspacePath}/.vscode/extensions.json`, 'utf8'); + const angularJSON = JSON.parse(fs.readFileSync(`${workspacePath}/angular.json`, 'utf8')); + expect(vscodeContent).toContain('"Orta.vscode-jest"'); + expect(angularJSON.schematics['@o3r/core:component']).toBeDefined(); + expect(angularJSON.schematics['@o3r/core:component-container']).toBeDefined(); + expect(angularJSON.schematics['@o3r/core:component-presenter']).toBeDefined(); + expect(angularJSON.cli?.schematicCollections?.indexOf('@o3r/components') > -1).toBe(true); + + const packageJson = JSON.parse(fs.readFileSync(`${workspacePath}/package.json`, 'utf8')); + expect(packageJson.dependencies['@o3r/components']).toBeDefined(); + expect(diff.added).toContain('libs/test-lib/cms.json'); expect(diff.added).toContain('libs/test-lib/placeholders.metadata.json'); expect(diff.added).toContain('libs/test-lib/tsconfig.cms.json'); diff --git a/packages/@o3r/configuration/schematics/index.it.spec.ts b/packages/@o3r/configuration/schematics/index.it.spec.ts index 9916152068..f868b4e24a 100644 --- a/packages/@o3r/configuration/schematics/index.it.spec.ts +++ b/packages/@o3r/configuration/schematics/index.it.spec.ts @@ -5,6 +5,7 @@ */ const o3rEnvironment = globalThis.o3rEnvironment; +import * as fs from 'node:fs'; import * as path from 'node:path'; import { addImportToAppModule, @@ -49,7 +50,7 @@ describe('new otter application with configuration', () => { expect(() => packageManagerRunOnProject(appName, isInWorkspace, { script: 'build' }, execAppOptions)).not.toThrow(); }); test('should add configuration to a library', () => { - const { applicationPath, workspacePath, libName, libraryPath, isInWorkspace, untouchedProjectsPaths, o3rVersion } = o3rEnvironment.testEnvironment; + const { workspacePath, untouchedProjectsPaths, isInWorkspace, applicationPath, libName, libraryPath, o3rVersion } = o3rEnvironment.testEnvironment; const execAppOptions = { ...getDefaultExecSyncOptions(), cwd: workspacePath }; const relativeLibraryPath = path.relative(workspacePath, libraryPath); packageManagerExec({ script: 'ng', args: ['add', `@o3r/configuration@${o3rVersion}`, '--skip-confirmation', '--project-name', libName] }, execAppOptions); @@ -66,8 +67,16 @@ describe('new otter application with configuration', () => { const diff = getGitDiff(workspacePath); expect(diff.added.length).toEqual(20); - expect(diff.modified.length).toEqual(7); - expect(diff.modified).toContain('package.json'); + const vscodeContent = fs.readFileSync(`${workspacePath}/.vscode/extensions.json`, 'utf8'); + const angularJSON = JSON.parse(fs.readFileSync(`${workspacePath}/angular.json`, 'utf8')); + expect(vscodeContent).toContain('"Orta.vscode-jest"'); + expect(angularJSON.schematics['@o3r/core:component']).toBeDefined(); + expect(angularJSON.schematics['@o3r/core:component-container']).toBeDefined(); + expect(angularJSON.schematics['@o3r/core:component-presenter']).toBeDefined(); + expect(angularJSON.cli?.schematicCollections?.indexOf('@o3r/configuration') > -1).toBe(true); + + const packageJson = JSON.parse(fs.readFileSync(`${workspacePath}/package.json`, 'utf8')); + expect(packageJson.dependencies['@o3r/configuration']).toBeDefined(); expect(diff.added).toContain(path.posix.join(relativeLibraryPath, 'src/components/test-component/test-component.config.ts').replace(/[/\\]+/g, '/')); expect(diff.added).toContain(path.posix.join(relativeLibraryPath, 'src/components/test-signal/test-signal.config.ts').replace(/[/\\]+/g, '/')); diff --git a/packages/@o3r/testing/package.json b/packages/@o3r/testing/package.json index 21e5a12b83..964630802f 100644 --- a/packages/@o3r/testing/package.json +++ b/packages/@o3r/testing/package.json @@ -249,6 +249,9 @@ "typescript-eslint": "~8.19.0", "zone.js": "~0.14.2" }, + "generatorDependencies": { + "@angular-builders/jest": "~18.0.0" + }, "schematics": "./collection.json", "ng-update": { "migrations": "./migration.json" diff --git a/packages/@o3r/testing/schematics/ng-add/index.ts b/packages/@o3r/testing/schematics/ng-add/index.ts index 00c50d8939..db90efdfa1 100644 --- a/packages/@o3r/testing/schematics/ng-add/index.ts +++ b/packages/@o3r/testing/schematics/ng-add/index.ts @@ -55,6 +55,8 @@ const devDependenciesToInstall = [ 'jest-environment-jsdom', 'jest-preset-angular', 'ts-jest', + '@angular-builders/jest', + '@angular-devkit/build-angular', '@types/jest' ]; @@ -162,7 +164,8 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule { const jestConfigFilesForWorkspace = () => mergeWith(apply(url('./templates/workspace'), [ template({ - ...options + ...options, + tsconfigPath: `./${['tsconfig.base.json', 'tsconfig.json'].find((tsconfigBase) => tree.exists(`./${tsconfigBase}`))}` }), move(tree.root.path), renameTemplateFiles() diff --git a/packages/@o3r/testing/schematics/ng-add/templates/workspace/tsconfig.jest.json.template b/packages/@o3r/testing/schematics/ng-add/templates/workspace/tsconfig.jest.json.template new file mode 100644 index 0000000000..8e03a0d772 --- /dev/null +++ b/packages/@o3r/testing/schematics/ng-add/templates/workspace/tsconfig.jest.json.template @@ -0,0 +1,11 @@ +{ + "extends": "<%= tsconfigPath %>", + "compilerOptions": { + "strictPropertyInitialization": false, + "strictNullChecks": false, + "types": ["node", "jest"], + "composite": true, + "esModuleInterop": true + }, + "include": [] +} diff --git a/packages/@o3r/workspace/package.json b/packages/@o3r/workspace/package.json index ba0cd9de4e..0b0d26cc37 100644 --- a/packages/@o3r/workspace/package.json +++ b/packages/@o3r/workspace/package.json @@ -124,6 +124,7 @@ }, "generatorDependencies": { "@angular/material": "~18.2.0", + "@angular-builders/jest": "~18.0.0", "@ngrx/router-store": "~18.0.0", "@ngrx/effects": "~18.0.0", "@ngrx/store-devtools": "~18.0.0", diff --git a/packages/@o3r/workspace/schematics/index.it.spec.ts b/packages/@o3r/workspace/schematics/index.it.spec.ts index 896c6fd7c0..afdd4a0bd7 100644 --- a/packages/@o3r/workspace/schematics/index.it.spec.ts +++ b/packages/@o3r/workspace/schematics/index.it.spec.ts @@ -114,6 +114,8 @@ describe('new otter workspace', () => { expect(() => packageManagerExec({ script: 'ng', args: ['g', 'library', libName] }, execAppOptions)).not.toThrow(); expect(existsSync(path.join(workspacePath, 'project'))).toBe(false); generatedLibFiles.forEach((file) => expect(existsSync(path.join(inLibraryPath, file))).toBe(true)); + // TODO apps are generated without jest full configuration - needs to be fixed before removing this part + expect(() => packageManagerExec({ script: 'ng', args: ['test', libName] }, execAppOptions)).not.toThrow(); expect(() => packageManagerRunOnProject(libName, true, { script: 'build' }, execAppOptions)).not.toThrow(); }); diff --git a/packages/@o3r/workspace/schematics/library/index.spec.ts b/packages/@o3r/workspace/schematics/library/index.spec.ts index e8ea8ad7e0..943c475809 100644 --- a/packages/@o3r/workspace/schematics/library/index.spec.ts +++ b/packages/@o3r/workspace/schematics/library/index.spec.ts @@ -17,6 +17,15 @@ jest.mock('@angular-devkit/schematics', () => { }); const collectionPath = path.join(__dirname, '..', '..', 'collection.json'); +const angularJsonFile = `{ + "version": 1, + "projects": { + "my-new-module": { + "projectType": "library", + "root": "packages-test/my-new-module" + } + } +}`; describe('New module generator', () => { let initialTree: Tree; @@ -36,7 +45,7 @@ describe('New module generator', () => { }); it('should generate the minimum mandatory files', async () => { - initialTree.create('angular.json', '{"version": 1, "projects": {} }'); + initialTree.create('angular.json', angularJsonFile); initialTree.create('package.json', '{ "version": "0.0.0-test" }'); initialTree.create('/packages-test/my-new-module/package.json', '{ "version": "0.0.0-test" }'); initialTree.create('/packages-test/my-new-module/ng-package.json', '{ }'); @@ -58,11 +67,43 @@ describe('New module generator', () => { expect(tree.exists('/packages-test/my-new-module/project.json')).toBe(false); expect(JSON.parse(tree.readContent('/tsconfig.base.json')).compilerOptions.paths['@my/new-module']).toContain('packages-test/my-new-module/src/public-api'); expect(JSON.parse(tree.readContent('/tsconfig.build.json')).compilerOptions.paths['@my/new-module'][0]).toBe('packages-test/my-new-module/dist'); + expect(tree.exists('/packages-test/my-new-module/testing/setup-jest.ts')).toBe(false); + expect(JSON.parse(tree.readContent('/packages-test/my-new-module/package.json')).scripts.test).toContain('ng test my-new-module'); + expect(tree.exists('/packages-test/my-new-module/jest.config.js')).toBe(false); expect(tree.files.length).toBeGreaterThanOrEqual(9); }); - // eslint-disable-next-line jest/no-disabled-tests -- TODO: Should be re-enable when the following issue #2066 is fixed - describe.skip('in NX monorepo', () => { + it('should generate an project with jest files', async () => { + initialTree.create('angular.json', angularJsonFile); + initialTree.create('package.json', '{ "version": "0.0.0-test" }'); + initialTree.create('/packages-test/my-new-module/package.json', '{ "version": "0.0.0-test" }'); + initialTree.create('/packages-test/my-new-module/ng-package.json', '{ }'); + const runner = new SchematicTestRunner('schematics', collectionPath); + const angularPackageJson = require.resolve('@schematics/angular/package.json'); + const o3rCorePackageJson = require.resolve('@o3r/core/package.json'); + runner.registerCollection('@o3r/core', path.resolve(path.dirname(o3rCorePackageJson), require(o3rCorePackageJson).schematics)); + runner.registerCollection('@schematics/angular', path.resolve(path.dirname(angularPackageJson), require(angularPackageJson).schematics)); + jest.spyOn(require('@angular-devkit/schematics'), 'externalSchematic'); + const tree = await runner.runSchematic('library', { + path: 'packages-test', + name: '@my/new-module', + skipLinter: true, + skipInstall: true, + testingFramework: 'jest' + }, initialTree); + expect(tree.exists('/packages-test/my-new-module/testing/setup-jest.ts')).toBe(true); + expect(tree.exists('/packages-test/my-new-module/jest.config.js')).toBe(true); + const packageJsonContent = tree.readJson('/packages-test/my-new-module/package.json') as any; + expect(packageJsonContent.scripts.test).toBe('ng test my-new-module'); + expect(packageJsonContent.devDependencies['@angular-builders/jest']).toBeDefined(); + (tree.readJson('/packages-test/my-new-module/tsconfig.spec.json') as { references: { path: string }[] }) + .references + .forEach((ref) => { + expect(tree.exists(path.join('/packages-test/my-new-module', ref.path))).toBe(true); + }); + }); + + describe('in NX monorepo', () => { it('should generate Nx project.json with given name', async () => { initialTree.create('nx.json', '{"workspaceLayout": { "libsDir": "packages-test" } }'); initialTree.create('angular.json', '{"version": 1, "projects": {} }'); @@ -77,14 +118,16 @@ describe('New module generator', () => { runner.registerCollection('@schematics/angular', path.resolve(path.dirname(angularPackageJson), require(angularPackageJson).schematics)); runner.registerCollection('@nx/workspace', path.resolve(path.dirname(nxWorkspacePackageJson), require(nxWorkspacePackageJson).generators)); const tree = await runner.runExternalSchematic('schematics', 'library', { - path: 'packages-test', name: '@my/new-module', projectName: 'test-module-name', skipLinter: true }, initialTree); expect(tree.exists('/packages-test/my-new-module/project.json')).toBe(true); - expect(tree.readContent('/packages-test/my-new-module/project.json')).toContain('"name": "test-module-name"'); + const projectJson: any = tree.readJson('/packages-test/my-new-module/project.json'); + expect(projectJson.name).toBe('test-module-name'); + expect(tree.exists(projectJson.targets.test.options.jestConfig)).toBe(true); + expect(projectJson.targets.test.executor).toBe('@nrwl/jest:jest'); }); }); }); diff --git a/packages/@o3r/workspace/schematics/library/rules/rules.ng.ts b/packages/@o3r/workspace/schematics/library/rules/rules.ng.ts index 6384dd4447..1294b68eab 100644 --- a/packages/@o3r/workspace/schematics/library/rules/rules.ng.ts +++ b/packages/@o3r/workspace/schematics/library/rules/rules.ng.ts @@ -14,6 +14,9 @@ import { template, url, } from '@angular-devkit/schematics'; +import type { + Tree, +} from '@angular-devkit/schematics'; import { findConfigFileRelativePath, } from '@o3r/schematics'; @@ -32,6 +35,44 @@ import { updatePackageDependenciesFactory, } from './shared'; +/** + * Set jest files and script in the generated library. + * @param options + */ +function setUpAngularTestPackageJson(options: NgGenerateModuleSchema & { targetPath: string }): Rule { + return (tree: Tree) => { + const packageJsonPath = path.join(options.targetPath, 'package.json'); + const packageJsonContent = tree.readJson(packageJsonPath) as PackageJson; + packageJsonContent.scripts ||= {}; + packageJsonContent.scripts.test ||= `ng test ${options.name}`; + tree.overwrite(packageJsonPath, JSON.stringify(packageJsonContent, null, 2)); + return tree; + }; +} + +/** + * Set jest files and script in the generated library. + * @param options + */ +function setUpJestForAngularJson(options: NgGenerateModuleSchema & { targetPath: string }) { + return (tree: Tree) => { + const angularFile = tree.readJson('/angular.json') as { projects: any }; + const project: any = angularFile.projects[options.name]; + project.architect ||= {}; + project.architect.test = { + builder: '@angular-builders/jest:run', + options: { + tsConfig: `tsconfig.spec.json`, + configPath: `jest.config.js`, + setupFilesAfterEnv: './testing/setup-jest.ts' + } + }; + + tree.overwrite('/angular.json', JSON.stringify(angularFile, null, 2)); + return tree; + }; +} + /** * generate the rules to adapt the library generated by ng cli * @param options Schematic options @@ -50,21 +91,48 @@ export function ngGenerateModule(options: NgGenerateModuleSchema & { targetPath: const o3rCorePackageJson: PackageJson & { generatorDependencies?: Record } = JSON.parse(readFileSync(o3rCorePackageJsonPath).toString()); const otterVersion = o3rCorePackageJson.dependencies!['@o3r/schematics']; - const templateNg = apply(url('./templates/ng'), [ + const templateNg = apply(url('./templates/ng/common'), [ template({ ...options, - tsconfigSpecPath: findConfigFileRelativePath(tree, - ['tsconfig.test.json', 'tsconfig.spec.json', 'tsconfig.jest.json', 'tsconfig.jasmine.json', 'tsconfig.base.json', 'tsconfig.json'], options.targetPath), tsconfigBasePath: findConfigFileRelativePath(tree, ['tsconfig.base.json', 'tsconfig.json'], options.targetPath), tsconfigBuildPath: findConfigFileRelativePath(tree, ['tsconfig.build.json', 'tsconfig.base.json', 'tsconfig.json'], options.targetPath) }), renameTemplateFiles(), move(options.targetPath) ]); - + const templateCommonJest = apply(url('./templates/jest'), [ + template({ + ...options, + tsconfigBasePath: findConfigFileRelativePath(tree, ['tsconfig.base.json', 'tsconfig.json'], options.targetPath) + }), + renameTemplateFiles(), + move(options.targetPath) + ]); + const templateNgJest = apply(url('./templates/ng/jest'), [ + template({ + ...options, + tsconfigSpecPath: findConfigFileRelativePath(tree, + ['tsconfig.test.json', 'tsconfig.spec.json', 'tsconfig.jest.json', 'tsconfig.jasmine.json', 'tsconfig.base.json', 'tsconfig.json'], options.targetPath), + tsconfigBasePath: findConfigFileRelativePath(tree, ['tsconfig.base.json', 'tsconfig.json'], options.targetPath) + }), + renameTemplateFiles(), + move(options.targetPath) + ]); + const packageJsonContent = tree.readText('/package.json'); + const hasJestInstalled = options.testingFramework === 'jest' || packageJsonContent.match('"jest"'); return chain([ mergeWith(templateNg, MergeStrategy.Overwrite), - updatePackageDependenciesFactory(options.targetPath, otterVersion!, o3rCorePackageJson, options), + ...hasJestInstalled + ? [ + mergeWith(templateCommonJest, MergeStrategy.Overwrite), + mergeWith(templateNgJest, MergeStrategy.Overwrite), + setUpJestForAngularJson(options), + setUpAngularTestPackageJson({ ...options }) + ] + : [ + setUpAngularTestPackageJson(options) + ], + updatePackageDependenciesFactory(options.targetPath, otterVersion!, o3rCorePackageJson, { ...options, useJest: !!hasJestInstalled }), updateNgPackagrFactory(options.targetPath), (t) => { const genPackageJsonPath = path.posix.join(options.targetPath, 'package.json'); diff --git a/packages/@o3r/workspace/schematics/library/rules/rules.nx.ts b/packages/@o3r/workspace/schematics/library/rules/rules.nx.ts index 7675ea7368..d91150c019 100644 --- a/packages/@o3r/workspace/schematics/library/rules/rules.nx.ts +++ b/packages/@o3r/workspace/schematics/library/rules/rules.nx.ts @@ -105,12 +105,11 @@ export function nxGenerateModule(options: NgGenerateModuleSchema & { packageJson const o3rCorePackageJsonPath = path.resolve(__dirname, '..', '..', '..', 'package.json'); const o3rCorePackageJson: PackageJson & { generatorDependencies?: Record } = JSON.parse(readFileSync(o3rCorePackageJsonPath).toString()); const otterVersion = o3rCorePackageJson.dependencies!['@o3r/schematics']; - const templateNx = apply(url('./templates/nx'), [ template({ ...options, path: targetPath, - projectRoot: path.posix.resolve(targetPath, options.name), + projectRoot: targetPath, otterVersion, tsconfigBasePath: findConfigFileRelativePath(tree, ['tsconfig.base.json', 'tsconfig.json'], targetPath), runner: getPackageManagerRunner(getWorkspaceConfig(tree)) @@ -118,12 +117,21 @@ export function nxGenerateModule(options: NgGenerateModuleSchema & { packageJson renameTemplateFiles(), move(targetPath) ]); + const templateJest = apply(url('./templates/jest'), [ + template({ + ...options, + tsconfigBasePath: findConfigFileRelativePath(tree, ['tsconfig.base.json', 'tsconfig.json'], targetPath) + }), + renameTemplateFiles(), + move(targetPath) + ]); rules.push(mergeWith(templateNx, MergeStrategy.Overwrite)); return chain([ ...rules, - updatePackageDependenciesFactory(targetPath, otterVersion!, o3rCorePackageJson, options), + updatePackageDependenciesFactory(targetPath, otterVersion!, o3rCorePackageJson, { ...options, useJest: true }), updateNgPackagrFactory(targetPath), + mergeWith(templateJest, MergeStrategy.Overwrite), (t) => { const packageJson = t.readJson(path.posix.join(targetPath, 'package.json')) as PackageJson; packageJson.name = options.packageJsonName; diff --git a/packages/@o3r/workspace/schematics/library/rules/shared.ts b/packages/@o3r/workspace/schematics/library/rules/shared.ts index 9a985dd3de..b168ecea0f 100644 --- a/packages/@o3r/workspace/schematics/library/rules/shared.ts +++ b/packages/@o3r/workspace/schematics/library/rules/shared.ts @@ -25,7 +25,7 @@ export function updatePackageDependenciesFactory( targetPath: string, otterVersion: string, o3rCorePackageJson: PackageJson & { generatorDependencies?: Record }, - options: NgGenerateModuleSchema): Rule { + options: NgGenerateModuleSchema & { useJest?: boolean }): Rule { return (tree) => { const packageJson = tree.readJson(path.posix.join(targetPath, 'package.json')) as PackageJson; const runner = getPackageManagerRunner(getWorkspaceConfig(tree)); @@ -56,20 +56,27 @@ export function updatePackageDependenciesFactory( '@angular/platform-browser': packageJson.peerDependencies['@angular/common'], '@angular/platform-browser-dynamic': packageJson.peerDependencies['@angular/common'], '@schematics/angular': o3rCorePackageJson.peerDependencies!['@schematics/angular'], - '@types/jest': o3rCorePackageJson.generatorDependencies!['@types/jest'], '@typescript-eslint/eslint-plugin': o3rCorePackageJson.generatorDependencies!['@typescript-eslint/parser'], '@typescript-eslint/parser': o3rCorePackageJson.generatorDependencies!['@typescript-eslint/parser'], 'cpy-cli': o3rCorePackageJson.generatorDependencies!['cpy-cli'], eslint: o3rCorePackageJson.generatorDependencies!.eslint, 'eslint-import-resolver-node': o3rCorePackageJson.generatorDependencies!['eslint-import-resolver-node'], - 'eslint-plugin-jest': o3rCorePackageJson.generatorDependencies!['eslint-plugin-jest'], + ...options.useJest + ? { + '@angular-builders/jest': o3rCorePackageJson.generatorDependencies!['@angular-builders/jest'], + '@angular-devkit/build-angular': o3rCorePackageJson.devDependencies!['@angular-devkit/build-angular'], + '@types/jest': o3rCorePackageJson.devDependencies!['@types/jest'], + 'eslint-plugin-jest': o3rCorePackageJson.devDependencies!['eslint-plugin-jest'], + jest: o3rCorePackageJson.devDependencies!.jest, + 'jest-environment-jsdom': o3rCorePackageJson.devDependencies!.jest, + 'jest-junit': o3rCorePackageJson.devDependencies!['jest-junit'], + 'jest-preset-angular': o3rCorePackageJson.devDependencies!['jest-preset-angular'], + 'ts-jest': o3rCorePackageJson.devDependencies!['ts-jest'] + } + : {}, 'eslint-plugin-jsdoc': o3rCorePackageJson.generatorDependencies!['eslint-plugin-jsdoc'], 'eslint-plugin-prefer-arrow': o3rCorePackageJson.generatorDependencies!['eslint-plugin-prefer-arrow'], 'eslint-plugin-unicorn': o3rCorePackageJson.generatorDependencies!['eslint-plugin-unicorn'], - jest: o3rCorePackageJson.generatorDependencies!.jest, - 'jest-environment-jsdom': o3rCorePackageJson.generatorDependencies!.jest, - 'jest-junit': o3rCorePackageJson.generatorDependencies!['jest-junit'], - 'jest-preset-angular': o3rCorePackageJson.generatorDependencies!['jest-preset-angular'], rxjs: o3rCorePackageJson.peerDependencies!.rxjs, typescript: o3rCorePackageJson.peerDependencies!.typescript, 'zone.js': o3rCorePackageJson.generatorDependencies!['zone.js'] diff --git a/packages/@o3r/workspace/schematics/library/templates/ng/jest.config.js.template b/packages/@o3r/workspace/schematics/library/templates/jest/jest.config.js.template similarity index 98% rename from packages/@o3r/workspace/schematics/library/templates/ng/jest.config.js.template rename to packages/@o3r/workspace/schematics/library/templates/jest/jest.config.js.template index 2f5aa0133c..fdf141006d 100644 --- a/packages/@o3r/workspace/schematics/library/templates/ng/jest.config.js.template +++ b/packages/@o3r/workspace/schematics/library/templates/jest/jest.config.js.template @@ -10,7 +10,7 @@ module.exports = { displayName: require('./package.json').name, preset: 'jest-preset-angular', setupFilesAfterEnv: ['/testing/setup-jest.ts'], - rootDir: '.', + rootDir: __dirname, moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths), testPathIgnorePatterns: [ '/dist', diff --git a/packages/@o3r/workspace/schematics/library/templates/ng/testing/setup-jest.ts.template b/packages/@o3r/workspace/schematics/library/templates/jest/testing/setup-jest.ts.template similarity index 100% rename from packages/@o3r/workspace/schematics/library/templates/ng/testing/setup-jest.ts.template rename to packages/@o3r/workspace/schematics/library/templates/jest/testing/setup-jest.ts.template diff --git a/packages/@o3r/workspace/schematics/library/templates/ng/.gitignore.template b/packages/@o3r/workspace/schematics/library/templates/ng/common/.gitignore.template similarity index 100% rename from packages/@o3r/workspace/schematics/library/templates/ng/.gitignore.template rename to packages/@o3r/workspace/schematics/library/templates/ng/common/.gitignore.template diff --git a/packages/@o3r/workspace/schematics/library/templates/ng/.npmignore.template b/packages/@o3r/workspace/schematics/library/templates/ng/common/.npmignore.template similarity index 100% rename from packages/@o3r/workspace/schematics/library/templates/ng/.npmignore.template rename to packages/@o3r/workspace/schematics/library/templates/ng/common/.npmignore.template diff --git a/packages/@o3r/workspace/schematics/library/templates/ng/tsconfig.builders.json.template b/packages/@o3r/workspace/schematics/library/templates/ng/common/tsconfig.builders.json.template similarity index 100% rename from packages/@o3r/workspace/schematics/library/templates/ng/tsconfig.builders.json.template rename to packages/@o3r/workspace/schematics/library/templates/ng/common/tsconfig.builders.json.template diff --git a/packages/@o3r/workspace/schematics/library/templates/ng/tsconfig.json.template b/packages/@o3r/workspace/schematics/library/templates/ng/common/tsconfig.json.template similarity index 100% rename from packages/@o3r/workspace/schematics/library/templates/ng/tsconfig.json.template rename to packages/@o3r/workspace/schematics/library/templates/ng/common/tsconfig.json.template diff --git a/packages/@o3r/workspace/schematics/library/templates/ng/jest/tsconfig.build.composite.json.template b/packages/@o3r/workspace/schematics/library/templates/ng/jest/tsconfig.build.composite.json.template new file mode 100644 index 0000000000..75989c2388 --- /dev/null +++ b/packages/@o3r/workspace/schematics/library/templates/ng/jest/tsconfig.build.composite.json.template @@ -0,0 +1,6 @@ +{ + "extends": "./tsconfig.lib.json", + "compilerOptions": { + "composite": true + } +} diff --git a/packages/@o3r/workspace/schematics/library/templates/ng/tsconfig.spec.json.template b/packages/@o3r/workspace/schematics/library/templates/ng/jest/tsconfig.spec.json.template similarity index 92% rename from packages/@o3r/workspace/schematics/library/templates/ng/tsconfig.spec.json.template rename to packages/@o3r/workspace/schematics/library/templates/ng/jest/tsconfig.spec.json.template index 4dba869bbf..ab6b0e6f18 100644 --- a/packages/@o3r/workspace/schematics/library/templates/ng/tsconfig.spec.json.template +++ b/packages/@o3r/workspace/schematics/library/templates/ng/jest/tsconfig.spec.json.template @@ -3,7 +3,7 @@ "compilerOptions": { "composite": true, "outDir": "test", - "rootDir": ".", + "rootDir": "." }, "include": [ "./src/**/*.spec.ts" diff --git a/packages/@o3r/workspace/schematics/library/templates/nx/jest.config.js.template b/packages/@o3r/workspace/schematics/library/templates/nx/jest.config.js.template deleted file mode 100644 index b983a234bc..0000000000 --- a/packages/@o3r/workspace/schematics/library/templates/nx/jest.config.js.template +++ /dev/null @@ -1,38 +0,0 @@ -const { pathsToModuleNameMapper } = require('ts-jest'); -const { compilerOptions } = require('<%= tsconfigBasePath %>'); - -globalThis.ngJest = { - skipNgcc: true -}; - -/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ -module.exports = { - displayName: require('./package.json').name, - preset: 'jest-preset-angular', - setupFilesAfterEnv: ['/testing/setup-jest.ts'], - rootDir: '.', - moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths), - testPathIgnorePatterns: [ - '/dist', - ], - reporters: [ - 'default', - 'github-actions' - ], - globalSetup: 'jest-preset-angular/global-setup', - transform: { - // eslint-disable-next-line @typescript-eslint/naming-convention - '^.+\\.tsx?$': [ - 'jest-preset-angular', - { - tsconfig: '/tsconfig.spec.json', - stringifyContentPathRegex: '\\.html$' - } - ] - }, - snapshotSerializers: [ - 'jest-preset-angular/build/serializers/no-ng-attributes', - 'jest-preset-angular/build/serializers/ng-snapshot', - 'jest-preset-angular/build/serializers/html-comment', - ] -}; diff --git a/packages/@o3r/workspace/schematics/library/templates/nx/testing/setup-jest.ts.template b/packages/@o3r/workspace/schematics/library/templates/nx/testing/setup-jest.ts.template deleted file mode 100644 index ab68e1eb87..0000000000 --- a/packages/@o3r/workspace/schematics/library/templates/nx/testing/setup-jest.ts.template +++ /dev/null @@ -1,2 +0,0 @@ -import 'jest-preset-angular/setup-jest'; -