Babel with Jest, ESLint, Prettier, and Wallaby
Explore how to configure Babel alongside Jest for running React tests, incorporate ESLint and Prettier for code quality and formatting, and set up Wallaby for real-time test feedback. Understand how these tools work together to support test-driven development with React components.
We'll cover the following...
Bridging Jest and Babel
Let’s bring Jest to the party.
-
In order to get Jest to run tests through Babel, you’ll need both
jestand a package calledbabel-jest:$ npm install --save-dev jest@23.6.0 babel-jest@23.6.0 + jest@23.6.0 + babel-jest@23.6.0 -
As of this writing, you’ll also need a special version of the
babel-corepackage. The reason is that the package recently switched names. Babel 6 called itbabel-core, but Babel 7 namespaces it as@babel/core. So this package creates a bridge for projects looking forbabel-coreto direct them to@babel/core:$ npm install --save-dev babel-core@^7.0.0-bridge.0 + babel-core@^7.0.0-bridge.0 -
Next, update the
scriptsentry inpackage.jsonto make Jest the project’s official test runner:
-
Since you will be writing React components, you will of course need the
reactpackage:$ npm install react@16.4.2 + react@16.4.2Notice that this is the first time in the course that we’ve omitted
--save-devwhen installing a dependency. Since React will be used at runtime in our app (remember, those JSX tags compile toReact.createElement()calls), it’s not just a development dependency. -
You’ll also need the
react-dompackage, a bridge between React elements and the DOM, though only as a dev dependency for testing:$ npm install --save-dev react-dom@16.4.2 + react-dom@16.4.2
Test using Jest and JSX
Create a test file that takes advantage of JSX and the ES2015 import syntax: ...