-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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 src/setupTests.js to specify environment setup for Jest (#545) #548
Conversation
Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at cla@fb.com. Thanks! |
Can you please specify the test plan?
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
Test plan added to the initial PR description:
|
@@ -0,0 +1,4 @@ | |||
/** |
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 we shouldn’t ship this by default. It’s a little bit advanced feature, and it’s fine to just document it. Can you add relevant documentation section to template/README.md
instead?
Removed the sample 'setupTests.js' and added docs to the README.md |
Sorry for the trouble but I just merged another set of changes that made this conflict. |
@@ -671,6 +672,22 @@ import { expect } from 'chai'; | |||
|
|||
and then use them in your tests like you normally do. | |||
|
|||
### Test environment setup |
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.
Let’s call this ### Initializing Test Environment
Fixed doc title and conflicts ✅ |
@@ -674,6 +675,22 @@ import { expect } from 'chai'; | |||
|
|||
and then use them in your tests like you normally do. | |||
|
|||
### Initializing Test 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.
Please add “>Note: this feature is available since react-scripts@0.4.0
” here. You can copy and paste an existing similar notes and change the version.
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.
Done
Awesome. Thank you. |
const config = { | ||
moduleNameMapper: { | ||
'^[./a-zA-Z0-9$_-]+\\.(jpg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm)$': resolve('config/jest/FileStub.js'), | ||
'^[./a-zA-Z0-9$_-]+\\.css$': resolve('config/jest/CSSStub.js') | ||
}, | ||
scriptPreprocessor: resolve('config/jest/transform.js'), | ||
setupFiles: [resolve('config/polyfills.js')], | ||
setupFiles: setupFiles, |
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.
nit: you don't need the : setupFiles
piece here because of object short notation.
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’m not sure Node 4 supports it, does it?
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.
True, wasn't sure of what es6 features I could use there since there are some "var" elsewhere in the code :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.
yes, it does! Node 4 supports let/var as well as object short notation and arrow functions.
Out in 0.4.0. |
Fix #545
Test plan
Run
npm run create-react-app my-app
and in the generatedApp.js
, add something likelocalStorage.getItem('test');
. Runnpm test
, it should fail becauselocalStorage
is undefined. Add asetupTests.js
insrc
that looks like this:Then run
npm test
again (you need to re-run the command in order to have thesetupTests.js
added to Jest config) and it should pass.You can also simply add a
setupTests.js
that does aconsole.log('Hello world')
to check it's properly executed before the tests.