Blog>
Snippets

Configuring ESLint for jest tests in Next.js

Present a way to accommodate ESLint for jest testing in Next.js by adding jest plugin and environment information in ESLint configuration.
module.exports = {
  // Extends the UDE recommended base configuration and plugin for React
  extends: ['eslint:recommended', 'plugin:react/recommended', 'next', 'next/core-web-vitals'],
  // Enables ESLint to understand Jest globals when a file contains tests
  env: {
    es6: true,
    node: true,
    'jest/globals': true
  },
  // Sets the parser options for ECMAScript versions
  parserOptions: {
    ecmaVersion: 2018,
    sourceType: 'module'
  },
  // Specifies the ESLint plugin for Jest
  plugins: ['jest'],
  // eslint-plugin-jest settings
  settings: {
    jest: {
      version: 26
    }
  }
};
This ESLint configuration extends recommended settings with Next.js and Jest configurations, sets up the environment to understand Jest globals, specifies Jest as a plugin, and configures Jest within ESLint settings.