bootstrap.mjs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. // Mock metrics in unit tests, can be overridden
  36. vi.mock('@overleaf/metrics', () => {
  37. return {
  38. default: { prom: { Counter: vi.fn(), Histogram: vi.fn() } },
  39. }
  40. })
  41. beforeEach(ctx => {
  42. // This function is a utility to duplicate the behaviour of passing `done` in place of `next` in an express route handler.
  43. ctx.rejectOnError = reject => {
  44. return err => {
  45. if (err) {
  46. reject(err)
  47. }
  48. }
  49. }
  50. ctx.logger = logger
  51. })
  52. afterEach(() => {
  53. vi.resetAllMocks()
  54. vi.resetModules()
  55. sinon.restore()
  56. const modelNames = mongoose.modelNames()
  57. modelNames.forEach(name => {
  58. delete mongoose.connection.models[name]
  59. })
  60. })