setup.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { beforeEach, afterEach, chai, vi } from 'vitest'
  2. import sinon from 'sinon'
  3. import mongodb from 'mongodb'
  4. import sinonChai from 'sinon-chai'
  5. import chaiAsPromised from 'chai-as-promised'
  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. // ensure every ObjectId has the id string as a property for correct comparisons
  33. mongodb.ObjectId.cacheHexString = true
  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. fatal: sandbox.stub(),
  44. },
  45. }
  46. beforeEach(ctx => {
  47. ctx.logger = stubs.logger
  48. vi.doMock('@overleaf/logger', () => ({
  49. default: ctx.logger,
  50. }))
  51. })
  52. afterEach(() => {
  53. sandbox.reset()
  54. vi.restoreAllMocks()
  55. vi.resetModules()
  56. })