-
-
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
Allow setting a test environment per file #2859
Conversation
packages/jest-cli/src/runTest.js
Outdated
} | ||
} | ||
|
||
/* $FlowFixMe */ |
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.
https://github.com/facebook/jest/pull/2859/files?w=1 is recommended
@@ -48,6 +49,7 @@ const readRawConfig = (argv, root) => { | |||
}; | |||
|
|||
module.exports = { | |||
getTestEnvironment, |
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.
Is this OK?
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.
yep!
Codecov Report
@@ Coverage Diff @@
## master #2859 +/- ##
==========================================
- Coverage 67.12% 67.07% -0.06%
==========================================
Files 142 142
Lines 5126 5136 +10
==========================================
+ Hits 3441 3445 +4
- Misses 1685 1691 +6
Continue to review full report at Codecov.
|
@cpojer Added a test to this, should be ready for review |
docs/Configuration.md
Outdated
* @testEnvironment jsdom | ||
*/ | ||
|
||
test('use document in a mostly node environment', () => { |
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.
Should be "use jsdom in this test file"
docs/Configuration.md
Outdated
*/ | ||
|
||
test('use document in a mostly node environment', () => { | ||
const ele = document.createElement('div'); |
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.
element
!
const testFixturePackage = require('../test-environment/package.json'); | ||
|
||
it('respects testEnvironment pragma', () => { | ||
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.
I don't think you really need to check this. Can you remove this line?
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.
It's there to prevent a false positive if the package.json is changed in the future. You don't think that's necessary?
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.
Yeah, good point actually. Let's keep it then!
*/ | ||
|
||
test('stub', () => { | ||
const ele = document.createElement('div'); |
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.
element!
|
||
/** | ||
* @testEnvironment jsdom | ||
*/ |
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.
The file should look like this:
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @testEnvironment jsdom
*/
/* eslint-env browser*/
packages/jest-cli/src/runTest.js
Outdated
const { | ||
Console, | ||
NullConsole, | ||
setGlobal, | ||
} = require('jest-util'); | ||
const getConsoleOutput = require('./reporters/getConsoleOutput'); | ||
|
||
const testEnvironmentPragmaRegex = /@testEnvironment\s+(.*)/; |
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.
There is a docblock parser in jest-haste-map. Please let's use that instead. Feel free to extract it into a separate package "jest-docblock".
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.
Also, I was thinking we could call this @jest-environment
instead. That way we can add more of these in the future with the same prefix :)
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.
Ah, nice! Will do
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.
Didn't see the comment about @jest-environment
(no idea how I missed it), will push a change to it
packages/jest-cli/src/runTest.js
Outdated
const BufferedConsole = require('./lib/BufferedConsole'); | ||
const promisify = require('./lib/promisify'); | ||
const { | ||
Console, | ||
NullConsole, | ||
setGlobal, | ||
} = require('jest-util'); | ||
const getConsoleOutput = require('./reporters/getConsoleOutput'); |
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.
Please keep the requires consistent. Here is how it should work with Jest:
const Capitalized = require('MyClass');
const {getTestEnvironment} = require('jest-config');
const lowercase = require('myfunction');
We have this weird spacing rule at FB, let's keep that for now :)
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 think it's correct now...
packages/jest-cli/src/runTest.js
Outdated
const TestRunner = require(config.testRunner); | ||
/* $FlowFixMe */ | ||
const ModuleLoader = require(config.moduleLoader || 'jest-runtime'); | ||
return promisify(fs.readFile)(path, 'utf8').then(data => { |
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 switch this to fs.readFileSync
?
I'm a bit worried about the fact that this will end up reading every test file twice and we could potentially change things around by passing a fake FS into ModuleLoader
that has a cache, like this:
new ModuleLoader({
…,
cacheFS: {
[path]: source,
},
});
and then later pass that source to transform
inside of jest-runtime
so that we don't read the same file twice. What do you think?
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.
Why sync? The function already returns a promise.
But yes, I agree the file should only have to be read once, I'll see what I can do 😄
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.
Because there is no other work that can be done in Jest at the same time, so there is no benefit in using async code for reading the file :)
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 looks pretty good! I left a few comments inline – would you mind making another iteration?
@cpojer Pushed with the changes requested except for the reading of the source file twice. From what I can see in the code, I can |
f4b2b7b
to
a19e8a6
Compare
packages/jest-docblock/package.json
Outdated
@@ -0,0 +1,10 @@ | |||
{ | |||
"name": "jest-docblock", | |||
"version": "19.0.0", |
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 set the version here (and where it is used) to 19.0.2? :)
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.
Will do!
packages/jest-cli/src/runTest.js
Outdated
data = fs.readFileSync(path, 'utf8'); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} |
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.
nice!
packages/jest-cli/src/runTest.js
Outdated
return Promise.reject(e); | ||
} | ||
let testEnvironment = config.testEnvironment; | ||
|
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.
code style: can you remove this newline and move the const above the let? :)
packages/jest-cli/src/runTest.js
Outdated
|
||
const fs = require('fs'); | ||
const docblock = require('jest-docblock'); | ||
const {getTestEnvironment} = require('jest-config'); |
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 think this one goes on top of all the requires (because of the {
)
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.
You should add eslint-plugin-imports on this stuff 😄
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.
Would love to use a lint plugin to enforce the current rules we have. Mind sending a separate PR for that? :)
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 can give it a try, at least. No promises
packages/jest-cli/src/runTest.js
Outdated
} | ||
let testEnvironment = config.testEnvironment; | ||
|
||
const testEnvironmentDocblock = docblock.parse(docblock.extract(data)); |
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 just do:
const customEnvironment = docblock.parse(docblock.extract(data))['jest-environment']
and then re-use that? We only need that one prop :)
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 had it that way, but your way is more than 80 chars wide...
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.
const parsedDocblock = docblock.parse(docblock.extract(data));
const customJestEnvironment = parsedDocblock['jest-environment'];
?
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.
Or add a method to docblock, extractAndParse
? I'm guessing they're separate for testing purposes, you wouldn't use one without the other in "real" code, would you?
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 like the code example up there, maybe just call it customEnvironment
instead? :)
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.
So
const parsedDocblock = docblock.parse(docblock.extract(testSource));
const customEnvironment = parsedDocblock['jest-environment'];
?
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.
Yeah that seems reasonable! :)
Nice! I guess we could make the change to jest-rutnime in a follow-up. The idea was to change jest-runtime to accept a |
Runtime changes in a separate commit makes sense. |
Not a todo, but maybe an issue here that links to this PR :) |
@cpojer OK, everything addressed except for reading the file twice (I can open up an issue for it) |
Really solid work @SimenB. Thank you! |
@SimenB Thanks for doing the work! I'm excited to try it out soon. :) |
* Extract docblock into its own module * Allow setting a test environment per file Fixes jestjs#2587
* Extract docblock into its own module * Allow setting a test environment per file Fixes jestjs#2587
I have a component that does some fancy work with image processing and canvases that requires images to load in the test. For this, I'm setting Would it be possible to somehow use this per-file test environment specification for |
@jcksncllwy you can create another |
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
Fixes #2587. This is WIP, opening a PR now to get feedback on direction.
Test plan
Source:
Test:
Command run without these changes:
Command run with these changes: