Skip to content

Instantly share code, notes, and snippets.

@gabrielfalcao
Created November 17, 2021 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielfalcao/5e6f868ed963f9bae61a6d02b31b614c to your computer and use it in GitHub Desktop.
Save gabrielfalcao/5e6f868ed963f9bae61a6d02b31b614c to your computer and use it in GitHub Desktop.
Jest Config to declare a test pyramid based on "projects"
// Example of test pyramid using jest
// Basic usage:
// jest --selectProjects 'unit tests'
// Assumes that you organize tests under __${type}_tests__/**/*.js
//
const path = require('path');
const correctPath = value => path.resolve(__dirname, value || '');
const testOfType = (name, config = {}) => ({
displayName: {
name: `${name} tests`,
color: `green`,
},
collectCoverage: true,
moduleNameMapper: {
// rename @my-project to the import path of your module
//'@my-project/(.*)': '<rootDir>/$1', // use this if your files are not under "src"
'@my-project': '<rootDir>/src'
},
testMatch: [`__${name}_tests__/*.test.js`, `**/__${name}_tests__/*.test.js`].map(correctPath),
coveragePathIgnorePatterns: ['/node_modules/', `.*__${name}_tests__.*`],
testPathIgnorePatterns: ['/node_modules/'],
...config,
});
const config = {
rootDir: correctPath(),
passWithNoTests: true,
projects: [
testOfType('unit', {
resetMocks: true,
coveragePathIgnorePatterns: ['/node_modules/', '.*__unit_tests__.*', '.*__mocks__.*'],
}),
testOfType('functional'),
],
};
module.exports = config;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment