diff --git a/jest.config.ut.js b/jest.config.ut.js index fd16d6d2b2..f5f14693bc 100644 --- a/jest.config.ut.js +++ b/jest.config.ut.js @@ -12,11 +12,11 @@ globalThis.ngJest = { * @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] = `/${relativePath}/${path}`; - return acc; - }, {}); + const relativePath = relative(rootDir, __dirname); + const moduleNameMapper = Object.fromEntries( + Object.entries(pathsToModuleNameMapper(compilerOptions.paths)) + .map(([moduleName, path]) => [moduleName, `/${relativePath}/${path}`]) + ); return { preset: 'ts-jest', setupFilesAfterEnv: ['/testing/setup-jest.ts'], diff --git a/packages/@o3r/core/builders/app-version/index.spec.ts b/packages/@o3r/core/builders/app-version/index.spec.ts index 2790c029f3..90847877cd 100644 --- a/packages/@o3r/core/builders/app-version/index.spec.ts +++ b/packages/@o3r/core/builders/app-version/index.spec.ts @@ -1,7 +1,7 @@ -import {Architect, createBuilder} from '@angular-devkit/architect'; +import { Architect, createBuilder } from '@angular-devkit/architect'; import { TestingArchitectHost } from '@angular-devkit/architect/testing'; import { schema } from '@angular-devkit/core'; -import { cleanVirtualFileSystem, useVirtualFileSystem } from '@o3r/test-helpers'; +import { awaitFakeTimers, cleanVirtualFileSystem, useVirtualFileSystem } from '@o3r/test-helpers'; import * as fs from 'node:fs'; import * as path from 'node:path'; import { AppVersionBuilderSchema } from './schema'; @@ -30,14 +30,14 @@ describe('App version Builder', () => { it('should extract version and call pattern replacement', async () => { const packageJsonPath = path.resolve(__dirname, workspaceRoot, 'package.json'); const fakeFilePath = path.resolve(__dirname, workspaceRoot, 'app', 'package.json'); - await virtualFileSystem.promises.mkdir(path.dirname(packageJsonPath), {recursive: true}); - await virtualFileSystem.promises.writeFile(packageJsonPath, '{"version": "7.7.7"}'); + await awaitFakeTimers(virtualFileSystem.promises.mkdir(path.dirname(packageJsonPath), {recursive: true})); + await awaitFakeTimers(virtualFileSystem.promises.writeFile(packageJsonPath, '{"version": "7.7.7"}')); const options: AppVersionBuilderSchema = { file: fakeFilePath, versionToReplace: '0.0.0-placeholder' }; const run = await architect.scheduleBuilder('.:app-version', options); - const output = await run.result; + const output = await awaitFakeTimers(run.result); expect(output.error).toBeUndefined(); await run.stop(); diff --git a/packages/@o3r/core/builders/pattern-replacement/index.spec.ts b/packages/@o3r/core/builders/pattern-replacement/index.spec.ts index 9900993ba4..9ac71da9ca 100644 --- a/packages/@o3r/core/builders/pattern-replacement/index.spec.ts +++ b/packages/@o3r/core/builders/pattern-replacement/index.spec.ts @@ -1,7 +1,7 @@ import { Architect } from '@angular-devkit/architect'; import { TestingArchitectHost } from '@angular-devkit/architect/testing'; import { schema } from '@angular-devkit/core'; -import { cleanVirtualFileSystem, useVirtualFileSystem } from '@o3r/test-helpers'; +import { awaitFakeTimers, cleanVirtualFileSystem, useVirtualFileSystem } from '@o3r/test-helpers'; import * as fs from 'node:fs'; import * as path from 'node:path'; import { PatternReplacementBuilderSchema } from './schema'; @@ -27,19 +27,19 @@ describe('Pattern replacement Builder', () => { it('should replace pattern', async () => { const fakeFilePath = path.resolve(__dirname, workspaceRoot, 'not-a-real-file.txt'); - await virtualFileSystem.promises.mkdir(path.dirname(fakeFilePath), {recursive: true}); - await virtualFileSystem.promises.writeFile(fakeFilePath, 'Replace [stuff] here'); + await awaitFakeTimers(virtualFileSystem.promises.mkdir(path.dirname(fakeFilePath), {recursive: true})); + await awaitFakeTimers(virtualFileSystem.promises.writeFile(fakeFilePath, 'Replace [stuff] here')); const options: PatternReplacementBuilderSchema = { files: [fakeFilePath], searchValue: '\\[stuff\\]', replaceValue: 'things' }; const run = await architect.scheduleBuilder('.:pattern-replacement', options); - const output = await run.result; + const output = await awaitFakeTimers(run.result); expect(output.error).toBeUndefined(); await run.stop(); - const replacementOutput = await virtualFileSystem.promises.readFile(fakeFilePath, {encoding: 'utf8'}); + const replacementOutput = await awaitFakeTimers(virtualFileSystem.promises.readFile(fakeFilePath, {encoding: 'utf8'})); expect(replacementOutput).toBe('Replace things here'); }); }); diff --git a/packages/@o3r/core/jest.config.js b/packages/@o3r/core/jest.config.js index d386ad3fe0..cd284fdd2a 100644 --- a/packages/@o3r/core/jest.config.js +++ b/packages/@o3r/core/jest.config.js @@ -1,12 +1,7 @@ -const getJestConfig = require('../../../jest.config.ut').getJestConfig; - /** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */ module.exports = { - ...getJestConfig(__dirname, true), - displayName: require('./package.json').name, - fakeTimers: { - enableGlobally: true, - // TODO try to make date utils work with fake Date - doNotFake: ['Date'] - } + projects: [ + '/testing/jest.config.ut.js', + '/testing/jest.config.ut.builders.js' + ] }; diff --git a/packages/@o3r/core/project.json b/packages/@o3r/core/project.json index a396a28285..2ad6b69fdf 100644 --- a/packages/@o3r/core/project.json +++ b/packages/@o3r/core/project.json @@ -50,12 +50,6 @@ "jestConfig": "packages/@o3r/core/jest.config.js" } }, - "prepare-publish": { - "executor": "nx:run-script", - "options": { - "script": "prepare:publish" - } - }, "test-int": { "executor": "@nx/jest:jest", "options": { @@ -63,6 +57,12 @@ "silent": false } }, + "prepare-publish": { + "executor": "nx:run-script", + "options": { + "script": "prepare:publish" + } + }, "publish": { "executor": "nx:run-commands", "options": { diff --git a/packages/@o3r/core/testing/jest.config.ut.builders.js b/packages/@o3r/core/testing/jest.config.ut.builders.js new file mode 100644 index 0000000000..5ec4c1c33e --- /dev/null +++ b/packages/@o3r/core/testing/jest.config.ut.builders.js @@ -0,0 +1,16 @@ +const path = require('node:path'); +const getJestConfig = require('../../../../jest.config.ut').getJestConfig; +const rootDir = path.join(__dirname, '..'); + +/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */ +module.exports = { + ...getJestConfig(rootDir, false), + displayName: `${require('../package.json').name}/builders`, + rootDir, + setupFilesAfterEnv: [], + testPathIgnorePatterns: [ + '/.*/templates/.*', + '/src/.*', + '\\.it\\.spec\\.ts$' + ] +}; diff --git a/packages/@o3r/core/testing/jest.config.ut.js b/packages/@o3r/core/testing/jest.config.ut.js new file mode 100644 index 0000000000..2c21eb89a1 --- /dev/null +++ b/packages/@o3r/core/testing/jest.config.ut.js @@ -0,0 +1,16 @@ +const path = require('node:path'); +const getJestConfig = require('../../../../jest.config.ut').getJestConfig; +const rootDir = path.join(__dirname, '..'); + +/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */ +module.exports = { + ...getJestConfig(rootDir, true), + displayName: require('../package.json').name, + rootDir, + testPathIgnorePatterns: [ + '/.*/templates/.*', + '/schematics/.*', + '/builders/.*', + '\\.it\\.spec\\.ts$' + ] +}; diff --git a/packages/@o3r/core/testing/setup-jest.ts b/packages/@o3r/core/testing/setup-jest.ts index bad0f01916..1100b3e8a6 100644 --- a/packages/@o3r/core/testing/setup-jest.ts +++ b/packages/@o3r/core/testing/setup-jest.ts @@ -1,2 +1 @@ -import '@o3r/test-helpers/setup-jest'; import 'jest-preset-angular/setup-jest'; diff --git a/packages/@o3r/test-helpers/src/utilities/fake-timers.ts b/packages/@o3r/test-helpers/src/utilities/fake-timers.ts new file mode 100644 index 0000000000..e67b30e196 --- /dev/null +++ b/packages/@o3r/test-helpers/src/utilities/fake-timers.ts @@ -0,0 +1,7 @@ +/** + * Run all fake timers before resolving the promise + * @param promise + */ +export function awaitFakeTimers(promise: PromiseLike): Promise { + return jest.runAllTimersAsync().then(() => promise); +} diff --git a/packages/@o3r/test-helpers/src/utilities/index.ts b/packages/@o3r/test-helpers/src/utilities/index.ts index 8671f43045..160bb5bcbf 100644 --- a/packages/@o3r/test-helpers/src/utilities/index.ts +++ b/packages/@o3r/test-helpers/src/utilities/index.ts @@ -1,6 +1,7 @@ export * from './angular'; export * from './create-with-lock'; export * from './exec'; +export * from './fake-timers'; export * from './locker'; export * from './package-manager'; export * from './verdaccio';