vitest_bootstrap.mjs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 SandboxedModule from 'sandboxed-module'
  8. import { createRequire } from 'module'
  9. const require = createRequire(import.meta.url)
  10. SandboxedModule.configure({
  11. ignoreMissing: true,
  12. requires: {
  13. // This is already imported the same way in the mocha bootstrap
  14. // eslint-disable-next-line import/no-extraneous-dependencies
  15. sshpk: require('sshpk'),
  16. },
  17. globals: {
  18. AbortController,
  19. AbortSignal,
  20. Buffer,
  21. Promise,
  22. console,
  23. process,
  24. URL,
  25. TextEncoder,
  26. TextDecoder,
  27. },
  28. sourceTransformers: {
  29. removeNodePrefix: function (source) {
  30. return source.replace(/require\(['"]node:/g, "require('")
  31. },
  32. },
  33. })
  34. /*
  35. * Chai configuration
  36. */
  37. // add chai.should()
  38. chai.should()
  39. // Load sinon-chai assertions so expect(stubFn).to.have.been.calledWith('abc')
  40. // has a nicer failure messages
  41. chai.use(sinonChai)
  42. // Load promise support for chai
  43. chai.use(chaiAsPromised)
  44. // Do not truncate assertion errors
  45. chai.config.truncateThreshold = 0
  46. vi.mock('@overleaf/logger', async () => {
  47. return {
  48. default: {
  49. debug: vi.fn(),
  50. info: vi.fn(),
  51. log: vi.fn(),
  52. warn: vi.fn(),
  53. err: vi.fn(),
  54. error: vi.fn(),
  55. fatal: vi.fn(),
  56. },
  57. }
  58. })
  59. beforeEach(ctx => {
  60. // This function is a utility to duplicate the behaviour of passing `done` in place of `next` in an express route handler.
  61. ctx.rejectOnError = reject => {
  62. return err => {
  63. if (err) {
  64. reject(err)
  65. }
  66. }
  67. }
  68. ctx.logger = logger
  69. })
  70. afterEach(() => {
  71. vi.restoreAllMocks()
  72. vi.resetModules()
  73. sinon.restore()
  74. })