-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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 passing options to react-test-renderer .create() method #896
Conversation
@shilman - reopening storybook-eol/storyshots#76 here |
b083f2a
to
f27db8b
Compare
@jrdrg thanks for doing this work! Is there anything I can do to help get this merged? :) I've been trying to run this branch locally but not having any luck with installing dependencies (using the latest versions of |
@JaKXz I'm seeing this when running the tests from the storybook root dir after doing an install via lerna, which seems to be why the build is failing.
If I |
I wanted to get a PR in for this issue, but I can't run tests in the StoryShots package in the mono repo, so I made a working PR for the deprecated StoryShots repo instead: |
0437617
to
072f7ed
Compare
Codecov Report
@@ Coverage Diff @@
## master #896 +/- ##
=======================================
Coverage ? 0%
=======================================
Files ? 2
Lines ? 16
Branches ? 3
=======================================
Hits ? 0
Misses ? 13
Partials ? 3
Continue to review full report at Codecov.
|
@JaKXz @ndelangen @theinterned I fixed up the stuff that was preventing the tests from passing, looks like the build succeeded this time |
.jestrc
Outdated
@@ -3,7 +3,8 @@ | |||
"clearMocks": true, | |||
"moduleNameMapper": { | |||
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js", | |||
"\\.(css|scss)$": "<rootDir>/__mocks__/styleMock.js" | |||
"\\.(css|scss)$": "<rootDir>/__mocks__/styleMock.js", | |||
"@kadira/storybook-addons": "<rootDir>/packages/addons/dist/index.js" |
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.
What's this for?
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 was running into this issue storybook-eol/storybook-addons#1 when trying to run yarn test
from the storybook root directory. So I added some console.logs while debugging and determined that this constructor https://github.com/storybooks/storybook/blob/master/packages/addons/src/index.js#L2 was getting called 3 times during the storyshots test, when it seemed to me it should be a singleton. Running the tests in the package itself (i.e. not using lerna and doing a yarn install directly from /packages/storyshots) worked fine, so I figured it might have something to do with jest resolving the symlinks so adding this line makes sure that all references to storybook-addons use the same absolute path.
packages/storyshots/.jestrc
Outdated
@@ -0,0 +1,20 @@ | |||
{ |
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 are there 2 pretty much identical .jestrc files?
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 second .jestrc file is for the test-examples
script. Running yarn test
uses the .jestrc in the root directory, but yarn test-examples
uses lerna to run npm test in each package's directory. If I used "test": "jest --config ../../.jestrc"
in the storyshots directory to reference the jestrc in the root, I ran into this error:
lerna ERR! test Errored while running script in 'storyshots'
lerna ERR! execute Error: Command failed: npm run test
lerna ERR! execute ● Validation Error:
lerna ERR! execute
lerna ERR! execute Module ./node_modules/jest-enzyme/lib/index.js in the setupTestFrameworkScriptFile option was not found.
lerna ERR! execute
lerna ERR! execute Configuration Documentation:
lerna ERR! execute https://facebook.github.io/jest/docs/configuration.html
The rootDir
is also different for yarn test
and yarn test-examples
, so I copied the .jestrc from the root dir here and made a few minor changes to the paths so I could do "test": "jest --config ./.jestrc"
in the storyshots package.json.
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.
@ndelangen is there a better way to handle this?
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 Jest v20 takes care of a lot of the multi-project test running 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.
Looks like test-examples
was removed, so I removed this extra .jestrc
packages/storyshots/src/index.js
Outdated
@@ -14,10 +14,13 @@ const babel = require('babel-core'); | |||
const pkg = readPkgUp.sync().pkg; | |||
const isStorybook = | |||
(pkg.devDependencies && pkg.devDependencies['@kadira/storybook']) || |
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.
Looks like, this could be extracted into a function?
8bd1551
to
93bfda3
Compare
9fa5a1d
to
01f480a
Compare
ad6fdf2
to
bd7b71f
Compare
e276422
to
7156191
Compare
Ouch this is taking long, sorry!, I can see you're taking the effort to get this merged. |
See for instance #896 -- it's a common problem to need mocked nodes. This solution is probably a bit of a bandaid, as large projects would probably want to set options per-story, this is a lot better than nothing in the meantime.
See for instance #896 -- it's a common problem to need mocked nodes. This solution is probably a bit of a bandaid, as large projects would probably want to set options per-story, this is a lot better than nothing in the meantime.
Issue:
When running storyshots with a story for a component that uses refs, it will throw a null reference error (see facebook/react#7371)
What I did
This PR adds the ability to pass options to the react-test-renderer create method, for example to allow mocking refs as per https://facebook.github.io/react/blog/2016/11/16/react-v15.4.0.html#mocking-refs-for-snapshot-testing
How to test
Run
yarn test
-ComponentWithRef.stories.js
should use thecreateNodeMock
function defined instoryshots.test.js
to mock its ref and not fail with a null reference error. The story forComponentWithRef
attempts to get the scrollWidth of a div when the component mounts.