DefinitelyTyped/types/jest-environment-puppeteer/jest-environment-puppeteer-tests.ts
Jeroen Claassens bbe8520b9e fix(jest-environment-puppeteer): Class & Exports (#37647)
* fix(jest-environment-puppeteer): Class & Exports

This makes the typings actually compatible to create your own test
environment and not just as part of the bigger jest-puppeteer package.
How to do this is pretty much what the test covers. One of the reasons
to do this is to write a handler where Puppeteer takes screenshots after
each failed test, as discussed [here](https://github.com/smooth-code/jest-puppeteer/issues/131)

* Change to `export =` syntax

Co-Authored-By: Pranav Senthilnathan <pranav.senthilnathan@live.com>

* Fix the last stuff requested
2019-08-19 14:55:15 -07:00

43 lines
1.4 KiB
TypeScript

import { Browser, Page, BrowserContext } from 'puppeteer';
import JestEnvironmentPuppeteer from 'jest-environment-puppeteer';
import { Config, Circus } from '@jest/types';
import { Script } from 'vm';
const myBrowser: Browser = browser; // $ExpectType Browser
const myPage: Page = page; // $ExpectType Page
const myContext: BrowserContext = context; // $ExpectType BrowserContext
jestPuppeteer.debug();
jestPuppeteer.resetPage();
// Creating a custom Jest environment
class CustomJestEnvironment extends JestEnvironmentPuppeteer {
constructor(config: Config.ProjectConfig) {
super(config);
}
async setup() {
await super.setup();
await this.global.page.goto('https://www.google.com');
}
async teardown() {
await this.global.page.waitFor(2000);
await super.teardown();
}
runScript(script: Script) {
return super.runScript(script);
}
async handleTestEvent(event: Circus.Event, state: Circus.State) {
if (event.name === 'test_fn_failure') {
console.error('woaw your test failed, you should feel bad!');
}
}
}
type JestEnvironmentPuppeteerGlobal = JestEnvironmentPuppeteer['global']; // $ExpectType Global
type JestEnvironmentPuppeteerGlobalPuppeteer = JestEnvironmentPuppeteer['global']['jestPuppeteer']; // $ExpectType JestPuppeteer
type JestEnvironmentPuppeteerFakeTimers = JestEnvironmentPuppeteer['fakeTimers']; // $ExpectType FakeTimers<Timer> | null