bootstrap.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Run babel on tests to allow support for import/export statements in Node
  2. require('@babel/register')({
  3. extensions: ['.ts', '.tsx', '.js', '.jsx', '.mjs'],
  4. plugins: [
  5. [
  6. 'module-resolver',
  7. {
  8. alias: {
  9. '^@/(.+)': './frontend/js/\\1',
  10. '^@modules/(.+)': './modules/\\1',
  11. },
  12. },
  13. ],
  14. ],
  15. })
  16. // Load JSDOM to mock the DOM in Node
  17. // Set pretendToBeVisual to enable requestAnimationFrame
  18. require('jsdom-global')(undefined, {
  19. pretendToBeVisual: true,
  20. url: 'https://www.test-overleaf.com/',
  21. })
  22. const path = require('path')
  23. process.env.OVERLEAF_CONFIG = path.resolve(
  24. __dirname,
  25. '../../config/settings.webpack.js'
  26. )
  27. // Load sinon-chai assertions so expect(stubFn).to.have.been.calledWith('abc')
  28. // has a nicer failure messages
  29. const chai = require('chai')
  30. chai.use(require('sinon-chai'))
  31. chai.use(require('chai-as-promised'))
  32. // Populate meta for top-level access in modules on import
  33. const { resetMeta } = require('./helpers/reset-meta')
  34. resetMeta()
  35. // i18n requires access to 'ol-i18n' as defined above
  36. require('../../frontend/js/i18n')
  37. const moment = require('moment')
  38. moment.updateLocale('en', {
  39. calendar: {
  40. lastDay: '[Yesterday]',
  41. sameDay: '[Today]',
  42. nextDay: '[Tomorrow]',
  43. lastWeek: 'ddd, Do MMM YY',
  44. nextWeek: 'ddd, Do MMM YY',
  45. sameElse: 'ddd, Do MMM YY',
  46. },
  47. })
  48. // workaround for missing keys in jsdom-global's keys.js
  49. globalThis.AbortController = global.AbortController = window.AbortController
  50. globalThis.MutationObserver = global.MutationObserver = window.MutationObserver
  51. globalThis.StorageEvent = global.StorageEvent = window.StorageEvent
  52. globalThis.SVGElement = global.SVGElement = window.SVGElement
  53. globalThis.localStorage = global.localStorage = window.localStorage
  54. globalThis.cancelAnimationFrame = global.cancelAnimationFrame =
  55. window.cancelAnimationFrame
  56. globalThis.requestAnimationFrame = global.requestAnimationFrame =
  57. window.requestAnimationFrame
  58. globalThis.sessionStorage = global.sessionStorage = window.sessionStorage
  59. // add polyfill for ResizeObserver
  60. globalThis.ResizeObserver =
  61. global.ResizeObserver =
  62. window.ResizeObserver =
  63. require('@juggle/resize-observer').ResizeObserver
  64. // add stub for BroadcastChannel (unused in these tests)
  65. globalThis.BroadcastChannel =
  66. global.BroadcastChannel =
  67. window.BroadcastChannel =
  68. class BroadcastChannel {
  69. // Unused arguments left to document the signature of the stubbed function.
  70. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  71. addEventListener(type, listener) {}
  72. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  73. removeEventListener(type, listener) {}
  74. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  75. postMessage(message) {}
  76. }
  77. // add stub for WebSocket state enum
  78. globalThis.WebSocket = class WebSocket {
  79. static CONNECTING = 0
  80. static OPEN = 1
  81. static CLOSING = 2
  82. static CLOSED = 3
  83. }
  84. // node-fetch doesn't accept relative URL's: https://github.com/node-fetch/node-fetch/blob/master/docs/v2-LIMITS.md#known-differences
  85. const fetch = require('node-fetch')
  86. globalThis.fetch =
  87. global.fetch =
  88. window.fetch =
  89. (url, ...options) => fetch(new URL(url, 'http://127.0.0.1'), ...options)
  90. // ignore style/image files
  91. const { addHook } = require('pirates')
  92. addHook(() => '', {
  93. exts: ['.css', '.scss', '.svg', '.png', '.gif', '.mp4'],
  94. ignoreNodeModules: false,
  95. })
  96. globalThis.HTMLElement.prototype.scrollIntoView = () => {}
  97. globalThis.DOMParser = window.DOMParser
  98. // Polyfill for IndexedDB
  99. require('fake-indexeddb/auto')
  100. const fetchMock = require('fetch-mock').default
  101. fetchMock.spyGlobal()
  102. fetchMock.config.fetch = global.fetch
  103. fetchMock.config.Response = fetch.Response
  104. Object.defineProperty(navigator, 'onLine', {
  105. configurable: true,
  106. get: () => true,
  107. })