Setting Up ESLint for Playwright Projects with TypeScript

Cerosh Jacob
5 min readJun 10, 2024

--

Photo by National Cancer Institute on Unsplash

ESLint is a tool for identifying and reporting ECMAScript/JavaScript code patterns, improving consistency and preventing bugs. It's fully pluggable with rules as runtime addable plugins. Community plugins, configurations, and parsers can also be integrated with ESLint to extend its functionality.

Use the command below to install and configure ESLint for the Playwright project. The answers given below pertain to the questions asked during initialization. Upon successful execution, an eslint.config.js A configuration file will be created for the Playwright project. This file will then be modified to support Playwright projects that use TypeScript.

npm init @eslint/config@latest --save-dev
✔ How would you like to use ESLint? · problems
✔ What type of modules does your project use? · esm
✔ Which framework does your project use? · none
✔ Does your project use TypeScript? · typescript
✔ Where does your code run? · browser
The config that you've selected requires the following dependencies:

eslint@9.x, globals, @eslint/js, typescript-eslint
✔ Would you like to install them now? · Yes
✔ Which package manager do you want to use? · npm

Install additional plugins

npm install @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-playwright --save-dev

Recent versions (v8.0.0 and above) have seen significant changes in ESLint configuration. Previously, It could be set up using. .eslintrc Files in JSON or JavaScript format. However, with the introduction of flat configuration, a single JavaScript configuration file, eslint.config.js, is now required. This example uses ES module syntax, necessitating setting the type field as "module" in package.json. This change is required for Node.js To correctly interpret the Playwright project files.

A flat configuration, an array of objects, is an alternative to a nested one. Each object targets specific files and applies distinct rules. The filesproperty indicates which files the configuration applies to, while the ignores property outlines the files or directories to be excluded. The languageOptions feature permits the parser and parser options to be defined directly in the configuration. Plugins can be imported into the configuration file, allowing recommended rules from plugins or ESLint to spread directly within the rules Object. These alterations primarily affect configuration management without altering the core linting functionality. Below is a sample eslint.config.js.

import typescript from "@typescript-eslint/eslint-plugin";
import playwright from "eslint-plugin-playwright";
import typescriptParser from "@typescript-eslint/parser";
const { configs: typescriptConfigs } = typescript;

export default [
{
files: ["**/*.ts", "**/*.tsx"],
plugins: {
"@typescript-eslint": typescript,
"playwright": playwright
},
languageOptions: {
parser: typescriptParser,
parserOptions: {
ecmaVersion: "latest",
sourceType: "module"
}
},
rules: {
...typescriptConfigs.recommended.rules,
...playwright.configs['flat/recommended'].rules,
"no-console": "warn",
}
}
];

Files, plugins, parsers, and rules are the foundational components of an ESLint configuration. They allow for the customization and enforcement of coding standards, detection of potential errors, and maintenance of code quality within Playwright projects.

Files: This relates to the selection of files or file patterns the configuration applies to, allowing linting on specific files or groups of files. For example, ["tests/**/*.ts"] would select only the TypeScript Playwright test files. Adjust the files Field array to match Playwright test file patterns, depending on the project. However, note that the provided example would select all TypeScript files in the project.

Plugins: ESLint's functionality can be extended by integrating additional linting rules and features through third-party plugins. Typically, these plugins are named packages offering collections of rules for specific types of code or frameworks. They're specified as objects within the plugins Section of the ESLint configuration.

  • @typescript-eslint: This plugin provides comprehensive linting rules for TypeScript, enforcing best practices, catching errors, and maintaining consistent code style. It offers TypeScript-specific rules like type annotations and syntax checks.
  • playwright: This plugin is designed for projects that use the Playwright testing framework. It incorporates rules for best practices and code quality in Playwright test scripts and related files. This ensures the correct use of assertions and helps avoid common pitfalls.

Parsers:

The ESLint setting determines the parser used to analyze code. Accurate application of linting rules requires ESLint to understand the code's syntax and structure. Parsers convert the code into an abstract syntax tree (AST), which ESLint uses to apply its rules. Parser options are specific configurations for the parser that ESLint uses. These options help the parser understand the code's syntax and features by providing relevant information. The ecmaVersion and sourceType These are the most common parser options, enabling ESLint to parse modern JavaScript code as modules. Although ESLint can parse code without explicit parserOptions, including them is recommended. It helps avoid potential issues and ensures consistent parsing behaviour across different environments.

Rules:

This defines the linting rules applied to the specified files. Each rule has a unique identifier and can be one of the following severity levels: "off", "warn", or "error"These rules can be directly stated in the configuration or extended from preset configurations provided by ESLint or plugins. Custom configurations can modify or expand these rules as required.

The recommended rules from the @typescript-eslint and eslint-plugin-playwright Plugins are implemented to enforce best practices and standards. A custom rule to warn when console Statements are used. They allow console Use for debugging purposes and as reminders to remove or adequately handle these statements in production code.

After updating the ESLint configuration file, run ESLint from the command line to check for issues. Navigate to your project directory and execute the command below. By using . as an argument for eslintYou instructed ESLint to examine all files in the current directory and subdirectories. Consequently, ESLint will search for JavaScript, TypeScript, or other configured file types and analyze them according to the rules defined in your ESLint configuration file.

npx eslint .

ESLint can automatically address some issues by running it with the --fix Flag. This applies automatic fixes to the code where possible. However, not all issues can be resolved automatically; some may require manual intervention.

npx eslint . --fix

IDEs often come with plugins or built-in support for ESLint, allowing you to see linting issues in real-time as you code. After installing Microsoft's VS Code ESLint extension, open your VS Code settings (File > Preferences > Settings). Ensure that the settings.json The file includes the entries below to manage specific code actions that occur automatically when saving a file. An explicit value prompts VS Code before applying any automatic fixes suggested by ESLint when you save a file.

"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit",
"source.organizeImports": "explicit"
},

While Prettier handles code formatting, ESLint concentrates on code quality. Ensure Prettier is configured in your project and use it with ESLint for a seamless development experience.

Historically, ESLint plugins such as eslint-plugin-prettier Trigger the gap between code formatting and linting by directly incorporating Prettier's formatting rules into ESLint. However, as of version 8.53.0, ESLint has deprecated these formatting rules in favour of dedicated source code formatted like Prettier. This shift enables ESLint to focus exclusively on code quality and linting while Prettier manages all formatting tasks.

My recent publication compiled a comprehensive collection of 100 similar typescript programs. Each program not only elucidates essential Typescript concepts and expounds upon the significance of test automation but also provides practical guidance on its implementation using Playwright. This resource will undoubtedly be valuable if you look deeper into similar topics.

--

--

No responses yet