-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
sheetalkamat
merged 3 commits into
DefinitelyTyped:master
from
favna:upgrades/jest-environment-puppeteer
Aug 19, 2019
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 38 additions & 4 deletions
42
types/jest-environment-puppeteer/jest-environment-puppeteer-tests.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,42 @@ | ||
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!'); | ||
} | ||
} | ||
} | ||
|
||
type JestEnvironmentPuppeteerGlobal = JestEnvironmentPuppeteer['global']; // $ExpectType Global | ||
type JestEnvironmentPuppeteerGlobalPuppeteer = JestEnvironmentPuppeteer['global']['jestPuppeteer']; // $ExpectType JestPuppeteer | ||
type JestEnvironmentPuppeteerFakeTimers = JestEnvironmentPuppeteer['fakeTimers']; // $ExpectType FakeTimers<Timer> | null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
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 useimport JestEnvironmentPuppeteer = require('jest-environment-puppeteer')
instead.There was a problem hiding this comment.
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:Going back to either 3.0 or 3.4 without
esModuleInterop
still has it fail, so instead I tried version 2.8 withesModuleInterop
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:Ultimately I ended up going for TypeScript version 3.4 in 53b225a after all and with
esModuleInterop
, but I maintained the requestedexport =
andimport .. = 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 otherexport
statements when usinexport =
.There was a problem hiding this comment.
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+.