Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: jest test not running on new otter webapp #1607

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/@o3r/core/schematics/component/container/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
getInputComponentName,
getWorkspaceConfig
} from '@o3r/schematics';
import { addImportToModule } from '@schematics/angular/utility/ast-utils';
import { addImportToModule, insertImport } from '@schematics/angular/utility/ast-utils';
import { applyToUpdateRecorder, InsertChange } from '@schematics/angular/utility/change';
import * as path from 'node:path';
import * as ts from 'typescript';
Expand Down Expand Up @@ -208,7 +208,9 @@ function ngGenerateComponentContainerFn(options: NgGenerateComponentContainerSch
ts.isImportDeclaration(statement)
);

const changes = [new InsertChange(o3rSpecPath, lastImport?.getEnd() || 0, `
const changes = [
insertImport(specSourceFile, o3rSpecPath, 'Component', '@angular/core'),
new InsertChange(o3rSpecPath, lastImport?.getEnd() || 0, `
@Component({
template: '',
selector: '${properties.presenterComponentSelector}',
Expand Down
4 changes: 2 additions & 2 deletions packages/@o3r/core/schematics/ng-add/utils/linter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export const shouldOtterLinterBeInstalled = async (context: SchematicContext): P
try {
require.resolve(`${linterPackageName}/package.json`);
if (context.interactive) {
useOtterLinter = await askConfirmation(`You already have eslint installed. Would you like to add otter config rules for eslint ?
useOtterLinter = await askConfirmation(`You already have eslint installed. Would you like to add otter config rules for eslint?
Otherwise, you can add them later via this command: ng add @o3r/eslint-config-otter`, true);
}
} catch {
context.logger.info(`eslint package not installed. Skipping otter linter phase !
context.logger.info(`eslint package not installed. Skipping otter linter phase!
You can add otter linter config rules later to the project via this command: ng add @o3r/eslint-config-otter`);
}

Expand Down
11 changes: 10 additions & 1 deletion packages/@o3r/testing/schematics/index.it.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ describe('new otter application with testing', () => {
afterAll(async () => {
try { await rm(workspacePath, { recursive: true }); } catch { /* ignore error */ }
});
test('should add testing to existing application', async () => {
test('should add testing to existing application', () => {
packageManagerExec({script: 'ng', args: ['add', `@o3r/testing@${o3rVersion}`, '--skip-confirmation', '--project-name', projectName]}, execAppOptions);

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

test('should add testing to existing application and fixture to component', async () => {
const relativeProjectPath = path.relative(workspacePath, projectPath);
packageManagerExec({script: 'ng', args: ['add', `@o3r/testing@${o3rVersion}`, '--skip-confirmation', '--project-name', projectName]}, execAppOptions);

Expand All @@ -48,5 +56,6 @@ describe('new otter application with testing', () => {

expect(() => packageManagerInstall(execAppOptions)).not.toThrow();
expect(() => packageManagerRunOnProject(projectName, isInWorkspace, {script: 'build'}, execAppOptions)).not.toThrow();
expect(() => packageManagerRunOnProject(projectName, isInWorkspace, {script: 'test'}, execAppOptions)).not.toThrow();
});
});
9 changes: 1 addition & 8 deletions packages/@o3r/testing/schematics/ng-add/fixture/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import * as ts from 'typescript';
* Add fixture configuration
* @param options @see RuleFactory.options
* @param options.projectName
* @param registerUnitTestFixturePaths should add path mapping for unit test fixtures
* @param options.testingFramework
*/
export function updateFixtureConfig(options: { projectName?: string | null | undefined; testingFramework?: string | null | undefined }, registerUnitTestFixturePaths: boolean): Rule {
export function updateFixtureConfig(options: { projectName?: string | null | undefined; testingFramework?: string | null | undefined }): Rule {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can't do breaking change

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we cannot do a breaking change if the method is exposed from the package and here it is not the case.
image


const oldPaths = ['@otter/testing/core', '@otter/testing/core/*'];
/**
Expand Down Expand Up @@ -91,12 +90,6 @@ export function updateFixtureConfig(options: { projectName?: string | null | und
return acc;
}, {});

if (registerUnitTestFixturePaths) {
configWithPath.content.compilerOptions.baseUrl ||= '.';
configWithPath.content.compilerOptions.paths['@o3r/testing/core'] = ['node_modules/@o3r/testing/core/angular'];
configWithPath.content.compilerOptions.paths['@o3r/testing/core/*'] = ['node_modules/@o3r/testing/core/angular/*'];
}

tree.overwrite(configWithPath.tsconfig, JSON.stringify(configWithPath.content, null, 2));

return tree;
Expand Down
10 changes: 6 additions & 4 deletions packages/@o3r/testing/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ const devDependenciesToInstall = [
'pixelmatch',
'pngjs',
'jest',
'jest-preset-angular'
'jest-environment-jsdom',
'jest-preset-angular',
'ts-jest',
'@types/jest'
];

/**
Expand Down Expand Up @@ -90,7 +93,7 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule {
await askConfirmation('Do you want to setup Playwright test framework for E2E?', true);

const rules = [
updateFixtureConfig(options, installJest),
updateFixtureConfig(options),
removePackages(['@otter/testing']),
addVsCodeRecommendations(['Orta.vscode-jest']),
installPlaywright ? updatePlaywright(options, dependencies) : noop,
Expand Down Expand Up @@ -135,8 +138,7 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule {

const jestConfigFilesForWorkspace = () => mergeWith(apply(url('./templates/workspace'), [
template({
...options,
rootRelativePath
...options
}),
move(tree.root.path),
renameTemplateFiles()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { relative } = require('node:path');
const { join, relative } = require('node:path');
const { pathsToModuleNameMapper } = require('ts-jest');
// In the following statement, replace `<%= rootRelativePath %>/tsconfig.base` with the path to your `tsconfig` file
// In the following statement, replace `./tsconfig.json` with the path to your `tsconfig` file
// which contains the path mapping (ie the `compilerOptions.paths` option):
const { compilerOptions } = require('<%= rootRelativePath %>/tsconfig.base');
const { compilerOptions } = require('./tsconfig.json');

globalThis.ngJest = {
skipNgcc: true
Expand All @@ -14,11 +14,13 @@ globalThis.ngJest = {
* @returns {import('ts-jest/dist/types').JestConfigWithTsJest}
*/
module.exports.getJestProjectConfig = (rootDir, isAngularSetup) => {
const relativePath = relative(rootDir, '.');
const moduleNameMapper = Object.entries(pathsToModuleNameMapper(compilerOptions.paths)).reduce((acc, [moduleName, path]) => {
const relativePath = relative(rootDir, __dirname);
const moduleNameMapper = Object.entries(pathsToModuleNameMapper(compilerOptions.paths || {})).reduce((acc, [moduleName, path]) => {
acc[moduleName] = `<rootDir>/${relativePath}/${path}`;
return acc;
}, {});
moduleNameMapper['^@o3r/testing/core$'] = [require.resolve('@o3r/testing/core/angular')];
moduleNameMapper['^@o3r/testing/core/(.*)'] = [join(require.resolve('@o3r/testing/core/angular'), '$1')];
kpanot marked this conversation as resolved.
Show resolved Hide resolved
return {
preset: 'ts-jest',
setupFilesAfterEnv: ['<rootDir>/testing/setup-jest.ts'],
Expand Down
Loading