Skip to content

Commit 6adde1c

Browse files
authored
Merge pull request #66 from HarperFast/chore/unit-tests-third-times-a-charm
Migrate component unit tests
2 parents de67922 + 630b499 commit 6adde1c

23 files changed

+217
-143
lines changed

package.json

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,26 @@
3939
"format:write": "prettier --write .",
4040
"test:integration:api-tests": "node --test integrationTests/apiTests/tests/testSuite.mjs",
4141
"test:integration": "node integrationTests/utils/scripts/run.ts",
42-
"test:unit": "TSX_TSCONFIG_PATH=./unitTests/tsconfig.json npx mocha --config unitTests/.mocharc.json",
43-
"test:unit:all": "npm run test:unit unitTests"
42+
"test:unit": "mocha --config unitTests/.mocharc.json",
43+
"test:unit:all": "npm run test:unit unitTests",
44+
"test:unit:typestrip": "env NODE_OPTIONS='--conditions=typestrip' npm run test:unit",
45+
"test:unit:typestrip:all": "npm run test:unit:typestrip unitTests"
46+
},
47+
"imports": {
48+
"#src/*": {
49+
"import": "./*.ts",
50+
"require": {
51+
"typestrip": "./*.ts",
52+
"default": "./dist/*.js"
53+
}
54+
},
55+
"#js/*": {
56+
"import": "./*.js",
57+
"require": {
58+
"typestrip": "./*.js",
59+
"default": "./dist/*.js"
60+
}
61+
}
4462
},
4563
"engines": {
4664
"node": ">=20",

unitTests/.mocharc.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
{
2-
"$schema": "https://json.schemastore.org/mocharc.json",
3-
"require": "tsx",
42
"timeout": 0,
53
"ui": "bdd",
6-
"extension": ["js", "mjs", "ts"],
4+
"extension": ["js", "mjs"],
75
"recursive": true,
86
"exit": true
97
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { describe, it } from 'mocha';
2-
import { Component, ComponentInvalidPatternError } from '@/components/Component';
3-
import assert from 'node:assert/strict';
1+
/* eslint-disable sonarjs/no-nested-functions */
2+
const { Component, ComponentInvalidPatternError } = require('#src/components/Component');
3+
const assert = require('node:assert/strict');
44

55
describe('Component', () => {
66
const name = 'test-component';
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable @typescript-eslint/no-require-imports, sonarjs/void-use, sonarjs/assertions-in-tests, sonarjs/no-nested-functions */
22
// Not sure why sonar cannot pick up the assertions within the tests. I think its because they are `async`.
33

4-
import { tmpdir } from 'node:os';
5-
import {
4+
const { tmpdir } = require('node:os');
5+
const {
66
processResourceExtensionComponent,
77
ComponentV1,
88
InvalidFilesOptionError,
@@ -13,13 +13,13 @@ import {
1313
InvalidRootOptionError,
1414
InvalidPathOptionError,
1515
InvalidURLPathOptionError,
16-
} from '@/components/ComponentV1';
17-
import { Resources } from '@/resources/Resources';
18-
import assert from 'node:assert/strict';
19-
import { join } from 'node:path';
20-
import { mkdtempSync, writeFileSync, mkdirSync, rmSync } from 'node:fs';
21-
import { fake, restore, replace } from 'sinon';
22-
import fg from 'fast-glob';
16+
} = require('#src/components/ComponentV1');
17+
const { Resources } = require('#src/resources/Resources');
18+
const assert = require('node:assert/strict');
19+
const { join } = require('node:path');
20+
const { mkdtempSync, writeFileSync, mkdirSync, rmSync } = require('node:fs');
21+
const { fake, restore, replace } = require('sinon');
22+
const fg = require('fast-glob');
2323

2424
const TEMP_DIR_PATH = join(tmpdir(), 'harper.unit-test.component-v1-');
2525

@@ -45,7 +45,7 @@ function createTempFixture(fixture) {
4545
describe('ComponentV1', () => {
4646
const componentName = 'test-component';
4747

48-
const harperLogger = require('@/utility/logging/harper_logger');
48+
const harperLogger = require('#js/utility/logging/harper_logger');
4949

5050
beforeEach(() => {
5151
replace(harperLogger, 'warn', fake());
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { EntryHandler } from '@/components/EntryHandler';
2-
import { EventEmitter, once } from 'node:events';
3-
import assert from 'node:assert/strict';
4-
import { join, basename } from 'node:path';
5-
import { tmpdir } from 'node:os';
6-
import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs';
7-
import { writeFile, mkdir } from 'node:fs/promises';
8-
import { spy } from 'sinon';
9-
import { waitFor } from './waitFor';
1+
const { EntryHandler } = require('#src/components/EntryHandler');
2+
const { EventEmitter, once } = require('node:events');
3+
const assert = require('node:assert/strict');
4+
const { join, basename } = require('node:path');
5+
const { tmpdir } = require('node:os');
6+
const { mkdtempSync, mkdirSync, writeFileSync, rmSync } = require('node:fs');
7+
const { writeFile, mkdir } = require('node:fs/promises');
8+
const { spy } = require('sinon');
9+
const { waitFor } = require('./waitFor.js');
1010

1111
function generateFixture(dirPath, fixture) {
1212
mkdirSync(dirPath, { recursive: true });

unitTests/components/OptionsWatcher.test.ts renamed to unitTests/components/OptionsWatcher.test.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/* eslint-disable sonarjs/no-nested-functions */
2-
import { OptionsWatcher } from '@/components/OptionsWatcher';
3-
import { EventEmitter, once } from 'node:events';
4-
import assert from 'node:assert/strict';
5-
import { join } from 'node:path';
6-
import { tmpdir } from 'node:os';
7-
import { mkdtempSync, writeFileSync, rmSync } from 'node:fs';
8-
import { writeFile, rm } from 'node:fs/promises';
9-
import { stringify } from 'yaml';
10-
import { spy } from 'sinon';
11-
import { DEFAULT_CONFIG } from '@/components/DEFAULT_CONFIG';
2+
const { OptionsWatcher } = require('#src/components/OptionsWatcher');
3+
const { EventEmitter, once } = require('node:events');
4+
const assert = require('node:assert/strict');
5+
const { join } = require('node:path');
6+
const { tmpdir } = require('node:os');
7+
const { mkdtempSync, writeFileSync, rmSync } = require('node:fs');
8+
const { writeFile, rm } = require('node:fs/promises');
9+
const { stringify } = require('yaml');
10+
const { spy } = require('sinon');
11+
const { DEFAULT_CONFIG } = require('#src/components/DEFAULT_CONFIG');
1212

1313
/**
1414
* This function asserts that an event is emitted.
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import { Scope, MissingDefaultFilesOptionError } from '@/components/Scope';
2-
import { EventEmitter } from 'node:events';
3-
import assert from 'node:assert/strict';
4-
import { join, basename } from 'node:path';
5-
import { tmpdir } from 'node:os';
6-
import { mkdtempSync, writeFileSync, rmSync } from 'node:fs';
7-
import { stringify } from 'yaml';
8-
import { spy } from 'sinon';
9-
import { OptionsWatcher } from '@/components/OptionsWatcher';
10-
import { Resources } from '@/resources/Resources';
11-
import { EntryHandler } from '@/components/EntryHandler';
12-
import { restartNeeded, resetRestartNeeded } from '@/components/requestRestart';
13-
import { writeFile } from 'node:fs/promises';
14-
import { waitFor } from './waitFor';
1+
const { Scope, MissingDefaultFilesOptionError } = require('#src/components/Scope');
2+
const { EventEmitter } = require('node:events');
3+
const assert = require('node:assert/strict');
4+
const { join, basename } = require('node:path');
5+
const { tmpdir } = require('node:os');
6+
const { mkdtempSync, writeFileSync, rmSync } = require('node:fs');
7+
const { stringify } = require('yaml');
8+
const { spy } = require('sinon');
9+
const { OptionsWatcher } = require('#src/components/OptionsWatcher');
10+
const { Resources } = require('#src/resources/Resources');
11+
const { EntryHandler } = require('#src/components/EntryHandler');
12+
const { restartNeeded, resetRestartNeeded } = require('#src/components/requestRestart');
13+
const { writeFile } = require('node:fs/promises');
14+
const { waitFor } = require('./waitFor.js');
1515

1616
describe('Scope', () => {
1717
beforeEach(() => {

unitTests/components/componentLoader.test.ts renamed to unitTests/components/componentLoader.test.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
import { describe, it, before, beforeEach, after } from 'mocha';
2-
import assert from 'node:assert/strict';
3-
import sinon from 'sinon';
4-
import path from 'path';
5-
import { tmpdir } from 'os';
6-
import { mkdtempSync, mkdirSync, writeFileSync, rmSync, existsSync } from 'fs';
1+
const assert = require('node:assert/strict');
2+
const sinon = require('sinon');
3+
const path = require('path');
4+
const { tmpdir } = require('os');
5+
const { mkdtempSync, mkdirSync, writeFileSync, rmSync, existsSync } = require('fs');
76

87
describe('ComponentLoader Status Integration', function () {
98
let componentStatusRegistry;
109
let tempDir;
1110
let componentLoader;
1211
let lifecycle;
1312

14-
before(() => {
13+
before(function () {
1514
// Create a temporary directory for test components
1615
tempDir = mkdtempSync(path.join(tmpdir(), 'harper-test-components-'));
1716

1817
// Mock environment to use our temp directory
19-
const env = require('@/utility/environment/environmentManager');
18+
const env = require('#js/utility/environment/environmentManager');
2019
sinon.stub(env, 'get').callsFake((key) => {
2120
if (key === 'COMPONENTSROOT') {
2221
return tempDir;
@@ -30,7 +29,7 @@ describe('ComponentLoader Status Integration', function () {
3029
});
3130

3231
// Get both the lifecycle and internal objects
33-
const statusModule = require('@/components/status');
32+
const statusModule = require('#src/components/status/index');
3433
const { internal } = statusModule;
3534
lifecycle = statusModule.lifecycle;
3635
componentStatusRegistry = internal.componentStatusRegistry;
@@ -48,14 +47,14 @@ describe('ComponentLoader Status Integration', function () {
4847
sinon.spy(componentStatusRegistry, 'getStatus');
4948

5049
// Mock getConfigObj to avoid loading real config for root components
51-
const configUtils = require('@/config/configUtils');
50+
const configUtils = require('#js/config/configUtils');
5251
sinon.stub(configUtils, 'getConfigObj').returns({});
5352

5453
// Clear the componentLoader from require cache to ensure it gets our spied lifecycle
55-
delete require.cache[require.resolve('@/components/componentLoader')];
54+
delete require.cache[require.resolve('#src/components/componentLoader')];
5655

5756
// Load componentLoader after setting up spies
58-
componentLoader = require('@/components/componentLoader');
57+
componentLoader = require('#src/components/componentLoader');
5958
});
6059

6160
after(function () {
@@ -215,7 +214,7 @@ describe('ComponentLoader Status Integration', function () {
215214

216215
it('should handle component loading errors gracefully', async function () {
217216
// Stub the dataLoader module's handleApplication method to throw an error
218-
const dataLoaderModule = require('@/resources/dataLoader');
217+
const dataLoaderModule = require('#src/resources/dataLoader');
219218
const originalhandleApplication = dataLoaderModule.handleApplication;
220219
sinon.stub(dataLoaderModule, 'handleApplication').throws(new Error('DataLoader failed to initialize'));
221220

unitTests/components/deriveCommonPatternBase.test.ts renamed to unitTests/components/deriveCommonPatternBase.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { deriveCommonPatternBase } from '@/components/deriveCommonPatternBase';
2-
import assert from 'node:assert/strict';
1+
const { deriveCommonPatternBase } = require('#src/components/deriveCommonPatternBase');
2+
const assert = require('node:assert/strict');
33

44
describe('deriveCommonPatternBase', () => {
55
[

unitTests/components/deriveGlobOption.test.ts renamed to unitTests/components/deriveGlobOption.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import assert from 'node:assert/strict';
2-
import { deriveGlobOptions } from '@/components/deriveGlobOptions';
1+
const assert = require('node:assert/strict');
2+
const { deriveGlobOptions } = require('#src/components/deriveGlobOptions');
33

44
// components/deriveGlobOptions.test.ts
55

0 commit comments

Comments
 (0)