Skip to content

Commit 362a4f3

Browse files
fix: Improve test cleanup and remove non-null assertions
- Add try-finally blocks to ensure NODE_ENV is always restored in tests - Refactor ensureIndexSet to avoid non-null assertion operator - Improve test robustness and code safety All tests passing (244/244) Co-authored-by: xuyushun441-sys <255036401+xuyushun441-sys@users.noreply.github.com>
1 parent 9724f4e commit 362a4f3

2 files changed

Lines changed: 70 additions & 62 deletions

File tree

packages/core/src/api-registry.test.ts

Lines changed: 65 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -989,76 +989,82 @@ describe('ApiRegistry', () => {
989989
describe('Safety Guards', () => {
990990
it('should allow clear() in non-production environment', () => {
991991
const originalEnv = process.env.NODE_ENV;
992-
process.env.NODE_ENV = 'test';
992+
try {
993+
process.env.NODE_ENV = 'test';
993994

994-
registry.registerApi({
995-
id: 'test_api',
996-
name: 'Test API',
997-
type: 'rest',
998-
version: 'v1',
999-
basePath: '/test',
1000-
endpoints: [{ id: 'e1', path: '/test', responses: [] }],
1001-
});
1002-
1003-
expect(registry.getStats().totalApis).toBe(1);
1004-
1005-
// Should work without force flag in non-production
1006-
registry.clear();
1007-
expect(registry.getStats().totalApis).toBe(0);
1008-
1009-
process.env.NODE_ENV = originalEnv;
995+
registry.registerApi({
996+
id: 'test_api',
997+
name: 'Test API',
998+
type: 'rest',
999+
version: 'v1',
1000+
basePath: '/test',
1001+
endpoints: [{ id: 'e1', path: '/test', responses: [] }],
1002+
});
1003+
1004+
expect(registry.getStats().totalApis).toBe(1);
1005+
1006+
// Should work without force flag in non-production
1007+
registry.clear();
1008+
expect(registry.getStats().totalApis).toBe(0);
1009+
} finally {
1010+
process.env.NODE_ENV = originalEnv;
1011+
}
10101012
});
10111013

10121014
it('should prevent clear() in production without force flag', () => {
10131015
const originalEnv = process.env.NODE_ENV;
1014-
process.env.NODE_ENV = 'production';
1015-
1016-
registry.registerApi({
1017-
id: 'prod_api',
1018-
name: 'Production API',
1019-
type: 'rest',
1020-
version: 'v1',
1021-
basePath: '/prod',
1022-
endpoints: [{ id: 'e1', path: '/prod', responses: [] }],
1023-
});
1024-
1025-
// Should throw error in production without force flag
1026-
expect(() => registry.clear()).toThrow(
1027-
'Cannot clear registry in production environment without force flag'
1028-
);
1029-
1030-
// API should still exist
1031-
expect(registry.getStats().totalApis).toBe(1);
1016+
try {
1017+
process.env.NODE_ENV = 'production';
10321018

1033-
process.env.NODE_ENV = originalEnv;
1019+
registry.registerApi({
1020+
id: 'prod_api',
1021+
name: 'Production API',
1022+
type: 'rest',
1023+
version: 'v1',
1024+
basePath: '/prod',
1025+
endpoints: [{ id: 'e1', path: '/prod', responses: [] }],
1026+
});
1027+
1028+
// Should throw error in production without force flag
1029+
expect(() => registry.clear()).toThrow(
1030+
'Cannot clear registry in production environment without force flag'
1031+
);
1032+
1033+
// API should still exist
1034+
expect(registry.getStats().totalApis).toBe(1);
1035+
} finally {
1036+
process.env.NODE_ENV = originalEnv;
1037+
}
10341038
});
10351039

10361040
it('should allow clear() in production with force flag', () => {
10371041
const originalEnv = process.env.NODE_ENV;
1038-
process.env.NODE_ENV = 'production';
1039-
1040-
registry.registerApi({
1041-
id: 'prod_api',
1042-
name: 'Production API',
1043-
type: 'rest',
1044-
version: 'v1',
1045-
basePath: '/prod',
1046-
endpoints: [{ id: 'e1', path: '/prod', responses: [] }],
1047-
});
1048-
1049-
expect(registry.getStats().totalApis).toBe(1);
1050-
1051-
// Should work with force flag
1052-
registry.clear({ force: true });
1053-
expect(registry.getStats().totalApis).toBe(0);
1054-
1055-
// Verify logger warned about forced clear
1056-
expect(logger.warn).toHaveBeenCalledWith(
1057-
'API registry forcefully cleared in production',
1058-
{ force: true }
1059-
);
1042+
try {
1043+
process.env.NODE_ENV = 'production';
10601044

1061-
process.env.NODE_ENV = originalEnv;
1045+
registry.registerApi({
1046+
id: 'prod_api',
1047+
name: 'Production API',
1048+
type: 'rest',
1049+
version: 'v1',
1050+
basePath: '/prod',
1051+
endpoints: [{ id: 'e1', path: '/prod', responses: [] }],
1052+
});
1053+
1054+
expect(registry.getStats().totalApis).toBe(1);
1055+
1056+
// Should work with force flag
1057+
registry.clear({ force: true });
1058+
expect(registry.getStats().totalApis).toBe(0);
1059+
1060+
// Verify logger warned about forced clear
1061+
expect(logger.warn).toHaveBeenCalledWith(
1062+
'API registry forcefully cleared in production',
1063+
{ force: true }
1064+
);
1065+
} finally {
1066+
process.env.NODE_ENV = originalEnv;
1067+
}
10621068
});
10631069

10641070
it('should clear all indices when clear() is called', () => {

packages/core/src/api-registry.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -695,10 +695,12 @@ export class ApiRegistry {
695695
* @internal
696696
*/
697697
private ensureIndexSet(map: Map<string, Set<string>>, key: string): Set<string> {
698-
if (!map.has(key)) {
699-
map.set(key, new Set());
698+
let set = map.get(key);
699+
if (!set) {
700+
set = new Set();
701+
map.set(key, set);
700702
}
701-
return map.get(key)!;
703+
return set;
702704
}
703705

704706
/**

0 commit comments

Comments
 (0)