Common Jest configuration for Lightspeed webapps
npm install @lightspeed/config-jest@lightspeed/config-jest
Jest dependencies and configuration for both JavaScript and TypeScript in a convenient package.
This configuration is meant for Babel+TypeScript projects only, which is our default setup for Lightspeed web applications. For pure TypeScript projects, please use ts-jest directly instead.
Install the dependencies (@types/jest is only needed if your project uses TypeScript):
``sh`
yarn add -D @lightspeed/config-jest jest @types/jest
Consume the Jest configuration by creating a jest.config.js file:
`js`
// jest.config.js
module.exports = require('@lightspeed/config-jest');
Optionally, extend the configuration as you see fit:
`js
// jest.config.js
const baseConfig = require('@lightspeed/config-jest');
module.exports = Object.assign(baseConfig, {
testPathIgnorePatterns: ['
});
`
You can also add your own jest.setup.js file with setupFilesAfterEnv:
`js
// jest.config.js
const baseConfig = require('@lightspeed/config-jest');
module.exports = Object.assign(baseConfig, {
setupFilesAfterEnv: ['
});
`
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:
`sh`
yarn add -D @testing-library/react @testing-library/jest-dom
Then, in your jest.setup.js file, add:
`js`
// 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 library
For node-only projects, you must set testEnvironment to node instead of the default jsdom:
`js
// jest.config.js
const baseConfig = require('@lightspeed/config-jest');
module.exports = Object.assign(baseConfig, {
testEnvironment: 'node',
});
`
Finally, add an npm script to run jest:
`json`
{
"scripts": {
"test": "jest"
}
}
By default jest will run tests on any file named with *.test.(js|jsx|ts|tsx)`.
Go forth and test away! 🎉