在所有Jest测试中重用相同的Puppeteer实例 [英] Reusing same Puppeteer instance in all Jest tests

查看:122
本文介绍了在所有Jest测试中重用相同的Puppeteer实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Jest + Puppeteer替换了CasperJS.将所有内容都放在一个文件中效果很好:

I'm replacing CasperJS with Jest + Puppeteer. Putting everything in one file works great:

beforeAll(async () => {
  // get `page` and `browser` instances from puppeteer
});

describe('Test A', () => {
   // testing
});

describe('Test B', () => {
   // testing
});

afterAll(async () => {
 // close the browser
});

现在,我真的不想将所有内容都保存在一个文件中.仅一部分测试(例如,只是测试A")很难维护,也很难运行.

Now, I don't really want to keep everything in one file. It's harder to maintain and harder to run just part of the tests (say, just 'Test A').

我查看了Jest文档,并了解了 setupScript .这将是完美的,但它会在每个测试文件之前运行.我不希望这样,因为操纵up的设置会花费很多时间.无论我要运行多少个测试文件,我都想重复使用同一浏览器实例并只支付一次安装费用.

I've looked at Jest docs and read about setupScript. It would be perfect, but it runs before every test file. I don't want this because puppeteer setup takes quite a lot of time. I want to reuse same browser instance and pay the setup cost only once no matter how many test files I'll run.

所以,我想到了:

// setup puppeteer
await require('testA')(page, browser, config);
await require('testB')(page, browser, config);
// cleanup

这解决了模块化问题,重复使用了相同的浏览器实例,但不允许我单独运行测试.

This solves modularization, reuses same browser instance, but doesn't allow me to run tests separately.

最后,我偶然发现了创建自定义 testEnviroment的可能性.听起来不错,但没有充分记录,因此我什至不确定是否按测试文件或Jest运行创建env实例.稳定的API也是缺少setup方法的,我可以在其中设置puppeteer(我必须在不能异步的构造函数中执行此操作).

Finally, I stumbled upon possibility to create a custom testEnviroment. This sounds great but it isn't well documented, so I'm not even sure if env instance is created per test file, or per Jest run. Stable API is also a missing a setup method where I could set up puppeteer (I'd have to do that in constructor that can't be async).

由于我是Jest的新手,所以我可能会缺少一些明显的东西.在深入研究之前,我会在这里问.

Since I'm new to Jest I might be missing something obvious. Before I dig deeper into this I though I'll ask here.

推荐答案

更新(2018年2月):Jest现在有官方的

UPDATE (Feb 2018): Jest now have official Puppeteer guide, featuring reusing one browser instance across all tests :)

它已经在Twitter上得到 的回答,但为了清楚起见,请在此处发布.

It was already answered on Twitter, but let's post it here for clarity.

自Jest v22开始,您可以创建自定义测试环境是异步的,并且具有setup()/teardown()钩子:

Since Jest v22 you can create a custom test environment which is async and has setup()/teardown() hooks:

import NodeEnvironment from 'jest-environment-node';

class CustomEnvironment extends NodeEnvironment {
  async setup() {
    await super.setup();
    await setupPuppeteer();
  }

  async teardown() {
    await teardownPuppeteer();
    await super.teardown();
  }
}

并在您的Jest配置中使用它:

And use it in your Jest configuration:

{
  "testEnvironment": "path/to/CustomEnvironment.js"
}

值得注意的是,Jest在沙箱中并行测试(单独的vm上下文),并且需要为每个工作人员产生新的测试环境(通常是计算机的CPU内核数).

It's worth to note, that Jest parallelizes tests in sandboxes (separate vm contexts) and needs to spawn new test environment for every worker (so usually the number of CPU cores of your machine).

这篇关于在所有Jest测试中重用相同的Puppeteer实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆