bootstrap.mjs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { afterEach, beforeEach, chai, vi } from 'vitest'
  2. import 'sinon-mongoose'
  3. import sinon from 'sinon'
  4. import logger from '@overleaf/logger'
  5. import sinonChai from 'sinon-chai'
  6. import chaiAsPromised from 'chai-as-promised'
  7. import mongoose from 'mongoose'
  8. import mongodb from 'mongodb-legacy'
  9. mongodb.ObjectId.cacheHexString = true
  10. /*
  11. * Chai configuration
  12. */
  13. // add chai.should()
  14. chai.should()
  15. // Load sinon-chai assertions so expect(stubFn).to.have.been.calledWith('abc')
  16. // has a nicer failure messages
  17. chai.use(sinonChai)
  18. // Load promise support for chai
  19. chai.use(chaiAsPromised)
  20. // Do not truncate assertion errors
  21. chai.config.truncateThreshold = 0
  22. vi.mock('@overleaf/logger', async () => {
  23. return {
  24. default: {
  25. debug: vi.fn(),
  26. info: vi.fn(),
  27. log: vi.fn(),
  28. warn: vi.fn(),
  29. err: vi.fn(),
  30. error: vi.fn(),
  31. fatal: vi.fn(),
  32. },
  33. }
  34. })
  35. beforeEach(ctx => {
  36. // This function is a utility to duplicate the behaviour of passing `done` in place of `next` in an express route handler.
  37. ctx.rejectOnError = reject => {
  38. return err => {
  39. if (err) {
  40. reject(err)
  41. }
  42. }
  43. }
  44. ctx.logger = logger
  45. })
  46. afterEach(() => {
  47. vi.restoreAllMocks()
  48. vi.resetModules()
  49. sinon.restore()
  50. const modelNames = mongoose.modelNames()
  51. modelNames.forEach(name => {
  52. delete mongoose.connection.models[name]
  53. })
  54. })