TypeScript + Playwright Error: Cannot find module

TypeScript + Playwright Error: Cannot find module

我 运行 与剧作家一起解决编码测试自动化问题。 运行测试时, test.spec.ts 处出现以下错误:

错误:找不到模块“@common/common” 代码:'MODULE_NOT_FOUND'

如何解决这个问题?

下面有代码

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"
  ]
}

@playwright/test 在转译您的 TS 文件时不考虑您的 tsconfig.json(这就是为什么您的自定义 path 映射不起作用)。您可以手动转译 TypeScript,请参阅此处:https://playwright.dev/docs/test-typescript

如需进一步参考,请参阅此上游问题:https://github.com/microsoft/playwright/issues/7121