vitest_bootstrap.mjs 1.3 KB

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