Skip to content
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

Pass Jest done callback to testMethod #3853

Merged

Conversation

hisapy
Copy link
Contributor

@hisapy hisapy commented Jul 8, 2018

Issue: NO ISSUE

What I did

I added the Jest done callback to be passed to the testMethod in StoryShot addon. Also updated the addon's README.

How to test

Is this testable with Jest or Chromatic screenshots?
I don't think so

Does this need a new example in the kitchen sink apps?
I don't think so

Does this need an update to the documentation?
I updated the StoryShot addon README with usage example.

If your answer is yes to any of these, please make sure to include it in your PR.

@stale stale bot added inactive and removed inactive labels Aug 11, 2018
@storybookjs storybookjs deleted a comment from stale bot Aug 24, 2018
@hisapy hisapy force-pushed the pass-done-callback-to-testMethod branch 2 times, most recently from 1b24a89 to f489d48 Compare September 4, 2018 20:04
@hisapy
Copy link
Contributor Author

hisapy commented Sep 4, 2018

Hello again ... I finally made some time to update and finish this PR. Added tests, updated to use the new API of the getSnapshotFilename.

@hisapy
Copy link
Contributor Author

hisapy commented Sep 4, 2018

So far the only check failing is the one at Better Code Hub ... I tried to access it but it says only organization members can access the code ...

@hisapy
Copy link
Contributor Author

hisapy commented Sep 4, 2018

I'm going to rename the option to avoid conflicts with async keyword

@hisapy hisapy force-pushed the pass-done-callback-to-testMethod branch from f489d48 to ae450a3 Compare September 4, 2018 20:26
@codecov
Copy link

codecov bot commented Sep 4, 2018

Codecov Report

Merging #3853 into master will increase coverage by 0.15%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3853      +/-   ##
==========================================
+ Coverage   40.59%   40.74%   +0.15%     
==========================================
  Files         483      484       +1     
  Lines        5747     5760      +13     
  Branches      770      771       +1     
==========================================
+ Hits         2333     2347      +14     
+ Misses       3042     3041       -1     
  Partials      372      372
Impacted Files Coverage Δ
addons/storyshots/storyshots-core/src/api/index.js 82.35% <ø> (ø) ⬆️
...s/storyshots-core/src/api/ensureOptionsDefaults.js 100% <ø> (+10%) ⬆️
.../storyshots-core/src/api/snapshotsTestsTemplate.js 88.88% <100%> (+2.22%) ⬆️
...ore/stories/required_with_context/Async.stories.js 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c6de989...917d745. Read the comment docs.

@hisapy hisapy force-pushed the pass-done-callback-to-testMethod branch from ae450a3 to 917d745 Compare September 4, 2018 23:04
@hisapy
Copy link
Contributor Author

hisapy commented Sep 5, 2018

@igor-dv can you take a look at this again please ?

@ndelangen
Copy link
Member

Don't worry about Better Code Hub

@ndelangen
Copy link
Member

Let's get @igor-dv's approval too

Copy link
Member

@igor-dv igor-dv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we call it just async?


// finally mark test as done
done();
}, TIMEOUT);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be flacky


### StoryShots for async rendered components

You can make use of [Jest done callback](https://jestjs.io/docs/en/asynchronous) to test components that render asynchronously. This callback is passed as param to test method passed to `initStoryshots(...)` when the `asyncJest` option is given as true.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this parameter should be listed in the Options sections also

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just updated this PR with changes you requested @igor-dv .

By the way, I've used just async first but then I've opted for asyncJest to avoid issues with syntax highlightings that were assuming it was the async ES keyword

})


// file: StoryShots.test.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please separate each example file to a its own block

@hisapy hisapy force-pushed the pass-done-callback-to-testMethod branch from 917d745 to 721131f Compare September 16, 2018 19:03
Copy link
Member

@igor-dv igor-dv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Can you please merge from master, to pass CI?

@hisapy hisapy force-pushed the pass-done-callback-to-testMethod branch from 721131f to bb76bfd Compare September 18, 2018 11:59
@hisapy
Copy link
Contributor Author

hisapy commented Sep 18, 2018

@igor-dv the branch is now up-to-date ... the CircleCI is apparently failing because Slack notficaion failed. No active hooks

@igor-dv igor-dv merged commit 825baf4 into storybookjs:master Sep 20, 2018
@hisapy
Copy link
Contributor Author

hisapy commented Sep 20, 2018

Thx for accepting this PR ladies and gentlemen

@hisapy hisapy deleted the pass-done-callback-to-testMethod branch September 20, 2018 13:52
@SimenB
Copy link
Contributor

SimenB commented Sep 28, 2018

Note that done is mostly for legacy reason in Jest, we recommend to return a promise instead. Mostly because if an assertion fails, done is never called unless you're careful with try-catches etc. Using new Promise(resolveCb => {}) is less of a footgun.

Not saying that you shouldn't expose a done - there are definitely valid use cases, but the docs makes it seem as the callback is the only way if you need to do async work

@hisapy
Copy link
Contributor Author

hisapy commented Sep 28, 2018

Thx and wow! I haven't heard of done being legacy until now ... I'll try to investigate about the promise approach and create a new PR to address async components with it ASAP.

@SimenB
Copy link
Contributor

SimenB commented Sep 28, 2018

"legacy" might be too strong. It's probably never going away because of old code bases, and it's a nice way to programatically fail a test. But most of the time doing return Promise.reject(new Error('fail test')) is better, and async-await makes working with promises quite nice. So we definitely wanna encourage promises over a done callback.

If you work with a callback API I would still use promises, maybe like this:

test('some api with cb pattern', async () => {
  const result = await new Promise((resolve, reject) => {
    callApiWithCallback((err, res) => {
      if (err) {
        reject(err);
      } else {
        resolve(res);
      }
    });
  });

  expect(result).toBeSomething();
});

Or use something like https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original.

So yeah, done is perfectly valid, but we should try to encourage people to use promises instead. At least mention it in the docs section added in this PR

@hisapy
Copy link
Contributor Author

hisapy commented Sep 28, 2018

I'd like to submit a PR to mention about using promise instead of done() but I need to do my homework about how to use promise to test async rendered components... currently the ones I've been testing don't have a render or afterRender callback... I just wait 1 ms so the component is updated and then start the assertions based on enzyme's mount and jest. Perhaps it should be as easy as rendering the component inside the awaiting Promise but I'm not so sure ...

Anyway, for people that might be worry, I can tell you that you still get a full test run with failing assertions and done().

@SimenB
Copy link
Contributor

SimenB commented Sep 28, 2018

You can wait a millisecond using await new Promise(resolve => setTimeout(resolve, 1));. As long as a promise is returned (which using an async function does implicitly), Jest will wait for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants