TypeScript +剧作家错误: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

相关内容

最新更新