setup-vitest.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import type { MockInstance } from 'vitest'
  2. declare module 'vitest' {
  3. interface Matchers<T = any> extends CustomMatchers<T> {}
  4. }
  5. interface CustomMatchers<R = unknown> {
  6. toHaveBeenWarned(): R
  7. toHaveBeenWarnedLast(): R
  8. toHaveBeenWarnedTimes(n: number): R
  9. }
  10. vi.stubGlobal('MathMLElement', class MathMLElement {})
  11. expect.extend({
  12. toHaveBeenWarned(received: string) {
  13. const passed = warn.mock.calls.some(args => args[0].includes(received))
  14. if (passed) {
  15. asserted.add(received)
  16. return {
  17. pass: true,
  18. message: () => `expected "${received}" not to have been warned.`,
  19. }
  20. } else {
  21. const msgs = warn.mock.calls.map(args => args[0]).join('\n - ')
  22. return {
  23. pass: false,
  24. message: () =>
  25. `expected "${received}" to have been warned` +
  26. (msgs.length
  27. ? `.\n\nActual messages:\n\n - ${msgs}`
  28. : ` but no warning was recorded.`),
  29. }
  30. }
  31. },
  32. toHaveBeenWarnedLast(received: string) {
  33. const passed =
  34. warn.mock.calls[warn.mock.calls.length - 1][0].includes(received)
  35. if (passed) {
  36. asserted.add(received)
  37. return {
  38. pass: true,
  39. message: () => `expected "${received}" not to have been warned last.`,
  40. }
  41. } else {
  42. const msgs = warn.mock.calls.map(args => args[0]).join('\n - ')
  43. return {
  44. pass: false,
  45. message: () =>
  46. `expected "${received}" to have been warned last.\n\nActual messages:\n\n - ${msgs}`,
  47. }
  48. }
  49. },
  50. toHaveBeenWarnedTimes(received: string, n: number) {
  51. let found = 0
  52. warn.mock.calls.forEach(args => {
  53. if (args[0].includes(received)) {
  54. found++
  55. }
  56. })
  57. if (found === n) {
  58. asserted.add(received)
  59. return {
  60. pass: true,
  61. message: () => `expected "${received}" to have been warned ${n} times.`,
  62. }
  63. } else {
  64. return {
  65. pass: false,
  66. message: () =>
  67. `expected "${received}" to have been warned ${n} times but got ${found}.`,
  68. }
  69. }
  70. },
  71. })
  72. let warn: MockInstance
  73. const asserted: Set<string> = new Set()
  74. beforeEach(() => {
  75. asserted.clear()
  76. warn = vi.spyOn(console, 'warn')
  77. warn.mockImplementation(() => {})
  78. })
  79. afterEach(() => {
  80. const assertedArray = Array.from(asserted)
  81. const nonAssertedWarnings = warn.mock.calls
  82. .map(args => args[0])
  83. .filter(received => {
  84. return !assertedArray.some(assertedMsg => {
  85. return received.includes(assertedMsg)
  86. })
  87. })
  88. warn.mockRestore()
  89. if (nonAssertedWarnings.length) {
  90. throw new Error(
  91. `test case threw unexpected warnings:\n - ${nonAssertedWarnings.join(
  92. '\n - ',
  93. )}`,
  94. )
  95. }
  96. })