Skip to content

Commit

Permalink
test: rework unit tests to separate dom and node tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fpaul-1A committed Jan 25, 2024
1 parent 96d9658 commit 9f73632
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 31 deletions.
10 changes: 5 additions & 5 deletions jest.config.ut.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] = `<rootDir>/${relativePath}/${path}`;
return acc;
}, {});
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 Down
10 changes: 5 additions & 5 deletions packages/@o3r/core/builders/app-version/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();

Expand Down
10 changes: 5 additions & 5 deletions packages/@o3r/core/builders/pattern-replacement/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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');
});
});
13 changes: 4 additions & 9 deletions packages/@o3r/core/jest.config.js
Original file line number Diff line number Diff line change
@@ -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: [
'<rootDir>/testing/jest.config.ut.js',
'<rootDir>/testing/jest.config.ut.builders.js'
]
};
12 changes: 6 additions & 6 deletions packages/@o3r/core/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@
"jestConfig": "packages/@o3r/core/jest.config.js"
}
},
"prepare-publish": {
"executor": "nx:run-script",
"options": {
"script": "prepare:publish"
}
},
"test-int": {
"executor": "@nx/jest:jest",
"options": {
"jestConfig": "packages/@o3r/core/testing/jest.config.it.js",
"silent": false
}
},
"prepare-publish": {
"executor": "nx:run-script",
"options": {
"script": "prepare:publish"
}
},
"publish": {
"executor": "nx:run-commands",
"options": {
Expand Down
16 changes: 16 additions & 0 deletions packages/@o3r/core/testing/jest.config.ut.builders.js
Original file line number Diff line number Diff line change
@@ -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: [
'<rootDir>/.*/templates/.*',
'<rootDir>/src/.*',
'\\.it\\.spec\\.ts$'
]
};
16 changes: 16 additions & 0 deletions packages/@o3r/core/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 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: [
'<rootDir>/.*/templates/.*',
'<rootDir>/schematics/.*',
'<rootDir>/builders/.*',
'\\.it\\.spec\\.ts$'
]
};
1 change: 0 additions & 1 deletion packages/@o3r/core/testing/setup-jest.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
import '@o3r/test-helpers/setup-jest';
import 'jest-preset-angular/setup-jest';
7 changes: 7 additions & 0 deletions packages/@o3r/test-helpers/src/utilities/fake-timers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Run all fake timers before resolving the promise
* @param promise
*/
export function awaitFakeTimers<T>(promise: PromiseLike<T>): Promise<T> {
return jest.runAllTimersAsync().then(() => promise);
}
1 change: 1 addition & 0 deletions packages/@o3r/test-helpers/src/utilities/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down

0 comments on commit 9f73632

Please sign in to comment.