diff options
author | 2023-08-09 07:14:30 +0200 | |
---|---|---|
committer | 2023-08-08 22:14:30 -0700 | |
commit | 40d00a961ec4aa4398e18bada520eaf5791151cc (patch) | |
tree | 6d47891914742498df7becf253575d633386919c /test/js | |
parent | 1941dbbd71c6d6730ca78b21ef2fd20f51124950 (diff) | |
download | bun-40d00a961ec4aa4398e18bada520eaf5791151cc.tar.gz bun-40d00a961ec4aa4398e18bada520eaf5791151cc.tar.zst bun-40d00a961ec4aa4398e18bada520eaf5791151cc.zip |
feat(bun/test): Implement "toSatisfy" & "toIncludeRepeated" (fwup) (#3651)
* Fix merge issues
* oop
* make codegen
* Fix build issues
---------
Co-authored-by: dave caruso <me@paperdave.net>
Diffstat (limited to 'test/js')
-rw-r--r-- | test/js/bun/test/jest-extended.test.js | 76 |
1 files changed, 74 insertions, 2 deletions
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()") |