Skip to content

Commit

Permalink
test: add unit tests for setReadonlyConstantToGlobalThis
Browse files Browse the repository at this point in the history
  • Loading branch information
kiki-kanri committed Oct 16, 2024
1 parent ca225a5 commit aafe978
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/object.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { setReadonlyConstantToGlobalThis } from '../src/object';

describe('setReadonlyConstantToGlobalThis', () => {
const constantValue = 'testValue';
it('should define a readonly constant on globalThis', () => {
const constantName = Math.random().toString(36).substring(2, 15);
setReadonlyConstantToGlobalThis(constantName, constantValue);
expect((globalThis as any)[constantName]).toBe(constantValue);
expect(() => ((globalThis as any)[constantName] = 'newValue')).toThrow(TypeError);
expect((globalThis as any)[constantName]).toBe(constantValue);
});

it('should not be configurable', () => {
const constantName = Math.random().toString(36).substring(2, 15);
setReadonlyConstantToGlobalThis(constantName, constantValue);
const deleteResult = delete (globalThis as any)[constantName];
expect(deleteResult).toBe(false);
expect((globalThis as any)[constantName]).toBe(constantValue);
});

it('should allow passing additional attributes', () => {
const constantName = Math.random().toString(36).substring(2, 15);
setReadonlyConstantToGlobalThis(constantName, constantValue, { enumerable: true });
expect(Object.getOwnPropertyDescriptor(globalThis, constantName)?.enumerable).toBe(true);
});

it('should not be writable', () => {
const constantName = Math.random().toString(36).substring(2, 15);
setReadonlyConstantToGlobalThis(constantName, constantValue);
expect(Object.getOwnPropertyDescriptor(globalThis, constantName)?.writable).toBe(false);
});
});

0 comments on commit aafe978

Please sign in to comment.