안성민
안성민

Reputation: 183

TypeScript + Playwright Error: Cannot find module

I ran into a problem coding test automation with playwright. When running the test, the following error occurs at test.spec.ts:

Error: Cannot find module '@common/common' code: 'MODULE_NOT_FOUND'

How to solve this problem?

there are codes below

test.spec.ts

import { chromium, ChromiumBrowser, Page } from "playwright";
import { test, expect, PlaywrightTestConfig } from "@playwright/test";
import Common from "@common/common";

test.beforeAll(async ({ page }) => {

});

test.describe('Go test', () => {
  test('Test1', async ({ page }) => {
    console.log("1111111111");
  });
})

package.json

{
  "name": "type-sanity",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "ts-node -r tsconfig-paths/register src/main.ts",
    "test": "npx playwright test"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/randomstring": "^1.1.7",
    "playwright": "^1.14.0",
    "randomstring": "^1.2.1",
    "typescript": "^4.3.5"
  },
  "devDependencies": {
    "@playwright/test": "^1.14.0",
    "@types/node": "latest",
    "ts-node": "^10.1.0",
    "tsconfig-paths": "^3.10.1",
    "typescript-module-alias": "^1.0.2"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "ES5",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "outDir": "./dist",
    "baseUrl": ".",
    "types": [],
    "paths": {
      "@config/*": [
        "tests/config/*"
      ],
      "@common/*": [
        "src/common/*"
      ],
    }
  },
  "exclude": [
    "node_modules"
  ]
}

Upvotes: 10

Views: 21155

Answers (1)

Max Schmitt
Max Schmitt

Reputation: 3152

@playwright/test does not consider your tsconfig.json when transpiling your TS files (thats why your custom path mapping is not working). You can transpile your TypeScript manually, see here: https://playwright.dev/docs/test-typescript

For further reference see this upstream issue: https://github.com/microsoft/playwright/issues/7121

Upvotes: 8

Related Questions