-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: upgrade Jest to automatically polyfill the testing environment …
…in order to use helper libraries for testing web components
- Loading branch information
Showing
3 changed files
with
75 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const { | ||
CustomElementRegistry, | ||
CustomEvent, | ||
Document, | ||
Event, | ||
Node, | ||
Text, | ||
HTMLElement, | ||
HTMLTemplateElement, | ||
HTMLUnknownElement, | ||
} = require('basichtml'); | ||
|
||
const NodeEnvironment = require('jest-environment-node'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const puppeteer = require('puppeteer'); | ||
const os = require('os'); | ||
|
||
const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup'); | ||
|
||
class BasicHTMLEnvironment extends NodeEnvironment { | ||
constructor(config) { | ||
super(config); | ||
|
||
const window = this.global; | ||
|
||
window.customElements = new CustomElementRegistry(); | ||
window.document = new Document(window.customElements); | ||
window.document = new Document(window.customElements); | ||
window.HTMLElement = HTMLElement; | ||
window.HTMLUnknownElement = HTMLUnknownElement; | ||
window.Node = Node; | ||
window.Text = Text; | ||
window.window = window; | ||
|
||
this.global.navigator = { | ||
userAgent: 'node.js', | ||
}; | ||
|
||
// Ensure that the real global env has the document | ||
global.document = window.document; | ||
} | ||
|
||
async setup() { | ||
await super.setup(); | ||
// get the wsEndpoint | ||
const wsEndpoint = fs.readFileSync(path.join(DIR, 'wsEndpoint'), 'utf8'); | ||
if (!wsEndpoint) { | ||
throw new Error('wsEndpoint not found'); | ||
} | ||
|
||
// connect to puppeteer | ||
this.global.__BROWSER__ = await puppeteer.connect({ | ||
browserWSEndpoint: wsEndpoint, | ||
}); | ||
} | ||
|
||
runScript(script) { | ||
return super.runScript(script); | ||
} | ||
|
||
async teardown() { | ||
await super.teardown(); | ||
|
||
this.global.document = null; | ||
this.global.window = null; | ||
global.document = null; | ||
} | ||
} | ||
|
||
module.exports = BasicHTMLEnvironment; |
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
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