test: add regression tests

This commit is contained in:
Pengoose 2024-12-04 03:40:34 +09:00
parent ba140b1b83
commit eb395dab5b
3 changed files with 194 additions and 0 deletions

View file

@ -3612,3 +3612,108 @@ module.exports["toMatchObject() does not match properties up in the prototype ch
<g>- "ref": [Circular],</> <g>- "ref": [Circular],</>
<d> }</>`; <d> }</>`;
module.exports[".toBeOneOf() fails when the value is not in the expected array: expect(4).toBeOneOf([1, 2, 3])"] = `<d>expect(</><r>received</><d>).</>toBeOneOf<d>(</><g>expected</><d>)</>
Expected: to be one of <g>[1, 2, 3]</>
Received: <r>4</>`;
module.exports[".toBeOneOf() fails when the value is not in the expected array: expect(\"d\").toBeOneOf([\"a\", \"b\", \"c\"])"] = `<d>expect(</><r>received</><d>).</>toBeOneOf<d>(</><g>expected</><d>)</>
Expected: to be one of <g>["a", "b", "c"]</>
Received: <r>"d"</>`;
module.exports[".toBeOneOf() fails when the value is not in the expected array: expect(false).toBeOneOf([true])"] = `<d>expect(</><r>received</><d>).</>toBeOneOf<d>(</><g>expected</><d>)</>
Expected: to be one of <g>[true]</>
Received: <r>false</>`;
module.exports[".toBeOneOf() fails when the value is not in the expected array: expect(null).toBeOneOf([undefined])"] = `<d>expect(</><r>received</><d>).</>toBeOneOf<d>(</><g>expected</><d>)</>
Expected: to be one of <g>[undefined]</>
Received: <r>null</>`;
module.exports[".toBeOneOf() fails when the value is not in the expected array: expect(undefined).toBeOneOf([null])"] = `<d>expect(</><r>received</><d>).</>toBeOneOf<d>(</><g>expected</><d>)</>
Expected: to be one of <g>[null]</>
Received: <r>undefined</>`;
module.exports[".toBeOneOf() fails when the value is not in the expected array: expect(NaN).toBeOneOf([1, 2])"] = `<d>expect(</><r>received</><d>).</>toBeOneOf<d>(</><g>expected</><d>)</>
Expected: to be one of <g>[1, 2]</>
Received: <r>NaN</>`;
module.exports[".toBeOneOf() fails when the value is not in the expected array: expect(3n).toBeOneOf([1n, 2n])"] = `<d>expect(</><r>received</><d>).</>toBeOneOf<d>(</><g>expected</><d>)</>
Expected: to be one of <g>[1n, 2n]</>
Received: <r>3n</>`;
module.exports[".toBeOneOf() fails when the value is not in the expected array: expect([1, 2]).toBeOneOf([[3, 4], [5, 6]])"] = `<d>expect(</><r>received</><d>).</>toBeOneOf<d>(</><g>expected</><d>)</>
Expected: to be one of <g>[[3, 4], [5, 6]]</>
Received: <r>[1, 2]</>`;
module.exports[".toBeOneOf() fails when the value is not in the expected array: expect({\"a\": 1}).toBeOneOf([{\"b\": 2}, {\"c\": 3}])"] = `<d>expect(</><r>received</><d>).</>toBeOneOf<d>(</><g>expected</><d>)</>
Expected: to be one of <g>[{"b": 2}, {"c": 3}]</>
Received: <r>{"a": 1}</>`;
module.exports[".toBeOneOf() fails when the value is not in the expected array: expect({\"a\": {\"b\": {\"c\": 1}}}).toBeOneOf([{\"a\": {\"b\": {\"c\": 2}}}])"] = `<d>expect(</><r>received</><d>).</>toBeOneOf<d>(</><g>expected</><d>)</>
Expected: to be one of <g>[{"a": {"b": {"c": 2}}}]</>
Received: <r>{"a": {"b": {"c": 1}}}</>`;
module.exports[".toBeOneOf() fails when using .not and value is in the expected array: expect(2).not.toBeOneOf([1, 2, 3])"] = `<d>expect(</><r>received</><d>).</>not<d>.</>toBeOneOf<d>(</><g>expected</><d>)</>
Expected: not to be one of <g>[1, 2, 3]</>
Received: <r>2</>`;
module.exports[".toBeOneOf() fails when using .not and value is in the expected array: expect(\"b\").not.toBeOneOf([\"a\", \"b\", \"c\"])"] = `<d>expect(</><r>received</><d>).</>not<d>.</>toBeOneOf<d>(</><g>expected</><d>)</>
Expected: not to be one of <g>["a", "b", "c"]</>
Received: <r>"b"</>`;
module.exports[".toBeOneOf() fails when using .not and value is in the expected array: expect(true).not.toBeOneOf([false, true])"] = `<d>expect(</><r>received</><d>).</>not<d>.</>toBeOneOf<d>(</><g>expected</><d>)</>
Expected: not to be one of <g>[false, true]</>
Received: <r>true</>`;
module.exports[".toBeOneOf() fails when using .not and value is in the expected array: expect(null).not.toBeOneOf([undefined, null])"] = `<d>expect(</><r>received</><d>).</>not<d>.</>toBeOneOf<d>(</><g>expected</><d>)</>
Expected: not to be one of <g>[undefined, null]</>
Received: <r>null</>`;
module.exports[".toBeOneOf() fails when using .not and value is in the expected array: expect(undefined).not.toBeOneOf([undefined, null])"] = `<d>expect(</><r>received</><d>).</>not<d>.</>toBeOneOf<d>(</><g>expected</><d>)</>
Expected: not to be one of <g>[undefined, null]</>
Received: <r>undefined</>`;
module.exports[".toBeOneOf() fails when using .not and value is in the expected array: expect(NaN).not.toBeOneOf([NaN, 1, 2])"] = `<d>expect(</><r>received</><d>).</>not<d>.</>toBeOneOf<d>(</><g>expected</><d>)</>
Expected: not to be one of <g>[NaN, 1, 2]</>
Received: <r>NaN</>`;
module.exports[".toBeOneOf() fails when using .not and value is in the expected array: expect(1n).not.toBeOneOf([1n, 2n])"] = `<d>expect(</><r>received</><d>).</>not<d>.</>toBeOneOf<d>(</><g>expected</><d>)</>
Expected: not to be one of <g>[1n, 2n]</>
Received: <r>1n</>`;
module.exports[".toBeOneOf() fails when using .not and value is in the expected array: expect([1, 2]).not.toBeOneOf([[1, 2], [3, 4]])"] = `<d>expect(</><r>received</><d>).</>not<d>.</>toBeOneOf<d>(</><g>expected</><d>)</>
Expected: not to be one of <g>[[1, 2], [3, 4]]</>
Received: <r>[1, 2]</>`;
module.exports[".toBeOneOf() fails when using .not and value is in the expected array: expect({\"a\": 1}).not.toBeOneOf([{\"a\": 1}, {\"b\": 2}])"] = `<d>expect(</><r>received</><d>).</>not<d>.</>toBeOneOf<d>(</><g>expected</><d>)</>
Expected: not to be one of <g>[{"a": 1}, {"b": 2}]</>
Received: <r>{"a": 1}</>`;
module.exports[".toBeOneOf() fails when using .not and value is in the expected array: expect({\"a\": {\"b\": {\"c\": 1}}}).not.toBeOneOf([{\"a\": {\"b\": {\"c\": 1}}}, {\"a\": {\"b\": {\"c\": 2}}}])"] = `<d>expect(</><r>received</><d>).</>not<d>.</>toBeOneOf<d>(</><g>expected</><d>)</>
Expected: not to be one of <g>[{"a": {"b": {"c": 1}}}, {"a": {"b": {"c": 2}}}]</>
Received: <r>{"a": {"b": {"c": 1}}}</>`;
module.exports[".toBeOneOf() fails when value does not match any asymmetric matchers in expected array"] = `<d>expect(</><r>received</><d>).</>toBeOneOf<d>(</><g>expected</><d>)</>
Expected: to be one of <g>[{"a": 2}, ObjectContaining {"c": 3}]</>
Received: <r>{"a": 1, "b": 2}</>`;

View file

@ -2336,3 +2336,91 @@ test.describe('toMatchObject()', () => {
}); });
}); });
}); });
test.describe('.toBeOneOf()', () => {
const matchingCases = [
[2, [1, 2, 3]],
['b', ['a', 'b', 'c']],
[true, [false, true]],
[null, [undefined, null]],
[undefined, [undefined, null]],
[NaN, [NaN, 1, 2]],
[BigInt(1), [BigInt(1), BigInt(2)]],
[[1, 2], [[1, 2], [3, 4]]],
[{ a: 1 }, [{ a: 1 }, { b: 2 }]],
[{ a: { b: { c: 1 } } }, [{ a: { b: { c: 1 } } }, { a: { b: { c: 2 } } }]],
];
const nonMatchingCases = [
[4, [1, 2, 3]],
['d', ['a', 'b', 'c']],
[false, [true]],
[null, [undefined]],
[undefined, [null]],
[NaN, [1, 2]],
[BigInt(3), [BigInt(1), BigInt(2)]],
[[1, 2], [[3, 4], [5, 6]]],
[{ a: 1 }, [{ b: 2 }, { c: 3 }]],
[{ a: { b: { c: 1 } } }, [{ a: { b: { c: 2 } } }]],
];
matchingCases.forEach(([value, array]: [any, any]) => {
test(`passes when the value is in the expected array: expect(${stringify(value)}).toBeOneOf(${stringify(array)})`, () => {
expectUnderTest(value).toBeOneOf(array);
});
});
nonMatchingCases.forEach(([value, array]: [any, any]) => {
test(`fails when the value is not in the expected array: expect(${stringify(value)}).toBeOneOf(${stringify(array)})`, () => {
expect(() => expectUnderTest(value).toBeOneOf(array)).toThrowErrorMatchingSnapshot();
});
});
nonMatchingCases.forEach(([value, array]: [any, any]) => {
test(`passes when using .not and value is not in the expected array: expect(${stringify(value)}).not.toBeOneOf(${stringify(array)})`, () => {
expectUnderTest(value).not.toBeOneOf(array);
});
});
matchingCases.forEach(([value, array]: [any, any]) => {
test(`fails when using .not and value is in the expected array: expect(${stringify(value)}).not.toBeOneOf(${stringify(array)})`, () => {
expect(() => expectUnderTest(value).not.toBeOneOf(array)).toThrowErrorMatchingSnapshot();
});
});
test('supports asymmetric matchers within the expected array', () => {
expectUnderTest({ a: 1, b: 2 }).toBeOneOf([
{ a: 1 },
expect.objectContaining({ b: 2 }),
]);
expectUnderTest('hello world').toBeOneOf([
expect.stringContaining('world'),
'hello',
]);
});
test('fails when value does not match any asymmetric matchers in expected array', () => {
expect(() =>
expectUnderTest({ a: 1, b: 2 }).toBeOneOf([
{ a: 2 },
expect.objectContaining({ c: 3 }),
]),
).toThrowErrorMatchingSnapshot();
});
test('assertion error matcherResult property contains matcher name, expected and actual values', () => {
const actual = 5;
const expected = [1, 2, 3];
try {
expectUnderTest(actual).toBeOneOf(expected);
} catch (error) {
expect(error.matcherResult).toEqual(
expect.objectContaining({
actual,
expected,
name: 'toBeOneOf',
}),
);
}
});
});

View file

@ -141,6 +141,7 @@ test('should compile generic matchers', async ({ runTSC }) => {
expect(42).toBeLessThanOrEqual(1); expect(42).toBeLessThanOrEqual(1);
expect(42n).toBeLessThanOrEqual(1n); expect(42n).toBeLessThanOrEqual(1n);
expect(42).toBeNull(); expect(42).toBeNull();
expect(42).toBeOneOf([1, 2, 42]);
expect(42).toBeTruthy(); expect(42).toBeTruthy();
expect(42).toBeUndefined(); expect(42).toBeUndefined();
expect(42).toBeNaN(); expect(42).toBeNaN();