setupTests.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import * as E from "fp-ts/Either"
  2. import { expect } from "vitest"
  3. expect.extend({
  4. toBeLeft(received, expected) {
  5. const { isNot } = this
  6. return {
  7. pass:
  8. E.isLeft(received) &&
  9. this.equals(received.left, expected, undefined, false),
  10. message: () =>
  11. `Expected received value ${isNot ? "not " : ""}to be a left`,
  12. }
  13. },
  14. toBeRight(received) {
  15. const { isNot } = this
  16. return {
  17. pass: E.isRight(received),
  18. message: () =>
  19. `Expected received value ${isNot ? "not " : ""}to be a right`,
  20. }
  21. },
  22. toEqualLeft(received, expected) {
  23. const { isNot } = this
  24. const isLeft = E.isLeft(received)
  25. const leftEquals = E.isLeft(received)
  26. ? this.equals(received.left, expected)
  27. : false
  28. return {
  29. pass: isLeft && leftEquals,
  30. message: () => {
  31. if (!isLeft) {
  32. return `Expected received value ${isNot ? "not " : ""}to be a left`
  33. } else if (!leftEquals) {
  34. return `Expected received left value ${
  35. isNot ? "not" : ""
  36. } to equal expected value`
  37. }
  38. throw new Error("Invalid state on `toEqualLeft` matcher")
  39. },
  40. }
  41. },
  42. toSubsetEqualRight(received, expected) {
  43. const { isNot } = this
  44. const isRight = E.isRight(received)
  45. const rightSubsetEquals = E.isRight(received)
  46. ? !!this.utils.subsetEquality(received.right, expected)
  47. : false
  48. return {
  49. pass: isRight && rightSubsetEquals,
  50. message: () => {
  51. if (!isRight) {
  52. return `Expected received value ${isNot ? "not " : ""}to be a left`
  53. } else if (!rightSubsetEquals) {
  54. return `Expected received left value to ${
  55. isNot ? "not " : ""
  56. }equal expected value`
  57. }
  58. throw new Error("Invalid state on `toSubsetEqualRight` matcher")
  59. },
  60. }
  61. },
  62. })