Skip to content

Commit

Permalink
fix: the tsconfig spec generated for library contains several issues
Browse files Browse the repository at this point in the history
  • Loading branch information
cpaulve-1A committed Jan 6, 2025
1 parent 5cb8aeb commit 90d7ac8
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 61 deletions.
6 changes: 6 additions & 0 deletions packages/@o3r/workspace/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as path from 'node:path';
import {
getDefaultExecSyncOptions,
getGitDiff,
getPackageManager,
packageManagerExec,
packageManagerInstall,
packageManagerRun,
Expand Down Expand Up @@ -95,6 +96,10 @@ describe('new otter workspace', () => {
const execAppOptions = { ...getDefaultExecSyncOptions(), cwd: workspacePath };
const libName = 'test-library';
const inLibraryPath = path.resolve(workspacePath, 'libs', libName);
// TODO Remove node-linker once https://github.com/AmadeusITGroup/otter/issues/2620 is completed
if (getPackageManager() === 'yarn') {
packageManagerExec({ script: 'config', args: ['set', 'nodeLinker', 'node-modules'] }, execAppOptions);
}
expect(() => packageManagerInstall(execAppOptions)).not.toThrow();

const generatedLibFiles = [
Expand All @@ -115,6 +120,7 @@ describe('new otter workspace', () => {
expect(existsSync(path.join(workspacePath, 'project'))).toBe(false);
generatedLibFiles.forEach((file) => expect(existsSync(path.join(inLibraryPath, file))).toBe(true));
expect(() => packageManagerRunOnProject(libName, true, { script: 'build' }, execAppOptions)).not.toThrow();
expect(() => packageManagerExec({ script: 'ng', args: ['test', '--watch=false', '--browsers=ChromeHeadless'] }, execAppOptions)).not.toThrow();
});

test('should generate a monorepo setup', async () => {
Expand Down
41 changes: 37 additions & 4 deletions packages/@o3r/workspace/schematics/library/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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', '{ }');
Expand All @@ -58,11 +67,36 @@ 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(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);
expect(JSON.parse(tree.readContent('/packages-test/my-new-module/package.json')).scripts.test).toContain('jest');
});

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": {} }');
Expand All @@ -77,7 +111,6 @@ 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
Expand Down
17 changes: 17 additions & 0 deletions packages/@o3r/workspace/schematics/library/rules/rules.ng.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import type {
NgGenerateModuleSchema,
} from '../schema';
import {
setUpJest,
updateNgPackagrFactory,
updatePackageDependenciesFactory,
} from './shared';
Expand Down Expand Up @@ -61,9 +62,25 @@ export function ngGenerateModule(options: NgGenerateModuleSchema & { targetPath:
renameTemplateFiles(),
move(options.targetPath)
]);
const templateJest = apply(url('./templates/jest'), [
template({
...options,
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),
...hasJestInstalled
? [
mergeWith(templateJest, MergeStrategy.Overwrite),
setUpJest(options)
]
: [],
updatePackageDependenciesFactory(options.targetPath, otterVersion!, o3rCorePackageJson, options),
updateNgPackagrFactory(options.targetPath),
(t) => {
Expand Down
17 changes: 17 additions & 0 deletions packages/@o3r/workspace/schematics/library/rules/rules.nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import type {
NgGenerateModuleSchema,
} from '../schema';
import {
setUpJest,
updateNgPackagrFactory,
updatePackageDependenciesFactory,
} from './shared';
Expand Down Expand Up @@ -118,12 +119,28 @@ 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));
const packageJsonContent = tree.readText('/package.json');
const hasJestInstalled = options.testingFramework === 'jest' || packageJsonContent.match('jest');

return chain([
...rules,
updatePackageDependenciesFactory(targetPath, otterVersion!, o3rCorePackageJson, options),
updateNgPackagrFactory(targetPath),
...hasJestInstalled
? [
mergeWith(templateJest, MergeStrategy.Overwrite),
setUpJest(options)
]
: [],
(t) => {
const packageJson = t.readJson(path.posix.join(targetPath, 'package.json')) as PackageJson;
packageJson.name = options.packageJsonName;
Expand Down
23 changes: 23 additions & 0 deletions packages/@o3r/workspace/schematics/library/rules/shared.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as path from 'node:path';
import type {
Rule,
SchematicContext,
Tree,
} from '@angular-devkit/schematics';
import {
getPackageManagerRunner,
Expand All @@ -13,6 +15,27 @@ import {
NgGenerateModuleSchema,
} from '../schema';

/**
* Set jest files and script in the generated library.
* @param options
*/
export function setUpJest(options: NgGenerateModuleSchema) {
return (tree: Tree, context: SchematicContext) => {
const workspaceConfig = getWorkspaceConfig(tree);
const workspaceProject = (options.name && workspaceConfig?.projects?.[options.name]) || undefined;
if (!workspaceProject?.root) {
context.logger.error(`Failed to find a package json for ${options.name}`);
return;
}

Check warning on line 29 in packages/@o3r/workspace/schematics/library/rules/shared.ts

View check run for this annotation

Codecov / codecov/patch

packages/@o3r/workspace/schematics/library/rules/shared.ts#L28-L29

Added lines #L28 - L29 were not covered by tests
const packageJsonPath = path.join(workspaceProject.root, 'package.json');
const packageJsonContent = tree.readJson(packageJsonPath) as PackageJson;
packageJsonContent.scripts ||= {};
packageJsonContent.scripts.test ||= 'jest';
tree.overwrite(packageJsonPath, JSON.stringify(packageJsonContent, null, 2));
return tree;
};
}

/**
* Generate rule to update generated package.json file
* @param targetPath Path of the generated files
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

27 changes: 27 additions & 0 deletions tools/github-actions/new-version/packaged-action/LICENSE.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 90d7ac8

Please sign in to comment.