Skip to content

Commit

Permalink
test: use jest custom environment for it-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fpaul-1A committed Mar 26, 2024
1 parent 9daf5b8 commit b4073bd
Show file tree
Hide file tree
Showing 18 changed files with 288 additions and 260 deletions.
23 changes: 13 additions & 10 deletions packages/@ama-sdk/create/src/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
/**
* 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-create-sdk
* @jest-environment-o3r-type blank
*/
const o3rEnvironment = globalThis.o3rEnvironment;

import {
getDefaultExecSyncOptions,
getPackageManager,
getYarnVersionFromRoot,
packageManagerCreate,
packageManagerExec,
packageManagerInstall,
packageManagerRun,
prepareTestEnv,
setupLocalRegistry
packageManagerRun
} from '@o3r/test-helpers';
import * as fs from 'node:fs';
import { cpSync, mkdirSync, renameSync } from 'node:fs';
import * as path from 'node:path';

const projectName = 'test-sdk';
const sdkPackageName = '@my-test/sdk';
const o3rVersion = '999.0.0';
let sdkFolderPath: string;
let sdkPackagePath: string;
const execAppOptions = getDefaultExecSyncOptions();
const packageManager = getPackageManager();

describe('Create new sdk command', () => {
setupLocalRegistry();
beforeEach(async () => {
beforeEach(() => {
const isYarnTest = packageManager.startsWith('yarn');
const yarnVersion = isYarnTest ? getYarnVersionFromRoot(process.cwd()) || 'latest' : undefined;
sdkFolderPath = (await prepareTestEnv(projectName, {type: 'blank', yarnVersion })).workspacePath;
sdkFolderPath = o3rEnvironment.testEnvironment.workspacePath;
sdkPackagePath = path.join(sdkFolderPath, sdkPackageName.replace(/^@/, ''));
execAppOptions.cwd = sdkFolderPath;

Expand Down Expand Up @@ -98,7 +101,7 @@ describe('Create new sdk command', () => {
test('should use pinned versions when --exact-o3r-version is used', () => {
expect(() =>
packageManagerCreate({
script: `@ama-sdk@${o3rVersion}`,
script: `@ama-sdk@${o3rEnvironment.testEnvironment.o3rVersion}`,
args: ['typescript', sdkPackageName, '--exact-o3r-version', '--package-manager', packageManager, '--spec-path', path.join(sdkFolderPath, 'swagger-spec.yml')]
}, execAppOptions)
).not.toThrow();
Expand All @@ -109,7 +112,7 @@ describe('Create new sdk command', () => {
[
...Object.entries(packageJson.dependencies), ...Object.entries(packageJson.devDependencies), ...Object.entries(resolutions)
].filter(([dep]) => dep.startsWith('@o3r/') || dep.startsWith('@ama-sdk/')).forEach(([,version]) => {
expect(version).toBe(o3rVersion);
expect(version).toBe(o3rEnvironment.testEnvironment.o3rVersion);
});
});
});
34 changes: 12 additions & 22 deletions packages/@o3r/analytics/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,31 @@
/**
* 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-analytics
*/
const o3rEnvironment = globalThis.o3rEnvironment;

import {
addImportToAppModule,
getDefaultExecSyncOptions,
getGitDiff,
packageManagerExec,
packageManagerInstall,
packageManagerRunOnProject,
prepareTestEnv,
setupLocalRegistry
packageManagerRunOnProject
} from '@o3r/test-helpers';
import { rm } from 'node:fs/promises';
import * as path from 'node:path';

const appFolder = 'test-app-analytics';
const o3rVersion = '999.0.0';
const execAppOptions = getDefaultExecSyncOptions();
let projectPath: string;
let isInWorkspace: boolean;
let workspacePath: string;
let projectName: string;
let untouchedProjectPath: undefined | string;
describe('new otter application with analytics', () => {
setupLocalRegistry();
beforeAll(async () => {
({ projectPath, workspacePath, projectName, isInWorkspace, untouchedProjectPath } = await prepareTestEnv(appFolder));
execAppOptions.cwd = workspacePath;
});
afterAll(async () => {
try { await rm(workspacePath, { recursive: true }); } catch { /* ignore error */ }
});
test('should add analytics to existing application', () => {
test('should add analytics to existing application', async () => {
const { projectPath, workspacePath, projectName, isInWorkspace, untouchedProjectPath, o3rVersion } = 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);

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/analytics:add-analytics', '--path', componentPath]}, execAppOptions);
addImportToAppModule(projectPath, 'TestComponentModule', 'src/components/test-component');
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);
Expand Down
29 changes: 10 additions & 19 deletions packages/@o3r/apis-manager/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
/**
* 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-apis-manager
*/
const o3rEnvironment = globalThis.o3rEnvironment;

import {
getDefaultExecSyncOptions,
getGitDiff,
packageManagerExec,
packageManagerInstall,
packageManagerRunOnProject,
prepareTestEnv,
setupLocalRegistry
packageManagerRunOnProject
} from '@o3r/test-helpers';
import * as path from 'node:path';
import { rm } from 'node:fs/promises';

const appFolder = 'test-app-apis-manager';
const o3rVersion = '999.0.0';
const execAppOptions = getDefaultExecSyncOptions();
let workspacePath: string;
let projectName: string;
let isInWorkspace: boolean;
let untouchedProjectPath: undefined | string;
describe('new otter application with apis-manager', () => {
setupLocalRegistry();
beforeAll(async () => {
({ workspacePath, projectName, isInWorkspace, untouchedProjectPath } = await prepareTestEnv(appFolder));
execAppOptions.cwd = workspacePath;
});
afterAll(async () => {
try { await rm(workspacePath, { recursive: true }); } catch { /* ignore error */ }
});
test('should add apis-manager to existing application', () => {
const { workspacePath, projectName, isInWorkspace, untouchedProjectPath, o3rVersion } = o3rEnvironment.testEnvironment;
const execAppOptions = {...getDefaultExecSyncOptions(), cwd: workspacePath};
packageManagerExec({script: 'ng', args: ['add', `@o3r/apis-manager@${o3rVersion}`, '--skip-confirmation', '--project-name', projectName]}, execAppOptions);

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
Expand Down
29 changes: 10 additions & 19 deletions packages/@o3r/components/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
/**
* 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-components
*/
const o3rEnvironment = globalThis.o3rEnvironment;

import {
getDefaultExecSyncOptions,
getGitDiff,
packageManagerExec,
packageManagerInstall,
packageManagerRunOnProject,
prepareTestEnv,
setupLocalRegistry
packageManagerRunOnProject
} from '@o3r/test-helpers';
import * as path from 'node:path';
import { rm } from 'node:fs/promises';

const appFolder = 'test-app-components';
const o3rVersion = '999.0.0';
const execAppOptions = getDefaultExecSyncOptions();
let workspacePath: string;
let projectName: string;
let isInWorkspace: boolean;
let untouchedProjectPath: undefined | string;
describe('new otter application with components', () => {
setupLocalRegistry();
beforeAll(async () => {
({ workspacePath, projectName, isInWorkspace, untouchedProjectPath } = await prepareTestEnv(appFolder));
execAppOptions.cwd = workspacePath;
});
afterAll(async () => {
try { await rm(workspacePath, { recursive: true }); } catch { /* ignore error */ }
});
test('should add components to existing application', () => {
const { workspacePath, projectName, isInWorkspace, untouchedProjectPath, o3rVersion } = 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(() => packageManagerInstall(execAppOptions)).not.toThrow();
Expand Down
30 changes: 10 additions & 20 deletions packages/@o3r/configuration/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,34 +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-configuration
*/
const o3rEnvironment = globalThis.o3rEnvironment;

import {
addImportToAppModule,
getDefaultExecSyncOptions,
getGitDiff,
packageManagerExec,
packageManagerInstall,
packageManagerRunOnProject,
prepareTestEnv,
setupLocalRegistry
packageManagerRunOnProject
} from '@o3r/test-helpers';
import { rm } from 'node:fs/promises';
import * as path from 'node:path';

const appFolder = 'test-app-configuration';
const o3rVersion = '999.0.0';
const execAppOptions = getDefaultExecSyncOptions();
let projectPath: string;
let workspacePath: string;
let projectName: string;
let isInWorkspace: boolean;
let untouchedProjectPath: undefined | string;
describe('new otter application with configuration', () => {
setupLocalRegistry();
beforeAll(async () => {
({ projectPath, workspacePath, projectName, isInWorkspace, untouchedProjectPath } = await prepareTestEnv(appFolder));
execAppOptions.cwd = workspacePath;
});
afterAll(async () => {
try { await rm(workspacePath, { recursive: true }); } catch { /* ignore error */ }
});
test('should add configuration to existing application', async () => {
const { projectPath, workspacePath, projectName, isInWorkspace, untouchedProjectPath, o3rVersion } = o3rEnvironment.testEnvironment;
const execAppOptions = {...getDefaultExecSyncOptions(), cwd: workspacePath};
const relativeProjectPath = path.relative(workspacePath, projectPath);
packageManagerExec({script: 'ng', args: ['add', `@o3r/configuration@${o3rVersion}`, '--skip-confirmation', '--project-name', projectName]}, execAppOptions);

Expand Down
32 changes: 11 additions & 21 deletions packages/@o3r/core/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
/**
* 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-core
*/
const o3rEnvironment = globalThis.o3rEnvironment;

import {
addImportToAppModule,
getDefaultExecSyncOptions,
getGitDiff,
packageManagerExec,
packageManagerExecOnProject,
packageManagerInstall,
packageManagerRunOnProject,
prepareTestEnv,
setupLocalRegistry
packageManagerRunOnProject
} from '@o3r/test-helpers';
import * as path from 'node:path';
import { execSync, spawn } from 'node:child_process';
import getPidFromPort from 'pid-from-port';
import { rm } from 'node:fs/promises';

const devServerPort = 4200;
const appFolder = 'test-app-core';
const o3rVersion = '999.0.0';
const execAppOptions = getDefaultExecSyncOptions();
let projectPath: string;
let workspacePath: string;
let projectName: string;
let isInWorkspace: boolean;
let untouchedProjectPath: undefined | string;
describe('new otter application', () => {
setupLocalRegistry();
beforeAll(async () => {
({ projectPath, workspacePath, projectName, isInWorkspace, untouchedProjectPath } = await prepareTestEnv(appFolder));
execAppOptions.cwd = workspacePath;
});
test('should build empty app', async () => {
const { projectPath, workspacePath, projectName, isInWorkspace, untouchedProjectPath, o3rVersion } = o3rEnvironment.testEnvironment;
const execAppOptions = {...getDefaultExecSyncOptions(), cwd: workspacePath};
const relativeProjectPath = path.relative(workspacePath, projectPath);
const projectNameOptions = ['--project-name', projectName];
packageManagerExec({script: 'ng', args: ['add', `@o3r/core@${o3rVersion}`, '--preset', 'all', ...projectNameOptions, '--skip-confirmation']}, execAppOptions);
Expand Down Expand Up @@ -110,7 +103,7 @@ describe('new otter application', () => {
packageManagerExec({script: 'ng', args: ['g', '@o3r/testing:playwright-scenario', '--name', 'test-scenario', ...projectNameOptions]}, execAppOptions);
packageManagerExec({script: 'ng', args: ['g', '@o3r/testing:playwright-sanity', '--name', 'test-sanity', ...projectNameOptions]}, execAppOptions);

const diff = getGitDiff(execAppOptions.cwd as string);
const diff = getGitDiff(execAppOptions.cwd);

if (untouchedProjectPath) {
const relativeUntouchedProjectPath = path.relative(workspacePath, untouchedProjectPath);
Expand Down Expand Up @@ -149,7 +142,4 @@ describe('new otter application', () => {
// http-server already off
}
});
afterAll(async () => {
try { await rm(workspacePath, { recursive: true }); } catch { /* ignore error */ }
});
});
41 changes: 18 additions & 23 deletions packages/@o3r/create/src/index.it.spec.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
/**
* 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-create-app
* @jest-environment-o3r-type blank
*/
const o3rEnvironment = globalThis.o3rEnvironment;

import {
getDefaultExecSyncOptions,
getPackageManager,
type PackageManagerConfig,
packageManagerCreate,
packageManagerExec,
packageManagerInstall,
packageManagerRunOnProject,
prepareTestEnv,
setPackagerManagerConfig,
setupLocalRegistry
setPackagerManagerConfig
} from '@o3r/test-helpers';
import { existsSync, promises as fs } from 'node:fs';
import * as path from 'node:path';

const appFolder = 'test-create-app';
const defaultExecOptions = getDefaultExecSyncOptions();
const workspaceProjectName = 'my-project';
const o3rVersion = '999.0.0';
let workspacePath: string;
let packageManagerConfig: PackageManagerConfig;
const execWorkspaceOptions = getDefaultExecSyncOptions();

describe('Create new otter project command', () => {
setupLocalRegistry();
beforeEach(async () => {
({ workspacePath, packageManagerConfig, workspacePath } = (await prepareTestEnv(appFolder, {type: 'blank' })));
execWorkspaceOptions.cwd = workspacePath;
});

afterAll(async () => {
try { await fs.rm(workspacePath, { recursive: true }); } catch { /* ignore error */ }
});

test('should generate a project with an application', async () => {
const createOptions = ['--package-manager', getPackageManager(), '--skip-confirmation', ...(packageManagerConfig.yarnVersion ? ['--yarn-version', packageManagerConfig.yarnVersion] : [])];
const { workspacePath, packageManagerConfig, o3rVersion } = o3rEnvironment.testEnvironment;
const inAppPath = path.join(workspacePath, workspaceProjectName);
const execInAppOptions = {...execWorkspaceOptions, cwd: inAppPath };
const execWorkspaceOptions = {...defaultExecOptions, cwd: workspacePath };
const execInAppOptions = {...defaultExecOptions, cwd: inAppPath };
const createOptions = ['--package-manager', getPackageManager(), '--skip-confirmation', ...(packageManagerConfig.yarnVersion ? ['--yarn-version', packageManagerConfig.yarnVersion] : [])];

// TODO: remove it when fixing #1356
await fs.mkdir(inAppPath, { recursive: true });
Expand All @@ -52,11 +45,13 @@ describe('Create new otter project command', () => {
});

test('should generate a project with an application with --exact-o3r-version', async () => {
const { workspacePath, packageManagerConfig, o3rVersion } = o3rEnvironment.testEnvironment;
const inAppPath = path.join(workspacePath, workspaceProjectName);
const execWorkspaceOptions = {...defaultExecOptions, cwd: workspacePath };
const execInAppOptions = {...defaultExecOptions, cwd: inAppPath };
const packageManager = getPackageManager();
const createOptions = ['--package-manager', packageManager, '--skip-confirmation', '--exact-o3r-version',
...(packageManagerConfig.yarnVersion ? ['--yarn-version', packageManagerConfig.yarnVersion] : [])];
const inAppPath = path.join(workspacePath, workspaceProjectName);
const execInAppOptions = {...execWorkspaceOptions, cwd: inAppPath };

// TODO: remove it when fixing #1356
await fs.mkdir(inAppPath, { recursive: true });
Expand Down
Loading

0 comments on commit b4073bd

Please sign in to comment.