@lightspeed/config-jest
Introduction
Jest dependencies and configuration for both JavaScript and TypeScript in a convenient package.Important note
This configuration is meant for Babel+TypeScript projects only, which is our default setup for Lightspeed web applications. For pure TypeScript projects, please usets-jest
directly instead.Quick Start
Install
Install the dependencies (@types/jest
is only needed if your project uses TypeScript):yarn add -D @lightspeed/config-jest jest @types/jest
Setup
Consume the Jest configuration by creating ajest.config.js
file:// jest.config.js
module.exports = require('@lightspeed/config-jest');
Optionally, extend the configuration as you see fit:
// jest.config.js
const baseConfig = require('@lightspeed/config-jest');
module.exports = Object.assign(baseConfig, {
testPathIgnorePatterns: ['<rootDir>/some-dir'],
});
You can also add your own
jest.setup.js
file with setupFilesAfterEnv
:// jest.config.js
const baseConfig = require('@lightspeed/config-jest');
module.exports = Object.assign(baseConfig, {
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
});
React projects
If in a React project, we highly recommend installing @testing-library/react and @testing-library/jest-dom as they are amazing testing tools on top of Jest for React.Install dependencies:
yarn add -D @testing-library/react @testing-library/jest-dom
Then, in your
jest.setup.js
file, add:// add some helpful assertions
require('@testing-library/jest-dom/extend-expect');
Note that
react-testing-library
methods will be available out of the box after installing the libraryNode projects
For node-only projects, you must settestEnvironment
to node
instead of the default jsdom
:// jest.config.js
const baseConfig = require('@lightspeed/config-jest');
module.exports = Object.assign(baseConfig, {
testEnvironment: 'node',
});
Add an npm script to run your tests
Finally, add an npm script to runjest
:{
"scripts": {
"test": "jest"
}
}
By default
jest
will run tests on any file named with *.test.(js|jsx|ts|tsx)
.Go forth and test away! 🎉