From 40d00a961ec4aa4398e18bada520eaf5791151cc Mon Sep 17 00:00:00 2001 From: "Tiramify (A.K. Daniel)" <94789999+TiranexDev@users.noreply.github.com> Date: Wed, 9 Aug 2023 07:14:30 +0200 Subject: feat(bun/test): Implement "toSatisfy" & "toIncludeRepeated" (fwup) (#3651) * Fix merge issues * oop * make codegen * Fix build issues --------- Co-authored-by: dave caruso --- test/js/bun/test/jest-extended.test.js | 76 +++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 2 deletions(-) (limited to 'test/js') diff --git a/test/js/bun/test/jest-extended.test.js b/test/js/bun/test/jest-extended.test.js index 2cdec75fa..816863218 100644 --- a/test/js/bun/test/jest-extended.test.js +++ b/test/js/bun/test/jest-extended.test.js @@ -110,7 +110,37 @@ describe("jest-extended", () => { expect({}).not.toBeNil(); }); - // test('toSatisfy()') + test("toSatisfy()", () => { + // Arrow functions + const isOdd = value => value % 2 === 1; + const hasLetterH = (value) => value.includes("H"); + + expect(1).toSatisfy(isOdd); + expect("Hello").toSatisfy(hasLetterH); + + // Function expressions + function hasBunInAnArray(value) { return value.includes("bun"); } + + expect(["bun", "cheese", "patty"]).toSatisfy(hasBunInAnArray); + expect(["cheese", "patty"]).not.toSatisfy(hasBunInAnArray); + + // Inline functions + expect([]).toSatisfy((value) => value.length === 0); + expect([]).not.toSatisfy(value => value.length > 0); + + // Some other types + const fooIsBar = (value) => value?.foo === "bar"; + + expect({ foo: "bar" }).toSatisfy(fooIsBar); + expect({ foo: "bun" }).not.toSatisfy(fooIsBar); + expect({ bar: "foo" }).not.toSatisfy(fooIsBar); + + // Test errors + // @ts-expect-error + expect(() => expect(1).toSatisfy(() => new Error('Bun!'))).toThrow('predicate threw an exception'); + // @ts-expect-error + expect(() => expect(1).not.toSatisfy(() => new Error('Bun!'))).toThrow('predicate threw an exception'); + }); // Array @@ -509,7 +539,49 @@ describe("jest-extended", () => { expect("bob").not.toInclude("alice"); }); - // test("toIncludeRepeated()") + test("toIncludeRepeated()", () => { + // 0 + expect("a").toIncludeRepeated("b", 0) + expect("b").not.toIncludeRepeated("b", 0); + + // 1 + expect("abc").toIncludeRepeated("a", 1); + expect("abc").not.toIncludeRepeated("d", 1); + + // Any other number + expect("abc abc abc").toIncludeRepeated("abc", 1); + expect("abc abc abc").toIncludeRepeated("abc", 2); + expect("abc abc abc").toIncludeRepeated("abc", 3); + expect("abc abc abc").not.toIncludeRepeated("abc", 4); + + // Emojis/Unicode + expect("😘πŸ₯³πŸ˜€πŸ˜˜πŸ₯³").toIncludeRepeated("😘", 1); + expect("😘πŸ₯³πŸ˜€πŸ˜˜πŸ₯³").toIncludeRepeated("πŸ₯³", 2); + expect("😘πŸ₯³πŸ˜€πŸ˜˜πŸ₯³").not.toIncludeRepeated("😘", 3); + expect("😘πŸ₯³πŸ˜€πŸ˜˜πŸ₯³").not.toIncludeRepeated("πŸ˜Άβ€πŸŒ«οΈ", 1); + + // Empty string + expect("").not.toIncludeRepeated("a", 1); + + // if toIncludeRepeated() is called with a empty string, it should throw an error or else it segfaults + expect(() => expect("a").not.toIncludeRepeated("", 1)).toThrow() + + // Just to make sure it doesn't throw an error + expect("").not.toIncludeRepeated("a", 1) + expect("").not.toIncludeRepeated("πŸ˜Άβ€πŸŒ«οΈ", 1) + + // Expect them to throw an error + const tstErr = (y) => { return expect("").toIncludeRepeated("a", y) }; + + expect(() => tstErr(1.23)).toThrow(); + expect(() => tstErr(Infinity)).toThrow(); + expect(() => tstErr(NaN)).toThrow(); + expect(() => tstErr(-0)).toThrow(); // -0 and below (-1, -2, ...) + expect(() => tstErr(null)).toThrow(); + expect(() => tstErr(undefined)).toThrow(); + expect(() => tstErr({})).toThrow(); + }) ; + // test("toIncludeMultiple()") // test("toEqualIgnoringWhitespace()") -- cgit v1.2.3