jest-shim.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. /* no-op */
  8. };
  9. const createContextualFragment = (html: string) => {
  10. const div = document.createElement('div');
  11. div.innerHTML = html;
  12. return div.children[0]; // so hokey it's not even funny
  13. };
  14. (global as any).Range.prototype.createContextualFragment = (html: string) =>
  15. createContextualFragment(html);
  16. window.focus = () => {
  17. /* no-op */
  18. };
  19. // HACK: Polyfill that allows codemirror to render in a JSDOM env.
  20. (window as any).document.createRange = function createRange() {
  21. return {
  22. setEnd: () => {
  23. /* no-op */
  24. },
  25. setStart: () => {
  26. /* no-op */
  27. },
  28. getBoundingClientRect: () => ({ right: 0 }),
  29. getClientRects: (): ClientRect[] => [],
  30. createContextualFragment
  31. };
  32. };
  33. (window as any).document.elementFromPoint = (left: number, top: number) =>
  34. document.body;
  35. process.on('unhandledRejection', (error, promise) => {
  36. console.error('Unhandled promise rejection somewhere in tests');
  37. if (error) {
  38. console.error(error);
  39. const stack = (error as any).stack;
  40. if (stack) {
  41. console.error(stack);
  42. }
  43. }
  44. promise.catch(err => console.error('promise rejected', err));
  45. });