dimanche 11 mars 2018

How to test React ErrorBoundary

New to React, but not to test applications.

I'd like to make sure every time a component throws a error the ErrorBoundary is displayed. If you don't know what I mean by ErrorBoundary here is a link.

I'm using Mocha + Chai + Enzyme.

Let's say we need to test React counter example using the following test configuration.

Test Configuration

// DOM
import jsdom from 'jsdom';
const {JSDOM} = jsdom;
const {document} = (new JSDOM('<!doctype html><html><body></body></html>')).window;
global.document = document;
global.window = document.defaultView;
global.navigator = global.window.navigator;

// Enzyme
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });

// Chai
import chai from 'chai';
import chaiEnzyme from 'chai-enzyme';
function myAwesomeDebug (wrapper) {
  let html = wrapper.html()
  // do something cool with the html
  return `\n\tDEBUG:\n\t${html}`
}
chai.use(chaiEnzyme(myAwesomeDebug));

Aucun commentaire:

Enregistrer un commentaire