smartexpect/test/test.expectAny.ts

38 lines
1.3 KiB
TypeScript
Raw Normal View History

import { tap } from '@push.rocks/tapbundle';
import * as smartexpect from '../dist_ts/index.js';
tap.test('expect.any and expect.anything basic usage', async () => {
const obj = { a: 1, b: 'two', d: new Date() };
// Using expect.any to match types
smartexpect.expect(obj).object.toMatchObject({
a: smartexpect.expect.any(Number),
b: smartexpect.expect.any(String),
d: smartexpect.expect.any(Date),
});
// Using expect.anything to match any defined value
smartexpect.expect(obj).object.toMatchObject({
a: smartexpect.expect.anything(),
b: smartexpect.expect.anything(),
d: smartexpect.expect.anything(),
});
});
tap.test('expect.any mismatch and anything null/undefined rejection', async () => {
const obj = { a: 1, b: null };
// Mismatch for expect.any
try {
smartexpect.expect(obj).object.toMatchObject({ a: smartexpect.expect.any(String) });
throw new Error('Expected mismatch for expect.any did not throw');
} catch (err) {
// success: thrown on mismatch
}
// anything should reject null or undefined
try {
smartexpect.expect(obj).object.toMatchObject({ b: smartexpect.expect.anything() });
throw new Error('Expected anything() to reject null or undefined');
} catch (err) {
// success: thrown on null
}
});
export default tap.start();