retryableImport.spec.jsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import retryableImport from 'sentry/utils/retryableImport';
  2. describe('retryableImport', function () {
  3. it('can dynamically import successfully on first try', async function () {
  4. const importMock = jest.fn();
  5. importMock.mockReturnValue(
  6. new Promise(resolve =>
  7. resolve({
  8. default: {
  9. foo: 'bar',
  10. },
  11. })
  12. )
  13. );
  14. const result = await retryableImport(() => importMock());
  15. expect(result).toEqual({
  16. default: {
  17. foo: 'bar',
  18. },
  19. });
  20. expect(importMock).toHaveBeenCalledTimes(1);
  21. });
  22. it('does not retry if error was not a webpack chunk loading error', async function () {
  23. const importMock = jest.fn();
  24. importMock.mockReturnValueOnce(
  25. new Promise((_resolve, reject) => reject(new Error('Another error happened')))
  26. );
  27. try {
  28. await retryableImport(() => importMock());
  29. } catch (err) {
  30. // do nothing
  31. }
  32. expect(importMock).toHaveBeenCalledTimes(1);
  33. });
  34. it('can fail 2 dynamic imports and succeed on 3rd try', async function () {
  35. const importMock = jest.fn();
  36. importMock
  37. .mockReturnValueOnce(
  38. new Promise((_resolve, reject) => reject(new Error('Loading chunk 123 failed')))
  39. )
  40. .mockReturnValueOnce(
  41. new Promise((_resolve, reject) => reject(new Error('Loading chunk 123 failed')))
  42. )
  43. .mockReturnValue(
  44. new Promise(resolve =>
  45. resolve({
  46. default: {
  47. foo: 'bar',
  48. },
  49. })
  50. )
  51. );
  52. const result = await retryableImport(() => importMock());
  53. expect(result).toEqual({
  54. default: {
  55. foo: 'bar',
  56. },
  57. });
  58. expect(importMock).toHaveBeenCalledTimes(3);
  59. });
  60. it('only retries 3 times', async function () {
  61. const importMock = jest.fn(
  62. () =>
  63. new Promise((_resolve, reject) => reject(new Error('Loading chunk 123 failed')))
  64. );
  65. await expect(retryableImport(() => importMock())).rejects.toThrow(
  66. 'Loading chunk 123 failed'
  67. );
  68. expect(importMock).toHaveBeenCalledTimes(3);
  69. });
  70. });