setup.js 1.4 KB

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