jest-shim.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Shims originally adapted from https://github.com/nteract/nteract/blob/47f8b038ff129543e42c39395129efc433eb4e90/scripts/test-shim.js
  2. const fetchMod = ((window as any).fetch = require('node-fetch')); // tslint:disable-line
  3. (window as any).Request = fetchMod.Request;
  4. (window as any).Headers = fetchMod.Headers;
  5. (global as any).Image = (window as any).Image;
  6. (global as any).Range = function Range() {};
  7. const createContextualFragment = (html: string) => {
  8. const div = document.createElement('div');
  9. div.innerHTML = html;
  10. return div.children[0]; // so hokey it's not even funny
  11. };
  12. (global as any).Range.prototype.createContextualFragment = (html: string) =>
  13. createContextualFragment(html);
  14. window.focus = () => {};
  15. // HACK: Polyfill that allows codemirror to render in a JSDOM env.
  16. (window as any).document.createRange = function createRange() {
  17. return {
  18. setEnd: () => {},
  19. setStart: () => {},
  20. getBoundingClientRect: () => ({ right: 0 }),
  21. getClientRects: (): ClientRect[] => [],
  22. createContextualFragment
  23. };
  24. };
  25. (window as any).document.elementFromPoint = (left: number, top: number) =>
  26. document.body;
  27. process.on('unhandledRejection', (error, promise) => {
  28. console.error('Unhandled promise rejection somewhere in tests');
  29. console.error(error);
  30. console.error(error.stack);
  31. promise.catch(err => console.error('promise rejected', err));
  32. });