Skip to content

Commit

Permalink
Tests: Prevent unhandled rejections crashing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Feb 21, 2019
1 parent 051ef50 commit 186516d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
11 changes: 10 additions & 1 deletion test/lazy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const React = require('react'),
{Suspense} = React;

// Imports
const {itRenders, lazy, removeSpacing} = require('./utils');
const {itRenders, lazy, removeSpacing, preventUnhandledRejection} = require('./utils');

// Globals
const spy = jest.fn;
Expand Down Expand Up @@ -345,6 +345,7 @@ describe('multiple lazy components', () => {
);

const p = render(e);
preventUnhandledRejection(p);
expect(Lazy1).toHaveBeenCalled();
expect(Lazy2).toHaveBeenCalled();
expect(Lazy3).toHaveBeenCalled();
Expand Down Expand Up @@ -375,6 +376,7 @@ describe('multiple lazy components', () => {
);

const p = render(e);
preventUnhandledRejection(p);
expect(Lazy1).toHaveBeenCalled();
expect(Lazy2).toHaveBeenCalled();
expect(Lazy3).toHaveBeenCalled();
Expand Down Expand Up @@ -499,6 +501,7 @@ describe('multiple lazy components', () => {
);

const p = render(e);
preventUnhandledRejection(p);

expect(Lazy1.promise.abort).toHaveBeenCalledTimes(1);
expect(Lazy2.promise.abort).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -543,6 +546,7 @@ describe('multiple lazy components', () => {
);

const p = render(e);
preventUnhandledRejection(p);

expect(Lazy1).toHaveBeenCalled();
expect(Lazy2).toHaveBeenCalled();
Expand Down Expand Up @@ -584,6 +588,7 @@ describe('multiple lazy components', () => {
);

const p = render(e);
preventUnhandledRejection(p);

expect(Lazy1.promise.abort).toHaveBeenCalledTimes(1);
expect(Lazy2.promise.abort).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -647,6 +652,7 @@ describe('nested lazy components', () => {
);

const p = render(e);
preventUnhandledRejection(p);
await Lazy.promise;
await expect(p).rejects.toBe(LazyInner.promise);
});
Expand All @@ -669,6 +675,7 @@ describe('nested lazy components', () => {
);

const p = render(e);
preventUnhandledRejection(p);
await Lazy.promise;
await LazyInner.promise;
await expect(p).rejects.toBe(LazyInnerInner.promise);
Expand Down Expand Up @@ -751,6 +758,7 @@ describe('nested lazy components', () => {
);

const p = render(e);
preventUnhandledRejection(p);

await Lazy3.promise;

Expand Down Expand Up @@ -780,6 +788,7 @@ describe('nested lazy components', () => {
);

const p = render(e);
preventUnhandledRejection(p);

await Lazy1.promise;
await Lazy2.promise;
Expand Down
7 changes: 6 additions & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ module.exports = {
itRenders: wrapMethod(itRenders),
itRendersWithSyncCompare: wrapMethod(itRendersWithSyncCompare),
lazy,
removeSpacing
removeSpacing,
preventUnhandledRejection
};

/*
Expand Down Expand Up @@ -195,3 +196,7 @@ function lazySync(component) {
function removeSpacing(text) {
return text.replace(/\s*(?:\r?\n|^|$)\s*/g, '');
}

function preventUnhandledRejection(promise) {
promise.catch(() => {});
}

0 comments on commit 186516d

Please sign in to comment.