Skip to content

Commit

Permalink
test: library integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vscaiceanu-1a committed Jun 5, 2024
1 parent d894970 commit b75364f
Show file tree
Hide file tree
Showing 34 changed files with 830 additions and 280 deletions.
2 changes: 1 addition & 1 deletion packages/@ama-sdk/create/src/index.it.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('Create new sdk command', () => {
expect(() =>
packageManagerExec({
script: 'schematics',
args: ['@ama-sdk/schematics:typescript-core', '--spec-path', path.join(path.relative(sdkPackagePath, sdkFolderPath), 'swagger-spec.yml')]
args: ['@ama-sdk/schematics:typescript-core', '--spec-path', path.join(path.relative(sdkPackagePath, sdkFolderPath).split(path.sep).join(path.posix.sep), 'swagger-spec.yml')]
}, { ...execAppOptions, cwd: sdkPackagePath })
).not.toThrow();
expect(() => packageManagerRun({script: 'build'}, { ...execAppOptions, cwd: sdkPackagePath })).not.toThrow();
Expand Down
76 changes: 60 additions & 16 deletions packages/@o3r/analytics/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,79 @@ import {
addImportToAppModule,
getDefaultExecSyncOptions,
getGitDiff,
getPackageManager,
packageManagerExec,
packageManagerInstall,
packageManagerRunOnProject
} from '@o3r/test-helpers';
import * as path from 'node:path';

describe('new otter application with analytics', () => {
test('should add analytics to existing application', async () => {
const { projectPath, workspacePath, projectName, isInWorkspace, untouchedProjectPath, o3rVersion } = o3rEnvironment.testEnvironment;
let isYarnTest: boolean;

describe('ng add analytics', () => {

beforeEach(() => {
const packageManager = getPackageManager();
isYarnTest = packageManager.startsWith('yarn');
});

test('should add analytics to an application', async () => {
const { applicationPath, workspacePath, appName, isInWorkspace, untouchedAppPath, o3rVersion, libraryPath, untouchedLibPath } = o3rEnvironment.testEnvironment;
const execAppOptions = {...getDefaultExecSyncOptions(), cwd: workspacePath};
const relativeApplicationPath = path.relative(workspacePath, applicationPath);
expect(() => packageManagerExec({script: 'ng', args: ['add', `@o3r/analytics@${o3rVersion}`, '--project-name', appName, '--skip-confirmation']}, execAppOptions)).not.toThrow();

packageManagerExec({script: 'ng', args: ['g', '@o3r/core:component', 'test-component', '--use-otter-analytics', 'false', '--project-name', appName]}, execAppOptions);
const componentPath = path.normalize(path.posix.join(relativeApplicationPath, 'src/components/test-component/test-component.component.ts'));
packageManagerExec({script: 'ng', args: ['g', '@o3r/analytics:add-analytics', '--path', componentPath]}, execAppOptions);
await addImportToAppModule(applicationPath, 'TestComponentModule', 'src/components/test-component');

const diff = getGitDiff(workspacePath);

expect(diff.added).toContain(path.join(relativeApplicationPath, 'src/components/test-component/test-component.analytics.ts').replace(/[\\/]+/g, '/'));
expect(diff.added.length).toBe(10);
expect(diff.modified.sort()).toEqual([
'angular.json',
'apps/test-app/package.json',
'apps/test-app/src/app/app.component.ts',
'package.json',
isYarnTest ? 'yarn.lock' : 'package-lock.json'
].sort());
expect(diff.all.some(file => file.startsWith(path.posix.relative(workspacePath, libraryPath)))).toBe(false);

[untouchedAppPath, untouchedLibPath].forEach(untouchedProject => {
expect(diff.all.some(file => file.startsWith(path.posix.relative(workspacePath, untouchedProject)))).toBe(false);
});
expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRunOnProject(appName, isInWorkspace, {script: 'build'}, execAppOptions)).not.toThrow();
});

test('should add analytics to a library', () => {
const { applicationPath, workspacePath, libName, isInWorkspace, untouchedAppPath, o3rVersion, libraryPath, untouchedLibPath } = o3rEnvironment.testEnvironment;
const execAppOptions = {...getDefaultExecSyncOptions(), cwd: workspacePath};
const relativeProjectPath = path.relative(workspacePath, projectPath);
packageManagerExec({script: 'ng', args: ['add', `@o3r/analytics@${o3rVersion}`, '--project-name', projectName, '--skip-confirmation']}, execAppOptions);
const relativeLibraryPath = path.relative(workspacePath, libraryPath);
expect(() => packageManagerExec({script: 'ng', args: ['add', `@o3r/analytics@${o3rVersion}`, '--project-name', libName, '--skip-confirmation']}, execAppOptions)).not.toThrow();

packageManagerExec({script: 'ng', args: ['g', '@o3r/core:component', 'test-component', '--use-otter-analytics', 'false', '--project-name', projectName]}, execAppOptions);
const componentPath = path.normalize(path.join(relativeProjectPath, 'src/components/test-component/test-component.component.ts'));
packageManagerExec({script: 'ng', args: ['g', '@o3r/core:component', 'test-component', '--use-otter-analytics', 'false', '--project-name', libName]}, execAppOptions);
const componentPath = path.normalize(path.posix.join(relativeLibraryPath, 'src/components/test-component/test-component.component.ts'));
packageManagerExec({script: 'ng', args: ['g', '@o3r/analytics:add-analytics', '--path', componentPath]}, execAppOptions);
await addImportToAppModule(projectPath, 'TestComponentModule', 'src/components/test-component');

const diff = getGitDiff(workspacePath);
expect(diff.all.some((file) => /projects[\\/]dont-modify-me/.test(file))).toBe(false);
expect(diff.modified).toContain(path.join(relativeProjectPath, 'package.json').replace(/[\\/]+/g, '/'));
expect(diff.added).toContain(path.join(relativeProjectPath, 'src/components/test-component/test-component.analytics.ts').replace(/[\\/]+/g, '/'));

if (untouchedProjectPath) {
const relativeUntouchedProjectPath = path.relative(workspacePath, untouchedProjectPath);
expect(diff.all.filter((file) => new RegExp(relativeUntouchedProjectPath.replace(/[\\/]+/g, '[\\\\/]')).test(file)).length).toBe(0);
}
expect(diff.added).toContain(path.join(relativeLibraryPath, 'src/components/test-component/test-component.analytics.ts').replace(/[\\/]+/g, '/'));
expect(diff.added.length).toBe(10);
expect(diff.modified.sort()).toEqual([
'angular.json',
'libs/test-lib/package.json',
'package.json',
isYarnTest ? 'yarn.lock' : 'package-lock.json'
].sort());
expect(diff.all.some(file => file.startsWith(path.posix.relative(workspacePath, applicationPath)))).toBe(false);

[untouchedAppPath, untouchedLibPath].forEach(untouchedProject => {
expect(diff.all.some(file => file.startsWith(path.posix.relative(workspacePath, untouchedProject)))).toBe(false);
});
expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRunOnProject(projectName, isInWorkspace, {script: 'build'}, execAppOptions)).not.toThrow();
expect(() => packageManagerRunOnProject(libName, isInWorkspace, {script: 'build'}, execAppOptions)).not.toThrow();
});
});
26 changes: 18 additions & 8 deletions packages/@o3r/apis-manager/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const o3rEnvironment = globalThis.o3rEnvironment;
import {
getDefaultExecSyncOptions,
getGitDiff,
getPackageManager,
packageManagerExec,
packageManagerInstall,
packageManagerRunOnProject
Expand All @@ -16,18 +17,27 @@ import * as path from 'node:path';

describe('new otter application with apis-manager', () => {
test('should add apis-manager to existing application', () => {
const { workspacePath, projectName, isInWorkspace, untouchedProjectPath, o3rVersion } = o3rEnvironment.testEnvironment;
const { workspacePath, appName, isInWorkspace, untouchedAppPath, libraryPath, untouchedLibPath, o3rVersion } = o3rEnvironment.testEnvironment;
const execAppOptions = {...getDefaultExecSyncOptions(), cwd: workspacePath};
packageManagerExec({script: 'ng', args: ['add', `@o3r/apis-manager@${o3rVersion}`, '--skip-confirmation', '--project-name', projectName]}, execAppOptions);
const packageManager = getPackageManager();
const isYarnTest = packageManager.startsWith('yarn');
packageManagerExec({script: 'ng', args: ['add', `@o3r/apis-manager@${o3rVersion}`, '--skip-confirmation', '--project-name', appName]}, execAppOptions);

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRunOnProject(projectName, isInWorkspace, {script: 'build'}, execAppOptions)).not.toThrow();
expect(() => packageManagerRunOnProject(appName, isInWorkspace, {script: 'build'}, execAppOptions)).not.toThrow();

const diff = getGitDiff(workspacePath);
expect(diff.modified).toContain('package.json');
if (untouchedProjectPath) {
const relativeUntouchedProjectPath = path.relative(workspacePath, untouchedProjectPath);
expect(diff.all.filter((file) => new RegExp(relativeUntouchedProjectPath.replace(/[\\/]+/g, '[\\\\/]')).test(file)).length).toBe(0);
}
expect(diff.modified.sort()).toEqual([
'apps/test-app/package.json',
'apps/test-app/src/app/app.config.ts',
'apps/test-app/tsconfig.app.json',
'package.json',
isYarnTest ? 'yarn.lock' : 'package-lock.json'
].sort());
expect(diff.all.some(file => file.startsWith(path.posix.relative(workspacePath, libraryPath)))).toBe(false);

[untouchedAppPath, untouchedLibPath].forEach(untouchedProject => {
expect(diff.all.some(file => file.startsWith(path.posix.relative(workspacePath, untouchedProject)))).toBe(false);
});
});
});
1 change: 1 addition & 0 deletions packages/@o3r/application/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"@o3r/logger": "workspace:^",
"@o3r/routing": "workspace:^",
"@o3r/schematics": "workspace:^",
"@o3r/test-helpers": "workspace:^",
"@o3r/testing": "workspace:^",
"@schematics/angular": "~17.3.0",
"@stylistic/eslint-plugin-ts": "^1.5.4",
Expand Down
6 changes: 6 additions & 0 deletions packages/@o3r/application/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
"tsConfig": "packages/@o3r/application/tsconfig.build.json"
}
},
"test-int": {
"executor": "@nx/jest:jest",
"options": {
"jestConfig": "packages/@o3r/application/testing/jest.config.it.js"
}
},
"lint": {
"options": {
"eslintConfig": "packages/@o3r/application/.eslintrc.js",
Expand Down
24 changes: 24 additions & 0 deletions packages/@o3r/application/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Test environment exported by O3rEnvironment, must be first line of the file
* @jest-environment @o3r/test-helpers/jest-environment
* @jest-environment-o3r-app-folder test-app-application
*/
const o3rEnvironment = globalThis.o3rEnvironment;

import {
getDefaultExecSyncOptions,
packageManagerExec,
packageManagerInstall,
packageManagerRunOnProject
} from '@o3r/test-helpers';

describe('new Angular application', () => {
test('should add Otter Application to existing Angular app', () => {
const { workspacePath, isInWorkspace, appName, o3rVersion } = o3rEnvironment.testEnvironment;
const execAppOptions = {...getDefaultExecSyncOptions(), cwd: workspacePath};
packageManagerExec({script: 'ng', args: ['add', `@o3r/application@${o3rVersion}`,'--project-name', appName, '--skip-confirmation']}, execAppOptions);
expect(() => packageManagerInstall(execAppOptions)).not.toThrow();

expect(() => packageManagerRunOnProject(appName, isInWorkspace, {script: 'build'}, execAppOptions)).not.toThrow();
});
});
8 changes: 8 additions & 0 deletions packages/@o3r/application/testing/jest.config.it.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { join } = require('node:path');
const getJestConfig = require('../../../../jest.config.it').getJestConfig;

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
...getJestConfig(join(__dirname, '..')),
displayName: require('../package.json').name
};
2 changes: 1 addition & 1 deletion packages/@o3r/application/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"rootDir": ".",
},
"include": [
"./src/**/*.spec.ts"
"./**/*.spec.ts"
],
"exclude": [],
"references": [
Expand Down
53 changes: 44 additions & 9 deletions packages/@o3r/components/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,56 @@ import {
} from '@o3r/test-helpers';
import * as path from 'node:path';

describe('new otter application with components', () => {
test('should add components to existing application', () => {
const { workspacePath, projectName, isInWorkspace, untouchedProjectPath, o3rVersion } = o3rEnvironment.testEnvironment;
describe('ng add components', () => {
test('should add components to an application', () => {
const { workspacePath, appName, isInWorkspace, untouchedAppPath, o3rVersion, libraryPath, untouchedLibPath } = o3rEnvironment.testEnvironment;
const execAppOptions = {...getDefaultExecSyncOptions(), cwd: workspacePath};
packageManagerExec({script: 'ng', args: ['add', `@o3r/components@${o3rVersion}`, '--skip-confirmation', '--enable-metadata-extract', '--project-name', projectName]}, execAppOptions);
expect(() => packageManagerExec({script: 'ng',args: ['add', `@o3r/components@${o3rVersion}`,
'--skip-confirmation', '--enable-metadata-extract', '--project-name', appName]}, execAppOptions)).not.toThrow();

const diff = getGitDiff(workspacePath);
expect(diff.modified).toContain('package.json');
expect(diff.modified).toContain('angular.json');
expect(diff.modified).toContain('apps/test-app/package.json');
expect(diff.modified.length).toBe(5);
expect(diff.added).toContain('apps/test-app/cms.json');
expect(diff.added).toContain('apps/test-app/placeholders.metadata.json');
expect(diff.added).toContain('apps/test-app/tsconfig.cms.json');
expect(diff.added.length).toBe(3);

expect(diff.all.some(file => file.startsWith(path.posix.relative(workspacePath, libraryPath)))).toBe(false);

[untouchedAppPath, untouchedLibPath].forEach(untouchedProject => {
expect(diff.all.some(file => file.startsWith(path.posix.relative(workspacePath, untouchedProject)))).toBe(false);
});

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRunOnProject(projectName, isInWorkspace, {script: 'build'}, execAppOptions)).not.toThrow();
expect(() => packageManagerRunOnProject(appName, isInWorkspace, {script: 'build'}, execAppOptions)).not.toThrow();
});

test('should add components to a library', () => {
const { workspacePath, libName, isInWorkspace, untouchedAppPath, o3rVersion, applicationPath, untouchedLibPath } = o3rEnvironment.testEnvironment;
const execAppOptions = {...getDefaultExecSyncOptions(), cwd: workspacePath};
expect(() => packageManagerExec({script: 'ng',args: ['add', `@o3r/components@${o3rVersion}`,
'--skip-confirmation', '--enable-metadata-extract', '--project-name', libName]}, execAppOptions)).not.toThrow();

const diff = getGitDiff(workspacePath);
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(7);
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');
expect(diff.added.length).toBe(3);

expect(diff.all.some(file => file.startsWith(path.posix.relative(workspacePath, applicationPath)))).toBe(false);

if (untouchedProjectPath) {
const relativeUntouchedProjectPath = path.relative(workspacePath, untouchedProjectPath);
expect(diff.all.filter((file) => new RegExp(relativeUntouchedProjectPath.replace(/[\\/]+/g, '[\\\\/]')).test(file)).length).toBe(0);
}
[untouchedAppPath, untouchedLibPath].forEach(untouchedProject => {
expect(diff.all.some(file => file.startsWith(path.posix.relative(workspacePath, untouchedProject)))).toBe(false);
});

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRunOnProject(libName, isInWorkspace, {script: 'build'}, execAppOptions)).not.toThrow();
});
});
Loading

0 comments on commit b75364f

Please sign in to comment.