diff --git a/tests/expect/matchers.snapshots.js b/tests/expect/matchers.snapshots.js index 0f8a5a5030..5867280a48 100644 --- a/tests/expect/matchers.snapshots.js +++ b/tests/expect/matchers.snapshots.js @@ -3612,3 +3612,108 @@ module.exports["toMatchObject() does not match properties up in the prototype ch - "ref": [Circular], }`; +module.exports[".toBeOneOf() fails when the value is not in the expected array: expect(4).toBeOneOf([1, 2, 3])"] = `expect(received).toBeOneOf(expected) + +Expected: to be one of [1, 2, 3] +Received: 4`; + +module.exports[".toBeOneOf() fails when the value is not in the expected array: expect(\"d\").toBeOneOf([\"a\", \"b\", \"c\"])"] = `expect(received).toBeOneOf(expected) + +Expected: to be one of ["a", "b", "c"] +Received: "d"`; + +module.exports[".toBeOneOf() fails when the value is not in the expected array: expect(false).toBeOneOf([true])"] = `expect(received).toBeOneOf(expected) + +Expected: to be one of [true] +Received: false`; + +module.exports[".toBeOneOf() fails when the value is not in the expected array: expect(null).toBeOneOf([undefined])"] = `expect(received).toBeOneOf(expected) + +Expected: to be one of [undefined] +Received: null`; + +module.exports[".toBeOneOf() fails when the value is not in the expected array: expect(undefined).toBeOneOf([null])"] = `expect(received).toBeOneOf(expected) + +Expected: to be one of [null] +Received: undefined`; + +module.exports[".toBeOneOf() fails when the value is not in the expected array: expect(NaN).toBeOneOf([1, 2])"] = `expect(received).toBeOneOf(expected) + +Expected: to be one of [1, 2] +Received: NaN`; + +module.exports[".toBeOneOf() fails when the value is not in the expected array: expect(3n).toBeOneOf([1n, 2n])"] = `expect(received).toBeOneOf(expected) + +Expected: to be one of [1n, 2n] +Received: 3n`; + +module.exports[".toBeOneOf() fails when the value is not in the expected array: expect([1, 2]).toBeOneOf([[3, 4], [5, 6]])"] = `expect(received).toBeOneOf(expected) + +Expected: to be one of [[3, 4], [5, 6]] +Received: [1, 2]`; + +module.exports[".toBeOneOf() fails when the value is not in the expected array: expect({\"a\": 1}).toBeOneOf([{\"b\": 2}, {\"c\": 3}])"] = `expect(received).toBeOneOf(expected) + +Expected: to be one of [{"b": 2}, {"c": 3}] +Received: {"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}}}])"] = `expect(received).toBeOneOf(expected) + +Expected: to be one of [{"a": {"b": {"c": 2}}}] +Received: {"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])"] = `expect(received).not.toBeOneOf(expected) + +Expected: not to be one of [1, 2, 3] +Received: 2`; + +module.exports[".toBeOneOf() fails when using .not and value is in the expected array: expect(\"b\").not.toBeOneOf([\"a\", \"b\", \"c\"])"] = `expect(received).not.toBeOneOf(expected) + +Expected: not to be one of ["a", "b", "c"] +Received: "b"`; + +module.exports[".toBeOneOf() fails when using .not and value is in the expected array: expect(true).not.toBeOneOf([false, true])"] = `expect(received).not.toBeOneOf(expected) + +Expected: not to be one of [false, true] +Received: true`; + +module.exports[".toBeOneOf() fails when using .not and value is in the expected array: expect(null).not.toBeOneOf([undefined, null])"] = `expect(received).not.toBeOneOf(expected) + +Expected: not to be one of [undefined, null] +Received: null`; + +module.exports[".toBeOneOf() fails when using .not and value is in the expected array: expect(undefined).not.toBeOneOf([undefined, null])"] = `expect(received).not.toBeOneOf(expected) + +Expected: not to be one of [undefined, null] +Received: undefined`; + +module.exports[".toBeOneOf() fails when using .not and value is in the expected array: expect(NaN).not.toBeOneOf([NaN, 1, 2])"] = `expect(received).not.toBeOneOf(expected) + +Expected: not to be one of [NaN, 1, 2] +Received: NaN`; + +module.exports[".toBeOneOf() fails when using .not and value is in the expected array: expect(1n).not.toBeOneOf([1n, 2n])"] = `expect(received).not.toBeOneOf(expected) + +Expected: not to be one of [1n, 2n] +Received: 1n`; + +module.exports[".toBeOneOf() fails when using .not and value is in the expected array: expect([1, 2]).not.toBeOneOf([[1, 2], [3, 4]])"] = `expect(received).not.toBeOneOf(expected) + +Expected: not to be one of [[1, 2], [3, 4]] +Received: [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}])"] = `expect(received).not.toBeOneOf(expected) + +Expected: not to be one of [{"a": 1}, {"b": 2}] +Received: {"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}}}])"] = `expect(received).not.toBeOneOf(expected) + +Expected: not to be one of [{"a": {"b": {"c": 1}}}, {"a": {"b": {"c": 2}}}] +Received: {"a": {"b": {"c": 1}}}`; + +module.exports[".toBeOneOf() fails when value does not match any asymmetric matchers in expected array"] = `expect(received).toBeOneOf(expected) + +Expected: to be one of [{"a": 2}, ObjectContaining {"c": 3}] +Received: {"a": 1, "b": 2}`; + diff --git a/tests/expect/matchers.test.ts b/tests/expect/matchers.test.ts index 8824039bab..b01ba91ba4 100644 --- a/tests/expect/matchers.test.ts +++ b/tests/expect/matchers.test.ts @@ -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', + }), + ); + } + }); +}); diff --git a/tests/playwright-test/expect.spec.ts b/tests/playwright-test/expect.spec.ts index c35cdd7c09..54711278d5 100644 --- a/tests/playwright-test/expect.spec.ts +++ b/tests/playwright-test/expect.spec.ts @@ -141,6 +141,7 @@ test('should compile generic matchers', async ({ runTSC }) => { expect(42).toBeLessThanOrEqual(1); expect(42n).toBeLessThanOrEqual(1n); expect(42).toBeNull(); + expect(42).toBeOneOf([1, 2, 42]); expect(42).toBeTruthy(); expect(42).toBeUndefined(); expect(42).toBeNaN();