我想在所有测试之前在 ava 中启动一个伪造的实例并将页面传递给所有测试,因此我可以使用该活动的伪造的实例。我正在使用TypeScript并遵循以下指南:
问题是,我不了解如何在它们之间传递这些东西。 Ava给我一个TypeScript错误,这听起来很合理,但我不知道如何构建逻辑以使其正常工作:
test/current.spec.ts:41:50 - error TS2345: Argument of type '(t: TestExecutionContext, bp: number, expected: string) => Promise<void>' is not assignable to parameter of type 'OneOrMoreMacros<any, {}>'
currrent.spec.ts:
import path from 'path'
import test, { ExecutionContext } from 'ava'
import runBrowserWithPage from './_withPage'
import { Page } from 'puppeteer'
interface TestExecutionContext extends ExecutionContext {
context: {
page: Page
}
}
const url = 'file://' + path.resolve(__dirname, '../docs/index.html')
const widths = [575, 576, 768, 992, 1200]
test.before(runBrowserWithPage, async (t: TestExecutionContext, page: Page) => {
await page.goto(url, { waitUntil: 'load' })
t.context.page = page
})
// test.beforeEach(async ({ context }: TestExecutionContext) => {
// await context.page.reload()
// })
async function currentMacro(t: TestExecutionContext, bp: number, expected: string) {
await t.context.page.setViewport({ width: bp, height: 800 })
await t.context.page.screenshot({ path: `assets/${bp}.png` })
const current: any = await t.context.page.evaluate(() => {
// @ts-ignore
const Breakpoint = new Boobreaks()
return Breakpoint.current() // return breakpoint alias
})
t.is(current, expected)
}
// test('Boobreaks should be loaded', async (t: TestExecutionContext) => {
// })
test('Boobreaks should return the alias for xs', currentMacro, widths[0], 'xs')
_withPage.ts
import puppeteer from 'puppeteer'
export default async function withPage(t: any, run: any) {
const browser = await puppeteer.launch()
const page = await browser.newPage()
try {
await run(t, page)
} finally {
await page.close()
await browser.close()
}
}