使用 TypeScript 访问 Mocha 中的测试标题

Accessing test title in Mocha with TypeScript

根据 How can I retrieve the current test's name within a Mocha test? 中的建议,可以使用 this.test.title

在 Mocha 中访问测试标题

但是,在 TS 中这样做会导致以下错误:

Property 'title' does not exist on type 'TestFunction'.ts(2339)

我已经安装了 mocha 类型,但没有帮助:

npm install --save @types/mocha

我是 TypeScript 的新手,所以,我可能会遗漏一些东西。你是如何在测试中解决这个问题的?

我的测试如下:

import { Browser, Page } from 'playwright'
const playwright = require('playwright')
const myPage = require('../page-objects/mypage.ts')
const testData = require('../test-data/test-data.json')
let browser: Browser
let page: Page
let myPageCommands
const browserName = 'chromium'

describe('My Page Verification', () => {
    beforeEach(async () => {
        browser = await playwright[browserName].launch({ headless: false })
        page = await browser.newPage()
        myPageCommands = await new myPage(page)
        await myPageCommands .navigateTo('index')
    })

    afterEach(async () => {
        await browser.close()
    })

    it('Check Text Fields', async () => {
        await myPageCommands.fillTextField(testData)
        await myPageCommands.verifyTextField(testData)
        await console.log(this.test.title)
    })
})

这就是它的工作原理:

import { Context } from 'mocha';    

const context: Context = <any>this
        const test = context.currentTest || { state: 'undefined' };
        if (test.state === 'passed' && test.file) {
           
        }