-
-
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 Async Test Environment APIs #4506
Conversation
packages/jest-runner/src/run_test.js
Outdated
@@ -100,7 +100,11 @@ export default function runTest( | |||
mapCoverage: globalConfig.mapCoverage, | |||
}); | |||
const start = Date.now(); | |||
return testFramework(globalConfig, config, environment, runtime, path) | |||
return (environment.setup ? environment.setup() : (async () => ({}))()) |
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.
return (environment.setup ? environment.setup() : Promise.resolve({}))
can be used 2 other places as well
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.
Thanks! TIL :)
a49cb57
to
e7ea47c
Compare
packages/jest-runner/src/run_test.js
Outdated
if (globalConfig.logHeapUsage) { | ||
if (global.gc) { | ||
global.gc(); | ||
Promise.resolve() |
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.
Could we instead use the async
modifier on the promise callback and use await
? I think that would be tons clearer for this code!
c9989c9
to
f7e61f9
Compare
Codecov Report
@@ Coverage Diff @@
## master #4506 +/- ##
==========================================
- Coverage 57.13% 57.11% -0.03%
==========================================
Files 195 194 -1
Lines 6569 6568 -1
Branches 3 3
==========================================
- Hits 3753 3751 -2
- Misses 2816 2817 +1
Continue to review full report at Codecov.
|
@cpojer any thing to improve? |
packages/jest-runner/src/run_test.js
Outdated
.then((result: TestResult) => { | ||
return (environment.setup | ||
? environment.setup() | ||
: Promise.resolve({})).then(async testSetup => { |
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.
This is really hard to read. Can we not use a ternary here? Thanks!
packages/jest-runner/src/run_test.js
Outdated
throw err; | ||
}), | ||
); | ||
if (environment.teardown) await environment.teardown(); |
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.
this needs curly braces, wondering why CI didn't catch that :O
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.
packages/jest-runner/src/run_test.js
Outdated
// Delay the resolution to allow log messages to be output. | ||
return new Promise(resolve => setImmediate(() => resolve(result))); | ||
} catch (err) { | ||
if (environment.teardown) await environment.teardown(); |
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.
curly braces as well pls
f7e61f9
to
2645683
Compare
@cpojer any thing else? |
packages/jest-runner/src/run_test.js
Outdated
return testFramework(globalConfig, config, environment, runtime, path) | ||
.then((result: TestResult) => { | ||
return environment.setup().then(async testSetup => { | ||
setGlobal(environment.global, 'setup', testSetup); |
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.
This shouldn't be here, we don't want to expose this function on the global object.
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.
For some use case(i.e, the Test Plan of this PR) it's necessary to set global variables in the setup function for later usage.
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.
maybe
return environment.setup().then(async setupGlobal => {
setGlobal(environment.global, 'setup', setupGlobal);
would be better
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.
I prefer not to do this. The change in this PR should not automatically add things to the global scope. The setup function in the environment has access to the global scope and we can leave it to the responsibility of the user.
types/Environment.js
Outdated
@@ -31,6 +31,8 @@ declare class $JestEnvironment { | |||
}, | |||
testFilePath: string, | |||
moduleMocker: ModuleMocker, | |||
setup(): Promise<Global>, |
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.
Can you change the signature to Promise? I don't think we need to return the global here.
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.
Two more changes to clean up this feature please. It would also be great if you could add an integration test for this, with a custom test framework.
5fb8947
to
8a50998
Compare
@cpojer can you rebulid the failing ci-tests? It should be working. |
@xfumihiro rebuild didn't help. Have you tested on node 4 and/or 6? Node 8 passes CI, so if you've just tested on latest node locally that might explain it. |
wouldn't you want runScript to be async as well? for the puppeteer example you would want scripts to run in the browser (page.evaluate) which is async. |
@@ -14,10 +14,9 @@ const runJest = require('../runJest'); | |||
const testFixturePackage = require('../test-environment/package.json'); | |||
|
|||
it('respects testEnvironment docblock', () => { | |||
expect(testFixturePackage.jest.testEnvironment).toEqual('node'); |
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.
can you create a new test instead of modifying an existing one?
@devsnek not sure if this should be an issue as I've ran puppeteer tests in my own app successfully. |
49bee97
to
71fda69
Compare
@xfumihiro right but it would be cooler to just do that at the environment level, allowing a better test flow for future environments whatever they might be. you could also make it so runScript takes a string intead of vm.Script, for even more flexibility, although that might be out of the scope of this pr. |
bfd8559
to
bae1802
Compare
bae1802
to
93d9640
Compare
result.memoryUsage = process.memoryUsage().heapUsed; | ||
} | ||
// Delay the resolution to allow log messages to be output. | ||
return new Promise(resolve => setImmediate(() => resolve(result))); |
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.
this can probably be
await new Promise(resolve => setImmediate(resolve));
return result;
No idea if it affects the stacktraces of matchers, though.
Feel free to ignore!
Thank you. |
Published 21.3.0-beta.2. |
This is the last piece of the puzzle , we can now integrate Jest support in Detox! |
Great PR 👍 |
It still hasn't released to stable version, then why it is documented? http://facebook.github.io/jest/docs/en/configuration.html#testenvironment-string class CustomEnvironment extends NodeEnvironment {
constructor(config) {
super(config);
}
async setup() {
await super.setup();
await someSetupTasks();
}
async teardown() {
await someTeardownTasks();
await super.teardown();
}
runScript(script) {
return super.runScript(script);
}
} You guys should've merge document's change after releasing it. |
It should include a note that's it's not available until 21.3.0. PR welcome! |
Done #4765 |
I'm experimenting with an async environment (great PR! 👍), but apparently it doesn't run on Node v6:
Will async environments be limited to versions of Node supporting async/await? |
Just do |
Doh. Of course, makes sense. Thanks! |
I'm facing some issues with this async environment. But can't determine if the hooks are properly working.
require('ts-node/register'); // cz, the below required file is written in typescript
const resetDB = require('./reset-database');
module.exports = function() {
return resetDB(); // returns Promise
};
/* I tried running the below code to verify this file is working. It is working */
// resetDB()
// .then(() => console.log('ok'))
// .catch(err => console.log(err));
const NodeEnvironment = require('jest-environment-node');
const resetDatabase = require('./reset');
class CustomEnvironment extends NodeEnvironment {
constructor(config) {
super(config);
}
async setup() {
await super.setup();
console.log('setup running') // not prinitng
await resetDatabase();
}
async teardown() {
await resetDatabase();
await super.teardown();
}
runScript(script) {
return super.runScript(script);
}
}
module.exports = CustomEnvironment; these hooks are not 'properly' working for me. I can't see any consoles or database resets either. How do I debug this? Is it a bug?
"jest": {
"globals": {
"ts-jest": {
"tsConfigFile": "tsconfig.json"
}
},
"moduleFileExtensions": [
"ts",
"js"
],
"transform": {
"^.+\\.(ts)$": "ts-jest"
},
"testMatch": [
"**/tests/**/*.spec.ts"
],
"testEnvironment": "<path to jest-env.js>" // <--
} |
Are you using the beta of jest? |
Hello, first of all, thanks for the great PR! I want to ask a question, do
When the tests are running, these |
@imballinst Jest's test environments are "sandboxed" so this is normal. |
@xfumihiro alright, thanks man! |
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. |
Summary
Adding Async Test Environment APIs based on @cpojer's comments on #3420 & #3832
Fixes #3420
Test plan
Example Puppeteer Environment