Запустите кукловода перед тестами и передайте страницу тестам - PullRequest
0 голосов
/ 08 мая 2019

Я хочу запустить экземпляр кукловода в 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()
  }
}
...