Skip to content

Commit 798b7fb

Browse files
committed
test: improve unit tests
1 parent 45fdaf6 commit 798b7fb

File tree

3 files changed

+15
-34
lines changed

3 files changed

+15
-34
lines changed

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
preset: 'ts-jest',
1313
testEnvironment: 'node',
1414
testMatch: ['<rootDir>/test/**/*.{spec,test}.{ts,tsx}'],
15-
setupFiles: ['./test/jest.setup.ts'],
15+
setupFilesAfterEnv: ['<rootDir>/test/jest.setup.ts'],
1616
collectCoverage: true,
1717
coverageThreshold: {
1818
global: {

src/enum-item.ts

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,7 @@ export class EnumItemClass<
6666
}
6767
return content;
6868
}
69-
private _isTesting() {
70-
let jestWorkerId: string | undefined = undefined;
71-
try {
72-
jestWorkerId = process.env.JEST_WORKER_ID;
73-
} catch (e) {
74-
// safely ignore
75-
}
76-
/* istanbul ignore next */
77-
if (jestWorkerId != null) {
78-
return !!jestWorkerId;
79-
} else {
80-
/* istanbul ignore next */
81-
return false;
82-
}
83-
}
8469
private _readonlyPropMsg(name: string) {
85-
/* istanbul ignore next */
8670
return `Cannot modify property "${name}" on EnumItem. EnumItem instances are readonly and should not be mutated.`;
8771
}
8872
private _localizedProxy = new Proxy(this, {
@@ -95,31 +79,19 @@ export class EnumItemClass<
9579
},
9680
// Not allowed to edit
9781
set: (_, prop) => {
98-
/* istanbul ignore if */
99-
if (!this._isTesting()) {
100-
console.warn(this._readonlyPropMsg(String(prop)));
101-
}
82+
console.warn(this._readonlyPropMsg(String(prop)));
10283
return true;
10384
},
10485
defineProperty: (_, prop) => {
105-
/* istanbul ignore if */
106-
if (!this._isTesting()) {
107-
console.warn(this._readonlyPropMsg(String(prop)));
108-
}
86+
console.warn(this._readonlyPropMsg(String(prop)));
10987
return true;
11088
},
11189
deleteProperty: (_, prop) => {
112-
/* istanbul ignore if */
113-
if (!this._isTesting()) {
114-
console.warn(this._readonlyPropMsg(String(prop)));
115-
}
90+
console.warn(this._readonlyPropMsg(String(prop)));
11691
return true;
11792
},
11893
setPrototypeOf: () => {
119-
/* istanbul ignore if */
120-
if (!this._isTesting()) {
121-
console.warn('Cannot change prototype of EnumItem. EnumItem instances are immutable.');
122-
}
94+
console.warn('Cannot change prototype of EnumItem. EnumItem instances are immutable.');
12395
return true;
12496
},
12597
});

test/jest.setup.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
import { defaultLocalize, Enum } from '@enum-plus';
22
import { getLocales, setLang } from './data/week-config';
33

4-
setLang('en-US', Enum, getLocales, defaultLocalize);
4+
beforeAll(() => {
5+
setLang('en-US', Enum, getLocales, defaultLocalize);
6+
jest.spyOn(console, 'warn').mockImplementation(() => {
7+
// Mock console.warn to suppress warnings during tests
8+
});
9+
});
10+
11+
afterAll(() => {
12+
jest.clearAllMocks();
13+
});

0 commit comments

Comments
 (0)