Skip to content

Commit

Permalink
test: rework unit tests to separate dom and node tests (#1276)
Browse files Browse the repository at this point in the history
## Proposed change

Currently, we only have 1 jest config for all unit-tests
Unfortunately, the tests for `src` usually don't require the same
environment as the tests for `schematics` and `builders`

The idea is to have a different project for each (check
`packages/@o3r/core/jest.config.ts`)

This will fix the tests issue on
#1255 and
#1243

## Related issues

- 🐛 Fixes #(issue)
- 🚀 Feature #(issue)

<!-- Please make sure to follow the contributing guidelines on
https://github.com/amadeus-digital/Otter/blob/main/CONTRIBUTING.md -->
  • Loading branch information
fpaul-1A authored Jan 30, 2024
2 parents cbe8d03 + 84152f3 commit 380a2cc
Show file tree
Hide file tree
Showing 235 changed files with 1,717 additions and 393 deletions.
2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ packageExtensions:
"@angular/compiler-cli": ~17.0.4
"@schematics/angular": ~17.0.3
"@types/node": ^20.0.0
esbuild: ~0.19.0
esbuild: ~0.20.0
eslint: ^8.42.0
nx: ~17.2.0
rxjs: ^7.8.1
Expand Down
17 changes: 5 additions & 12 deletions apps/chrome-devtools/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
const getJestConfig = require('../../jest.config.ut').getJestConfig;
const getJestGlobalConfig = require('../../jest.config.ut').getJestGlobalConfig;

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
...getJestConfig(__dirname, true),
displayName: require('./package.json').name,
transformIgnorePatterns: ['^.+\\.js$'],
globals: {
chrome: {
runtime: {},
devtools: {
inspectedWindow: {}
}
}
}
...getJestGlobalConfig(),
projects: [
'<rootDir>/testing/jest.config.ut.js'
]
};
19 changes: 19 additions & 0 deletions apps/chrome-devtools/testing/jest.config.ut.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const path = require('node:path');
const getJestProjectConfig = require('../../../jest.config.ut').getJestProjectConfig;
const rootDir = path.join(__dirname, '..');

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
...getJestProjectConfig(rootDir, true),
displayName: require('../package.json').name,
rootDir,
transformIgnorePatterns: ['^.+\\.js$'],
globals: {
chrome: {
runtime: {},
devtools: {
inspectedWindow: {}
}
}
}
};
8 changes: 5 additions & 3 deletions apps/github-cascading-app/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const getJestConfig = require('../../jest.config.ut').getJestConfig;
const getJestGlobalConfig = require('../../jest.config.ut').getJestGlobalConfig;

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
...getJestConfig(__dirname, true),
displayName: require('./package.json').name
...getJestGlobalConfig(),
projects: [
'<rootDir>/testing/jest.config.ut.js'
]
};
10 changes: 10 additions & 0 deletions apps/github-cascading-app/testing/jest.config.ut.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require('node:path');
const getJestProjectConfig = require('../../../jest.config.ut').getJestProjectConfig;
const rootDir = path.join(__dirname, '..');

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
...getJestProjectConfig(rootDir, true),
displayName: require('../package.json').name,
rootDir
};
10 changes: 6 additions & 4 deletions apps/showcase/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const getJestConfig = require('../../jest.config.ut').getJestConfig;
const getJestGlobalConfig = require('../../jest.config.ut').getJestGlobalConfig;

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
...getJestConfig(__dirname, true),
displayName: require('./package.json').name
...getJestGlobalConfig(),
projects: [
'<rootDir>/testing/jest.config.ut.js'
]
};
10 changes: 10 additions & 0 deletions apps/showcase/testing/jest.config.ut.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require('node:path');
const getJestProjectConfig = require('../../../jest.config.ut').getJestProjectConfig;
const rootDir = path.join(__dirname, '..');

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
...getJestProjectConfig(rootDir, true),
displayName: require('../package.json').name,
rootDir
};
11 changes: 4 additions & 7 deletions apps/vscode-extension/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
const getJestConfig = require('../../jest.config.ut').getJestConfig;
const getJestGlobalConfig = require('../../jest.config.ut').getJestGlobalConfig;

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
...getJestConfig(__dirname, false),
displayName: require('./package.json').name,
testPathIgnorePatterns: [
'<rootDir>/dist',
'<rootDir>/.*/templates/.*',
'\\.it\\.spec\\.ts$'
...getJestGlobalConfig(),
projects: [
'<rootDir>/testing/jest.config.ut.js'
]
};
10 changes: 10 additions & 0 deletions apps/vscode-extension/testing/jest.config.ut.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require('node:path');
const getJestProjectConfig = require('../../../jest.config.ut').getJestProjectConfig;
const rootDir = path.join(__dirname, '..');

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
...getJestProjectConfig(rootDir, false),
displayName: require('../package.json').name,
rootDir
};
4 changes: 2 additions & 2 deletions jest.config.it.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const getJestConfig = require('./jest.config.ut').getJestConfig;
const getJestProjectConfig = require('./jest.config.ut').getJestProjectConfig;


/**
* @param rootDir {string}
* @returns {import('ts-jest/dist/types').JestConfigWithTsJest}
*/
module.exports.getJestConfig = (rootDir) => ({
...getJestConfig(rootDir, false),
...getJestProjectConfig(rootDir, false),
rootDir: '..',
setupFilesAfterEnv: null,
testPathIgnorePatterns: [
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { getJestProjects } = require('@o3r/workspace');
module.exports = {
projects: [
...getJestProjects(__dirname),
...getJestProjects(__dirname, 'testing/jest.config.it.{j,t}s')
...getJestProjects(__dirname, 'testing/jest.config.*.{j,t}s')
],
globalSetup: 'jest-preset-angular/global-setup',
reporters: [
Expand Down
34 changes: 22 additions & 12 deletions jest.config.ut.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ globalThis.ngJest = {
};

/**
* Jest configuration that can be set at project level
* @param rootDir {string}
* @param isAngularSetup {boolean}
* @returns {import('ts-jest/dist/types').JestConfigWithTsJest}
*/
module.exports.getJestConfig = (rootDir, isAngularSetup) => {
const relativePath = relative(rootDir, '.');
const moduleNameMapper = Object.entries(pathsToModuleNameMapper(compilerOptions.paths)).reduce((acc, [moduleName, path]) => {
acc[moduleName] = `<rootDir>/${relativePath}/${path}`;
return acc;
}, {});
module.exports.getJestProjectConfig = (rootDir, isAngularSetup) => {
const relativePath = relative(rootDir, __dirname);
const moduleNameMapper = Object.fromEntries(
Object.entries(pathsToModuleNameMapper(compilerOptions.paths))
.map(([moduleName, path]) => [moduleName, `<rootDir>/${relativePath}/${path}`])
);
return {
preset: 'ts-jest',
setupFilesAfterEnv: ['<rootDir>/testing/setup-jest.ts'],
Expand All @@ -29,11 +30,6 @@ module.exports.getJestConfig = (rootDir, isAngularSetup) => {
'<rootDir>/.*/templates/.*',
'\\.it\\.spec\\.ts$'
],
reporters: [
'default',
['jest-junit', {outputDirectory: '<rootDir>/dist-test', outputName: 'ut-report.xml'}],
'github-actions'
],
fakeTimers: {
enableGlobally: true
},
Expand All @@ -51,7 +47,6 @@ module.exports.getJestConfig = (rootDir, isAngularSetup) => {
// workaround for the SDK Core
customExportConditions: ['require', 'node']
},
testTimeout: 20000,
workerIdleMemoryLimit: '700MB',
...isAngularSetup ? {
preset: 'jest-preset-angular',
Expand All @@ -69,3 +64,18 @@ module.exports.getJestConfig = (rootDir, isAngularSetup) => {
} : {}
};
}

/**
* Jest configuration that can be set at root level
* @returns {import('ts-jest/dist/types').JestConfigWithTsJest}
*/
module.exports.getJestGlobalConfig = () => {
return {
testTimeout: 30000,
reporters: [
'default',
['jest-junit', {outputDirectory: '<rootDir>/dist-test', outputName: 'ut-report.xml'}],
'github-actions'
],
}
}
14 changes: 6 additions & 8 deletions packages/@ama-sdk/core/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const getJestConfig = require('../../../jest.config.ut').getJestConfig;
const getJestGlobalConfig = require('../../../jest.config.ut').getJestGlobalConfig;

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
...getJestConfig(__dirname, false),
displayName: require('./package.json').name,
fakeTimers: {
enableGlobally: true,
// TODO try to make date utils work with fake Date
doNotFake: ['Date']
}
...getJestGlobalConfig(),
projects: [
'<rootDir>/testing/jest.config.ut.js',
'<rootDir>/testing/jest.config.ut.builders.js'
]
};
1 change: 1 addition & 0 deletions packages/@ama-sdk/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"@nx/jest": "~17.2.0",
"@o3r/build-helpers": "workspace:^",
"@o3r/eslint-plugin": "workspace:^",
"@o3r/test-helpers": "workspace:^",
"@schematics/angular": "~17.0.3",
"@swc/cli": "^0.3.0",
"@swc/core": "^1.3.85",
Expand Down
4 changes: 0 additions & 4 deletions packages/@ama-sdk/core/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { firstValueFrom } from 'rxjs';
import { ngAdd } from './index';
jest.mock('@o3r/schematics', () => ({
...jest.requireActual('@o3r/schematics'),
createSchematicWithMetricsIfInstalled: jest.fn().mockImplementation((fn) => fn)
}));

describe('Ng add @ama-sdk/core', () => {
it('should update imports', async () => {
Expand Down
15 changes: 15 additions & 0 deletions packages/@ama-sdk/core/testing/jest.config.ut.builders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const path = require('node:path');
const getJestProjectConfig = require('../../../../jest.config.ut').getJestProjectConfig;
const rootDir = path.join(__dirname, '..');

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
...getJestProjectConfig(rootDir, false),
displayName: `${require('../package.json').name}/builders`,
rootDir,
testPathIgnorePatterns: [
'<rootDir>/.*/templates/.*',
'<rootDir>/src/.*',
'\\.it\\.spec\\.ts$'
]
};
21 changes: 21 additions & 0 deletions packages/@ama-sdk/core/testing/jest.config.ut.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const path = require('node:path');
const getJestProjectConfig = require('../../../../jest.config.ut').getJestProjectConfig;
const rootDir = path.join(__dirname, '..');

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
...getJestProjectConfig(rootDir, false),
displayName: require('../package.json').name,
rootDir,
fakeTimers: {
enableGlobally: true,
// TODO re-enable fake dates when issue fixed https://github.com/sinonjs/fake-timers/issues/437
doNotFake: ['Date']
},
testPathIgnorePatterns: [
'<rootDir>/.*/templates/.*',
'<rootDir>/builders/.*',
'<rootDir>/schematics/.*',
'\\.it\\.spec\\.ts$'
]
};
2 changes: 2 additions & 0 deletions packages/@ama-sdk/core/testing/setup-jest.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import 'isomorphic-fetch';
import '@o3r/test-helpers/setup-jest-builders';

14 changes: 6 additions & 8 deletions packages/@ama-sdk/schematics/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const getJestConfig = require('../../../jest.config.ut').getJestConfig;
const getJestGlobalConfig = require('../../../jest.config.ut').getJestGlobalConfig;

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
...getJestConfig(__dirname, false),
displayName: require('./package.json').name,
testPathIgnorePatterns: [
'<rootDir>/.*/templates/.*',
'\\.it\\.spec\\.ts$'
],
testTimeout: 30 * 1000
...getJestGlobalConfig(),
projects: [
'<rootDir>/testing/jest.config.ut.js',
'<rootDir>/testing/jest.config.ut.builders.js'
]
};
1 change: 1 addition & 0 deletions packages/@ama-sdk/schematics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"@o3r/build-helpers": "workspace:^",
"@o3r/eslint-plugin": "workspace:^",
"@o3r/schematics": "workspace:^",
"@o3r/test-helpers": "workspace:^",
"@openapitools/openapi-generator-cli": "~2.9.0",
"@schematics/angular": "~17.0.3",
"@types/jest": "~29.5.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('Typescript Shell Generator', () => {
skipInstall: true,
packageManager: 'npm'
}, Tree.empty());
}, 15000);
});

it('should generate basic SDK package', () => {
expect(yarnTree.files.sort()).toEqual(baseFileList.sort());
Expand Down
15 changes: 15 additions & 0 deletions packages/@ama-sdk/schematics/testing/jest.config.ut.builders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const path = require('node:path');
const getJestProjectConfig = require('../../../../jest.config.ut').getJestProjectConfig;
const rootDir = path.join(__dirname, '..');

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
...getJestProjectConfig(rootDir, false),
displayName: `${require('../package.json').name}/builders`,
rootDir,
testPathIgnorePatterns: [
'<rootDir>/.*/templates/.*',
'<rootDir>/src/.*',
'\\.it\\.spec\\.ts$'
]
};
16 changes: 16 additions & 0 deletions packages/@ama-sdk/schematics/testing/jest.config.ut.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const path = require('node:path');
const getJestProjectConfig = require('../../../../jest.config.ut').getJestProjectConfig;
const rootDir = path.join(__dirname, '..');

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
...getJestProjectConfig(rootDir, false),
displayName: require('../package.json').name,
rootDir,
testPathIgnorePatterns: [
'<rootDir>/.*/templates/.*',
'<rootDir>/builders/.*',
'<rootDir>/schematics/.*',
'\\.it\\.spec\\.ts$'
]
};
1 change: 1 addition & 0 deletions packages/@ama-sdk/schematics/testing/setup-jest.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import 'isomorphic-fetch';
import '@o3r/test-helpers/setup-jest-builders';
1 change: 1 addition & 0 deletions packages/@ama-sdk/showcase-sdk/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
'parserOptions': {
'tsconfigRootDir': __dirname,
'project': [
'tsconfig.eslint.json',
'testing/tsconfig.spec.json',
'tsconfigs/tsconfig.jasmine.json',
'tsconfigs/tsconfig.jest.json',
Expand Down
13 changes: 5 additions & 8 deletions packages/@ama-sdk/showcase-sdk/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
const { getJestConfig } = require('../../../jest.config.ut');
const getJestGlobalConfig = require('../../../jest.config.ut').getJestGlobalConfig;

/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
...getJestConfig(__dirname, false),
displayName: require('./package.json').name,
setupFilesAfterEnv: null,
moduleNameMapper: {
'^@ama-sdk/showcase-sdk$': ['<rootDir>/dist/cjs', '<rootDir>/src'],
'^@ama-sdk/showcase-sdk/(.*)$': ['<rootDir>/dist/cjs/$1', '<rootDir>/src/$1'],
}
...getJestGlobalConfig(),
projects: [
'<rootDir>/testing/jest.config.ut.js'
]
};
Loading

0 comments on commit 380a2cc

Please sign in to comment.