Jest Configuration for New PyCharm Projects

alpharithms fallback 1

Both Jest and PyCharm are incredible tools for software developers, but getting them to work together can be challenging. Fortunately, setting up a new PyCharm project correctly will allow most future Jest tests you write to execute without headache. This article provides a bare-minimum implementation of the configuration requirements to set up Jest with PyCharm.

Required Dependencies

This article assumes the use of TypeScript and sets up the environment to support TypeScript awareness within Jest modules. The following should be installed either in the local environment or the global environment (local syntax shown here):

npm install --save-dev jest ts-jest @types/jest typescript

Configuration Files

tsconfig.json

TypeScript needs to be informed that Jest files are part of the project, and the following is an example of a new tsconfig.json (TypeScript configuration file) that will get this setup.

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "commonjs",
    "esModuleInterop": true,
    "strict": true,
    "skipLibCheck": true,
    "types": ["jest", "node"]  // note the 'jest' inclusion here
  },
  "include": ["**/*.ts"]
}

Most of this is boilerplace — required for TSConfig — but the "jest" entry  in the types field is where the magic happens.

jest.config.cjs

Modern module-based TypeScript imports also require some configuration in Jest. The following file — formatted as .cjs to avoid module import errors — should be populated as follows:

/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
};

PyCharm Run Configurations

The configuration files defined thus far would provide PyCharm with all it needs to execute Jest-based test files from the native UI (little green arrows in the gutters), but if you hit snags, you can go to Run > Edit Configurations > Edit Configuration Templates > Jest > Jest Options and add the following:  --config=jest.config.js.

pycharm jest config
PyCharm global runtime configuration options for Jest will ensure this works for any new test files in the project.

Example Project Structure

These files should be all that you need to get started. An example of how these might look within a PyCharm-based project (or any project for that matter) is as follows:

your-project/
├── main.ts              ← Primary application code
├── main.spec.ts         ← Jest tests
├── jest.config.cjs
├── tsconfig.json
└── package.json

Discussion

This configuration is intended to be an example of the bare minimum required to get Jest + PyCharm configured. More complex configuration options are almost guaranteed depending on your project requirements. Copy/paste these at your own risk.

 

Zαck West
Full-stack software engineer specializing in product-focused user experiences, AI/ML-powered applications, and scalable distributed web architectures.