bootstrap.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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: [['module-resolver', { alias: { '^@/(.+)': './frontend/js/\\1' } }]],
  5. })
  6. // Load JSDOM to mock the DOM in Node
  7. // Set pretendToBeVisual to enable requestAnimationFrame
  8. require('jsdom-global')(undefined, {
  9. pretendToBeVisual: true,
  10. url: 'https://www.test-overleaf.com/',
  11. })
  12. const path = require('path')
  13. process.env.OVERLEAF_CONFIG = path.resolve(
  14. __dirname,
  15. '../../config/settings.webpack.js'
  16. )
  17. // Load sinon-chai assertions so expect(stubFn).to.have.been.calledWith('abc')
  18. // has a nicer failure messages
  19. const chai = require('chai')
  20. chai.use(require('sinon-chai'))
  21. chai.use(require('chai-as-promised'))
  22. // Populate meta for top-level access in modules on import
  23. const { resetMeta } = require('./helpers/reset-meta')
  24. resetMeta()
  25. // i18n requires access to 'ol-i18n' as defined above
  26. require('../../frontend/js/i18n')
  27. const moment = require('moment')
  28. moment.updateLocale('en', {
  29. calendar: {
  30. lastDay: '[Yesterday]',
  31. sameDay: '[Today]',
  32. nextDay: '[Tomorrow]',
  33. lastWeek: 'ddd, Do MMM YY',
  34. nextWeek: 'ddd, Do MMM YY',
  35. sameElse: 'ddd, Do MMM YY',
  36. },
  37. })
  38. // workaround for missing keys in jsdom-global's keys.js
  39. globalThis.AbortController = global.AbortController = window.AbortController
  40. globalThis.MutationObserver = global.MutationObserver = window.MutationObserver
  41. globalThis.StorageEvent = global.StorageEvent = window.StorageEvent
  42. globalThis.SVGElement = global.SVGElement = window.SVGElement
  43. globalThis.localStorage = global.localStorage = window.localStorage
  44. globalThis.performance = global.performance = window.performance
  45. globalThis.cancelAnimationFrame = global.cancelAnimationFrame =
  46. window.cancelAnimationFrame
  47. globalThis.requestAnimationFrame = global.requestAnimationFrame =
  48. window.requestAnimationFrame
  49. globalThis.sessionStorage = global.sessionStorage = window.sessionStorage
  50. // add polyfill for ResizeObserver
  51. globalThis.ResizeObserver =
  52. global.ResizeObserver =
  53. window.ResizeObserver =
  54. require('@juggle/resize-observer').ResizeObserver
  55. // add stub for BroadcastChannel (unused in these tests)
  56. globalThis.BroadcastChannel =
  57. global.BroadcastChannel =
  58. window.BroadcastChannel =
  59. class BroadcastChannel {
  60. addEventListener(type, listener) {}
  61. removeEventListener(type, listener) {}
  62. postMessage(message) {}
  63. }
  64. // add stub for WebSocket state enum
  65. globalThis.WebSocket = class WebSocket {
  66. static CONNECTING = 0
  67. static OPEN = 1
  68. static CLOSING = 2
  69. static CLOSED = 3
  70. }
  71. // node-fetch doesn't accept relative URL's: https://github.com/node-fetch/node-fetch/blob/master/docs/v2-LIMITS.md#known-differences
  72. const fetch = require('node-fetch')
  73. globalThis.fetch =
  74. global.fetch =
  75. window.fetch =
  76. (url, ...options) => fetch(new URL(url, 'http://127.0.0.1'), ...options)
  77. // ignore style/image files
  78. const { addHook } = require('pirates')
  79. addHook(() => '', {
  80. exts: ['.css', '.less', '.svg', '.png', '.gif', '.mp4'],
  81. ignoreNodeModules: false,
  82. })
  83. globalThis.HTMLElement.prototype.scrollIntoView = () => {}
  84. globalThis.DOMParser = window.DOMParser
  85. // Polyfill for IndexedDB
  86. require('fake-indexeddb/auto')