When utilizing the Page Object Model in Playwright with TypeScript, a Linting Error may occur, specifically a Parsing error related

Recently, I started using playwright and decided to implement the page object model using typescript. Everything was going smoothly until I ran a lint check. Unfortunately, the linting check failed in the Pull Request Check on GitHub.

The error is occurring specifically on my login page during the linting check.

import { Page } from "@playwright/test";

export class LoginPage {
    readonly page: Page;
    readonly userName: any;
    readonly password: any;
    readonly loginButton: any;

    constructor(page: Page) {
        this.page = page;
        this.userName = this.page.locator('[data-qa="login-input-email"]');
        this.password = this.page.locator('[data-qa="login-input-password"]');
        this.loginButton = this.page.locator('[data-qa="login-button"]');
    }

    /**
     * @param {string} text
     */
    async enterUsername(text) {
        await this.userName.fill(text);
    }

    /**
     * @param {string} text
     */
    async enterPassword(text) {
        await this.password.fill(text);
    }

    async clickLoginBtn() {
        await this.loginButton.click();
    }
}

When I tried running the lint command below in the terminal

npx eslint "./src/**" --cache

This is the error message it returned.

C:\Users\path\Desktop\path\Workspace\path\path\src\portal\locators\common\CommonPage.ts 5:13 error Parsing error: Unexpected token (5:13)

The issue seems to be related to the following declarations.

readonly page: Page;
readonly userName: any;
readonly password: any;
readonly loginButton: any;

Here is the content of .eslintrc.js

module.exports = {
    "env": {
        "es2021": true,
        "node": true
    },

    "extends": "eslint:recommended",
    "overrides": [
        {
            "env": {
            "node": true
        },
        "files": [
            ".eslintrc.{js,cjs}"
        ],
        "parserOptions": {
            "sourceType": "script",
            }
        }
    ],
    "parser": "@babel/eslint-parser",
    "parserOptions": {
    "requireConfigFile": false,
    "ecmaVersion": 2020,
    "sourceType": "module",
    "babelOptions": {
          "babelrc": false,
          "configFile": false
        },
    },
    "rules": {
    }
}

Does anyone have any insights on how to resolve this issue? Thank you!

Answer №1

If you are currently using @babel/eslint-parser as your parser, keep in mind that it is specifically designed for parsing JavaScript code, not Typescript. For parsing Typescript code, it is recommended to switch to @typescript-eslint/parser.

Make sure to install the following packages:

npm install -D eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin

Next, update your .eslintrc.js file with the following configuration:

module.exports = {
    "env": {
        "es2021": true,
        "node": true
    },
    "extends": [
        "eslint:recommended",
        "plugin:@typescript-eslint/recommended"
    ],
    "parser": "@typescript-eslint/parser",
    "overrides": [
        {
            "env": {
                "node": true
            },
            "files": [
                ".eslintrc.{js,cjs}"
            ],
            "parserOptions": {
                "sourceType": "script",
            }
        }
    ],
    "parserOptions": {
        "requireConfigFile": false,
        "ecmaVersion": 2020,
        "sourceType": "module",
    },
    "plugins": [
        "@typescript-eslint" 
    ],
    "rules": {
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Objects that are included into an array will end up with an undefined value

Currently, I am in the process of coding a command for my Discord bot that involves adding items to an array of objects stored within a JSON file. To achieve this functionality, I have implemented the following code snippet: let rawdata = fs.readFileSync(& ...

Performing a fetch() POST request in Express.js results in an empty body object being generated

Purpose: Implement a function in fetch() to send specified string data from the HTML document, for example "MY DATA" Here is the code snippet: HTML Code: <!DOCTYPE html> <html> <body> <script type="text/javascript"> function ...

Prevent unexpected page breaks in <tr> elements on Firefox (Could JavaScript be the solution?)

Wondering if there are any ways, possibly through JavaScript or CSS, to avoid having page breaks within <tr> elements specifically in Firefox. Given that FF does not yet fully support page-break-inside, I assume this might need to be addressed using ...

Allow users to zoom in and out on a specific section of the website similar to how it works on Google Maps

I am looking to implement a feature on my website similar to Google Maps. I want the top bar and side bars to remain fixed regardless of scrolling, whether using the normal scroll wheel or CTRL + scroll wheel. However, I would like the central part of the ...

Mastering the integration of NestJS with Redis for microservices

Currently, I am diving into the world of nestjs microservices. I am curious, what commands are available for me to use? const pattern = { cmd: 'get' }; this.client.send<any>(pattern, data) Additionally, how do I go about retrieving data ...

Encountering a duplicate key error in ExpressJS collection

Whenever I try to create a new event with categories that already exist in my database, such as creating an event with the category "javascript" and then attempting to create another event with categories "javascript, html, css", I encounter the error mess ...

What is the best way to notify the user if the percentage entered is not a numeric value?

My role is to notify the user when the entered value exceeds the acceptable range: if (document.myForm.outputPercentage.value <= 0 || document.myForm.outputPercentage.value >= 100) { alert( "Please enter a percentage between 1 and 100 ...

What is the method for modifying the array that has been generated using Vue's "prop" feature?

According to the Vue documentation, a prop is passed in as a raw value that may need transformation. The recommended approach is to define a computed property using the prop's value. If the "prop" is an array of objects, how can it be transformed int ...

Transitioning from a fixed position to absolute in CSS3

Sticky Logo Element In my project, I implemented a logo element that is positioned absolutely within the document. As the user scrolls, the logo sticks to the top of the window with a fixed position (you can view an example here: https://jsfiddle.net/swzb ...

Utilizing Angular JS to parse JSON data and showcase it in various tables

Just diving into Angular JS and looking for some guidance. Can someone show me how to parse and showcase JSON Data in separate tables using Angular JS? [ { "id": 0, "isActive": false, "balance": 1025.00, "picture": "htt ...

How to Avoid Duplicating Documents in MongoDB?

I'm currently exploring effective methods to avoid saving duplicate documents in MongoDB. Currently, my form captures the user's URL input. The workflow is as follows: Validate if the user's URL is valid (using dns.lookup). If the use ...

One of the challenges faced with using AngularJS is that it can often

I have a piece of code that is functioning correctly: angular.module('foo', []).config( function($locationProvider) { $locationProvider.html5Mode(true); } ); However, when the code is minified, it gets compressed and looks like this: a ...

Tips for implementing try-catch with multiple promises without utilizing Promise.all methodology

I am looking to implement something similar to the following: let promise1 = getPromise1(); let promise2 = getPromise2(); let promise3 = getPromise3(); // additional code ... result1 = await promise1; // handle result1 in a specific way result2 = await ...

"Learn how to dynamically update the user interface in express.js using handlebars without having to refresh the

As a newcomer to using express.js and Handlebars, I am faced with the challenge of implementing autocomplete functionality. Specifically, I want to enable autocompletion in a text input field without triggering a page refresh when users interact with it. C ...

When a user connects to Node.js using sockets, the previous messages are loaded. However, a bug causes the messages to be loaded to all chat users, instead of just the

New to node.js, I am currently creating a chat application with two main files: server.js (server side) and script.js (client side). In the server.js file: socket.on('previousMessages', function (data){ db.query("SELECT * FROM messages", f ...

After a postback in JavaScript, the Root Path variable becomes null

I have been attempting to save the Root URL in a JavaScript variable within my _Layout.cshtml like this: <script type="text/javascript> var rootpath = ""; $(document).ready(function () { rootpath = "@VirtualPathUtility.ToAbsolute("~/ ...

Extend JavaScript capabilities for window.print() function to automatically include backgrounds

I am looking to add a special magical property to my Print this Page button. This property will automatically enable the default unset option (shown in the picture) which is to print the backgrounds of div colors and background images. <a href="#" oncl ...

How can I access properties of generic types in TypeScript?

Converting the generic type to any is a valid approach (The type E could be a typescript type, class, or interface) of various entities like Product, Post, Todo, Customer, etc.: function test<E>(o:E):string { return (o as any)['property' ...

Functions have been successfully deployed, but they are not appearing on the Azure Portal

I am experiencing difficulties deploying basic Typescript functions to Azure. Despite a successful deployment using VS code and confirmation in the Output window, I cannot see any functions listed in the Azure Portal under the Function App: https://i.stac ...

Having trouble finding element in Google Drive preview mode with Selenium in Python

Following the automation of the steps below: accessing the shared link of the file in Chrome at https://drive.google.com/file/d/1jUtvNE0bIJuRZwzll9xJbMxBuYIeh1mj/view logging in with Gmail I have encountered difficulty in locating the connected apps sec ...