Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(jest-environment-puppeteer): Class & Exports #37647

Merged
merged 3 commits into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 45 additions & 3 deletions types/jest-environment-puppeteer/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
// Project: https://github.com/smooth-code/jest-puppeteer/tree/master/packages/jest-environment-puppeteer
// Definitions by: Josh Goldberg <https://github.com/joshuakgoldberg>
// Ifiok Jr. <https://github.com/ifiokjr>
// Jeroen Claassens <https://github.com/favna>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
// TypeScript Version: 3.4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the version change? This would be a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would be a breaking change

Hardly, if this extremely small set of breaking changes made by TS v3.0 is an issue then you should update your code base rather than resist upgrading.

But to answer your question, before I bumped it the tests failed, something to do with the imports the new tests and typings have to use.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not always reasonable to ask consumers to upgrade TypeScript versions for their projects especially if it's not necessary. In the case of this PR I think the esModuleInterop, which is only used for the tests, is causing the requirement for a higher version. Please remove this flag and use import JestEnvironmentPuppeteer = require('jest-environment-puppeteer') instead.

Copy link
Contributor Author

@favna favna Aug 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@PranavSenthilnathan I tried bumping it back down to 2.8 and removing esModuleInterop and I got the errors again, it comes from the jest-mock library. Here is the error it throws:

1> jest-environment-puppeteer failing:
1> Error: Errors in typescript@next for external dependencies:
1> node_modules/@jest/environment/build/index.d.ts(11,8): error TS1259: Module '"E:/dev/DefinitelyTyped/types/jest-environment-puppeteer/node_modules/jest-mock/build/index"' can only be default-imported using the 'esModuleInterop' flag
1> node_modules/@jest/source-map/build/getCallsite.d.ts(8,100): error TS2503: Cannot find namespace 'callsites'.
1>
1>     at E:\dev\DefinitelyTyped\node_modules\dtslint\bin\index.js:190:19
1>     at Generator.next (<anonymous>)
1>     at fulfilled (E:\dev\DefinitelyTyped\node_modules\dtslint\bin\index.js:5:58)

Going back to either 3.0 or 3.4 without esModuleInterop still has it fail, so instead I tried version 2.8 with esModuleInterop and I ended up with another error, once again due to external dependencies, this is a long one so I dumped it on gist: https://gist.github.com/Favna/fb37f696dcc390fa593cedcf8050751b.

Next thing I tried was TypeScript v3.0 with esModuleInterop which again threw an error:

=== ERRORS ===
Error in jest-environment-puppeteer
Error: Errors in typescript@3.0 for external dependencies:
node_modules/jest-mock/build/index.d.ts(125,162): error TS2304: Cannot find name 'Parameters'.

    at E:\dev\DefinitelyTyped\node_modules\dtslint\bin\index.js:190:19
    at Generator.next (<anonymous>)
    at fulfilled (E:\dev\DefinitelyTyped\node_modules\dtslint\bin\index.js:5:58)
Error: The following packages had errors: jest-environment-puppeteer
    at doRunTests (E:\dev\DefinitelyTyped\node_modules\types-publisher\src\tester\test-runner.ts:235:11)
    at processTicksAndRejections (internal/process/task_queues.js:86:5)
error Command failed with exit code 1.

Ultimately I ended up going for TypeScript version 3.4 in 53b225a after all and with esModuleInterop, but I maintained the requested export = and import .. = require('...'). I also added a few extra ExpectType tests because the latter means you cannot directly import the interfaces and I'm told I can't have other export statements when usin export =.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its unfortunate that the packages you are depending on are using features from typescript 3+.


import { JestEnvironment } from '@jest/environment';
import { JestFakeTimers as FakeTimers } from '@jest/fake-timers';
import { Circus, Global as GlobalType, Config } from '@jest/types';
import { ModuleMocker } from 'jest-mock';
import { Browser, Page, BrowserContext } from 'puppeteer';
import { Script, Context } from 'vm';

interface JestPuppeteer {
export interface JestPuppeteer {
/**
* Reset global.page
*
Expand Down Expand Up @@ -44,11 +50,47 @@ interface JestPuppeteer {
debug(): Promise<void>;
}

export interface Timer {
id: number;
ref: () => Timer;
unref: () => Timer;
}

export interface Global extends GlobalType.Global {
browser: Browser;
context: Context;
page: Page;
jestPuppeteer: JestPuppeteer;
}

/** Note: TestEnvironment is sandboxed. Each test suite will trigger setup/teardown in their own TestEnvironment. */
declare class PuppeteerEnvironment implements JestEnvironment {
context: Context | null;
fakeTimers: FakeTimers<Timer> | null;
global: Global;
moduleMocker: ModuleMocker | null;
constructor(config: Config.ProjectConfig);

/**
* Setup runs when the environment is being spun up, generally before each test suite
* You should always call `await super.setup()` in here
*/
setup(): Promise<void>;

/**
* Teardowns runs as the environment is being torn down, generally after each test suite.
* You should always call `await super.tearDown()` in here
*/
teardown(): Promise<void>;
runScript(script: Script): any;
handleTestEvent?(event: Circus.Event, state: Circus.State): void;
}

declare global {
const browser: Browser;
const context: BrowserContext;
const page: Page;
const jestPuppeteer: JestPuppeteer;
}

export {};
export default PuppeteerEnvironment;
favna marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
import * as puppeteer from "puppeteer";
import { Browser, Page, BrowserContext } from 'puppeteer';
import JestEnvironmentPuppeteer from 'jest-environment-puppeteer';
import { Config, Circus } from '@jest/types';
import { Script } from 'vm';

const myBrowser: puppeteer.Browser = browser;
const myPage: puppeteer.Page = page;
const myContext: puppeteer.BrowserContext = context;
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!');
}
}
}
9 changes: 9 additions & 0 deletions types/jest-environment-puppeteer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"private": true,
"dependencies": {
"@jest/environment": "^24",
"@jest/fake-timers": "^24",
"@jest/types": "^24",
"jest-mock": "^24"
}
}
3 changes: 2 additions & 1 deletion types/jest-environment-puppeteer/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true
},
"files": [
"index.d.ts",
Expand Down