-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Add example to testEnvironment in Configuration.md #4690
Conversation
Can you explain how this example code actually works in practice? How does puppeteer use the vm or run individual scripts? |
wouldn't that take too much space? class CustomEnvironment extends NodeEnvironment {
constructor(config) {
super(config);
}
async setup() {
await super.setup();
await someSetupTask();
}
async teardown() {
await someTeardownTask();
await super.teardown();
}
runScript(script) {
return super.runScript(script);
}
} |
The amount of space it needs doesn't matter to me so much, we should make sure the docs are easy to understand. We can also link to a longer example. |
In my case, I'm using it to launch/close the puppeteer browser instance. |
4994335
to
0c32e84
Compare
@xfumihiro, I'm also trying to use the same puppeteer browser across all tests, but the example you've provided doesn't do that (as best I can tell). The example you showed in #4506 seems to work just like |
@rzane you're right. Every script setup/teardown their own environment. More work need to be done to get this working. 😞 |
Just published a npm package to do this. |
Yeah, that seems like it will work, but I was surprised to find that jest doesn't have a |
Maybe it's better this way for sandboxing each test. For puppeteer use case, I think this is better than having Jest to take care the browser lifecycle. |
Yeah, what you're saying makes sense, and you've definitely come up with a clever solution. But, the benefit of using a Jest environment is that you can switch it on a per-test basis. For example, I have unit tests the run really quickly and don't require a browser, then, I have integration tests that need a browser and need the server to be started (Phoenix in this case, so it's not node). So, for my integration tests, I can just add: /**
* jest-environment ./path/to/my/env
*/ This way, if I don't run any integration tests, I can skip the browser/web server boot times. |
From my understanding, the testEnvironment is sandboxed. |
From looking at the custom Love jest though, thanks for the great project. |
We are also playing with Puppeteer+Jest combo and I've run into the same issue. Cost of starting the browser is very high (we moved tests from CasperJS and run times went up 2x), so we want to reuse instances. What we do ATM to solve this is we keep our own pool of browsers: const NodeEnvironment = require('jest-environment-node');
const puppeteer = require('puppeteer');
const debug = Boolean(process.env.debug);
const browserPool = [];
async function setupPuppeter() {
let browserSettings = {};
if (debug) {
browserSettings = {
headless: false,
slowMo: 25
};
}
return puppeteer.launch(browserSettings);
}
class PuppeteerEnvironment extends NodeEnvironment {
async setup() {
super.setup();
let browser, page;
if (browserPool.length) {
const item = browserPool.pop();
browser = item.browser;
page = item.page;
} else {
browser = await setupPuppeter();
page = await browser.newPage();
}
this._browser = browser;
this._page = page;
// expose browser and page to tests
this.context.browser = browser;
this.context.page = page;
}
async teardown() {
browserPool.push({
browser: this._browser,
page: this._page
});
await super.teardown();
}
}
module.exports = PuppeteerEnvironment; However, this requires us to use If someone has any ideas how to improve this, I'd love to hear them! [EDIT] Oh! |
@kdzwinel how about spawning just one browser before class init and just inject it into the env, will that work? |
@thymikee We want multiple browsers so that they run on separate cores in parallel. I like your idea with finding the last |
Oh damn, I've never given it deeper thoughts and had been under impression that test environment spins once per worker, but instead it's set up for every test 😱 (which totally makes sense). Sorry about that. There's no way currently to determine number of tests inside environment. I don't think I'll come up with something not requiring |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
No description provided.