4

我遇到了剧作家编码测试自动化的问题。运行测试时,test.spec.ts出现如下错误:

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

如何解决这个问题呢?

下面有代码

测试规范.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");
  });
})

包.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"
  ]
}
4

1 回答 1

2

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

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

于 2021-09-06T10:47:12.860 回答