Skip to content

Commit

Permalink
chore: vitest iterable equality temporary workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
sukovanej committed Apr 26, 2024
1 parent 8492f5b commit 924ba10
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/effect/test/Either.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,13 @@ describe("Either", () => {
Either.left("d")
)
})

it("vitest equality", () => {
expect(Either.right(2)).not.toStrictEqual(Either.right(1))
expect(Either.right(1)).toStrictEqual(Either.right(1))
expect(Either.left(2)).not.toStrictEqual(Either.left(1))
expect(Either.left(1)).toStrictEqual(Either.left(1))
expect(Either.left(1)).not.toStrictEqual(Either.right(1))
expect(Either.left(1)).not.toStrictEqual(Either.right(2))
})
})
7 changes: 7 additions & 0 deletions packages/effect/test/Option.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,4 +496,11 @@ describe("Option", () => {
expect(f(_.none(), _.some(2))).toStrictEqual(_.none())
expect(f(_.some(1), _.some(2))).toStrictEqual(_.some(3))
})

it("vitest equality", () => {
expect(_.some(2)).not.toStrictEqual(_.some(1))
expect(_.none()).not.toStrictEqual(_.some(1))
expect(_.some(2)).toStrictEqual(_.some(2))
expect(_.none()).toStrictEqual(_.none())
})
})
32 changes: 32 additions & 0 deletions setupTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { equals } from "@vitest/expect"

// workaround for https://github.com/vitest-dev/vitest/issues/5620

const testers = (globalThis as any)[Symbol.for("$$jest-matchers-object")]
.customEqualityTesters

function hasIterator(object: any) {
return !!(object !== null && object[Symbol.iterator])
}

testers.unshift((a: unknown, b: unknown) => {
if (
typeof a !== "object" ||
typeof b !== "object" ||
Array.isArray(a) ||
Array.isArray(b) ||
!hasIterator(a) ||
!hasIterator(b) ||
a === null ||
b === null
) {
return undefined
}

const aEntries = Object.entries(a)
const bEntries = Object.entries(b)

if (!equals(aEntries, bEntries)) return false

return undefined
})
1 change: 1 addition & 0 deletions vitest.shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const config: UserConfig = {
exclude: ["bun:sqlite"]
},
test: {
setupFiles: [path.join(__dirname, "setupTests.ts")],
fakeTimers: {
toFake: undefined
},
Expand Down

0 comments on commit 924ba10

Please sign in to comment.