smartexpect/test/test.keys.ts

51 lines
1.7 KiB
TypeScript
Raw Normal View History

import { tap, expect as tExpect } from '@push.rocks/tapbundle';
import * as smartexpect from '../dist_ts/index.js';
// Positive: toHaveKeys should match both own and inherited properties
tap.test('toHaveKeys includes own and inherited properties', async () => {
class Parent { a = 1; }
class Child extends Parent { b = 2; }
const child = new Child();
smartexpect.expect(child).object.toHaveKeys(['a', 'b']);
});
// Positive: toHaveOwnKeys should match only own properties
tap.test('toHaveOwnKeys includes only own properties', async () => {
class Parent { a = 1; }
class Child extends Parent { b = 2; }
const child = new Child();
smartexpect.expect(child).object.toHaveOwnKeys(['b']);
});
// Negative: toHaveKeys should fail if any key is missing
tap.test('toHaveKeys fails when keys are missing', async () => {
const obj = { a: 1 };
try {
smartexpect.expect(obj).object.toHaveKeys(['a', 'b']);
throw new Error('Assertion did not throw');
} catch (err: any) {
tExpect(err.message.includes('Expected object to have keys')).toBeTrue();
}
});
// Negative: toHaveOwnKeys should fail when own keys are missing (inherited keys not counted)
tap.test('toHaveOwnKeys fails when own keys are missing', async () => {
// 'a' is inherited via the prototype, not an own property
class Parent {}
Parent.prototype.a = 1;
class Child extends Parent {
constructor() {
super();
this.b = 2;
}
}
const child = new Child();
try {
smartexpect.expect(child).object.toHaveOwnKeys(['a', 'b']);
throw new Error('Assertion did not throw');
} catch (err: any) {
tExpect(err.message.includes('Expected object to have own keys')).toBeTrue();
}
});
export default tap.start();