jor*_*gen 5 testing code-coverage v8 nyc playwright
我正在努力使用 Playwright 正确设置报道。它报告所有文件的覆盖率均为 0(测试文件本身除外,如果我包含它们)。
我从https://playwright.dev/docs/api/class-coverage和https://github.com/bgotink/playwright-coverage/blob/main/src/fixtures.ts中获得灵感。我们的项目是一个单一存储库,其中文件夹中的测试e2e-tests/在其他相邻文件夹中包含的服务器上运行端到端测试,例如frontend/.
page当前设置在每个测试文件中使用如下所示的固定装置:
// frontend.spec.ts
import { test, expect } from "../fixtures";
test("something", ({ page ) => {
// Do test stuff with page
});
Run Code Online (Sandbox Code Playgroud)
其中夹具定义为
// fixtures/page.ts
import { Page, TestInfo } from "@playwright/test";
const pageWithCoverage = async (
{ page, browserName }: { page: Page; browserName: string },
use: (page: Page) => Promise<void>,
testInfo: TestInfo
) => {
if (!page.coverage) throw new Error(`Could not collect coverage with browser "${browserName}"`);
console.log(" Collecting coverage");
await page.coverage.startJSCoverage();
await use(page);
await page.coverage.stopJSCoverage();
};
export default pageWithCoverage;
Run Code Online (Sandbox Code Playgroud)
为了收集报道,我跑了
npx nyc --all --cwd ".." --include "**/frontend/**/* --nycrc-path e2e-tests/.nycrc npm t
Run Code Online (Sandbox Code Playgroud)
其中有关文件结构的相关部分是:
--all --cwd ".." --include "**/frontend/**/*"
Run Code Online (Sandbox Code Playgroud)
我正在使用一个.nycrc包含以下内容的文件nyc-config-tsx来检测 tsx 文件:
npx nyc --all --cwd ".." --include "**/frontend/**/* --nycrc-path e2e-tests/.nycrc npm t
Run Code Online (Sandbox Code Playgroud)
你能说出问题是什么吗?
前端是使用 构建的next。
我得到类似的结果,将结果存储到使用v8toIstanbul和运行的文件中npx nyc report
我今天也遇到了同样的问题,几乎是你的问题一周年纪念日。我发现这个问题希望它有答案......
不管怎样,这就是它对我有用的原因。
您奇怪地链接的存储库对我没有任何影响。它仍然没有正确注册您在 .tsx 文件中找到的 jsx 类型代码,因此每个报告都没有经过 .tsx 文件检测nyc。
我发现问题出在该包的这一行,因为如果我将其更改为包含jsx如下内容:
parserPlugins: parserPlugins.concat('typescript', 'jsx')
在node_modules内的文件中,然后它就工作了。
因此,修复方法是在我自己的项目中重新创建该文件,然后在nyc配置文件中扩展它。
// nyc-config.js
'use strict';
const { parserPlugins } = require('@istanbuljs/schema').defaults.nyc;
module.exports = {
cache: false,
parserPlugins: parserPlugins.concat('typescript', 'jsx'),
};
Run Code Online (Sandbox Code Playgroud)
// .nycrc.json
{
"extends": "./nyc-config.js",
"reporter": ["cobertura", "lcov"],
"all": true,
"include": [
"**/*.tsx",
"**/*.ts"
],
"exclude": [
// ...
],
}
Run Code Online (Sandbox Code Playgroud)
之后,nyc能够检测 .tsx 文件并检查它们的覆盖范围。
编辑:
我的项目使用 Vite 来构建,而不是 OP next,并且您运行测试的服务器启用覆盖率跟踪非常重要。
例如,在 中playwright.config.ts,你有
webServer: {
command: 'VITE_COVERAGE=true npx nyc npm run start',
port: 5173,
},
Run Code Online (Sandbox Code Playgroud)
如果您使用的是Vite。
| 归档时间: |
|
| 查看次数: |
2173 次 |
| 最近记录: |