setup.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { afterEach, beforeEach, chai, vi } from 'vitest'
  2. import sinonChai from 'sinon-chai'
  3. import chaiAsPromised from 'chai-as-promised'
  4. // Setup chai
  5. chai.should()
  6. // Workaround: vitest's built-in chai plugins register spy-related properties
  7. // (e.g. callCount) as getter-only via addChainableMethod. sinon-chai then tries
  8. // to overwrite them via addMethod (plain property assignment), which fails.
  9. // Pre-delete the conflicting getter-only properties so the assignment succeeds.
  10. const sinonChaiMethodProps = [
  11. 'callCount',
  12. 'calledBefore',
  13. 'calledAfter',
  14. 'calledImmediatelyBefore',
  15. 'calledImmediatelyAfter',
  16. 'calledOn',
  17. 'calledWith',
  18. 'calledOnceWith',
  19. 'calledWithExactly',
  20. 'calledOnceWithExactly',
  21. 'calledWithMatch',
  22. 'returned',
  23. 'thrown',
  24. ]
  25. for (const name of sinonChaiMethodProps) {
  26. if (Object.getOwnPropertyDescriptor(chai.Assertion.prototype, name)?.get) {
  27. delete chai.Assertion.prototype[name]
  28. }
  29. }
  30. chai.use(sinonChai)
  31. chai.use(chaiAsPromised)
  32. beforeEach(() => {
  33. vi.doMock('@overleaf/logger', () => ({
  34. default: {
  35. debug() {},
  36. log() {},
  37. info() {},
  38. warn() {},
  39. error() {},
  40. err() {},
  41. },
  42. }))
  43. })
  44. afterEach(() => {
  45. vi.restoreAllMocks()
  46. vi.resetModules()
  47. })