PuppeteerEnvironment.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // eslint-disable-next-line
  2. const NodeEnvironment = require('jest-environment-node');
  3. const getBrowser = require('./getBrowser');
  4. class PuppeteerEnvironment extends NodeEnvironment {
  5. // Jest is not available here, so we have to reverse engineer
  6. // the setTimeout function, see https://github.com/facebook/jest/blob/v23.1.0/packages/jest-runtime/src/index.js#L823
  7. setTimeout(timeout) {
  8. if (this.global.jasmine) {
  9. // eslint-disable-next-line no-underscore-dangle
  10. this.global.jasmine.DEFAULT_TIMEOUT_INTERVAL = timeout;
  11. } else {
  12. this.global[Symbol.for('TEST_TIMEOUT_SYMBOL')] = timeout;
  13. }
  14. }
  15. async setup() {
  16. const browser = await getBrowser();
  17. const page = await browser.newPage();
  18. this.global.browser = browser;
  19. this.global.page = page;
  20. }
  21. async teardown() {
  22. const { page, browser } = this.global;
  23. if (page) {
  24. await page.close();
  25. }
  26. if (browser) {
  27. await browser.disconnect();
  28. }
  29. if (browser) {
  30. await browser.close();
  31. }
  32. }
  33. }
  34. module.exports = PuppeteerEnvironment;