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

docs(jest-mock): Add example for ES6 modules #8550

Merged
merged 6 commits into from Jun 12, 2019

Conversation

@sargalias
Copy link
Contributor

sargalias commented Jun 10, 2019

Issue #8543

Summary

Add an example on the jest.mock API docs for using the factory parameter for ES6 modules with default exports.

Motivation

I haven't found information on how to do this in the Jest documentation. I believe this information should be available.

The issue is that compared to mocking out CommonJS modules, ES6 modules require the esModule: true property to be present.

Test plan

This is purely a documentation change, so I have not touched the tests for the functional code in Jest.

Other

A similar test to the example code passes tests.

image

If I'm missing anything please let me know and I will fix it. In particular I wasn't sure what to do with the docs / versioned docs. Currently I have just edited the docs/JestObjectAPI.md file).

@facebook-github-bot

This comment has been minimized.

Copy link

facebook-github-bot commented Jun 10, 2019

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. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

@facebook-github-bot

This comment has been minimized.

Copy link

facebook-github-bot commented Jun 10, 2019

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@codecov-io

This comment has been minimized.

Copy link

codecov-io commented Jun 10, 2019

Codecov Report

Merging #8550 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #8550   +/-   ##
=======================================
  Coverage   60.57%   60.57%           
=======================================
  Files         269      269           
  Lines       11054    11054           
  Branches     2696     2696           
=======================================
  Hits         6696     6696           
  Misses       3772     3772           
  Partials      586      586

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 9ac8ca7...91ba3e7. Read the comment docs.

@sargalias sargalias force-pushed the sargalias:feature/8543-docs-jest-mock-api branch from 12ad487 to 29b3928 Jun 10, 2019
@sargalias sargalias marked this pull request as ready for review Jun 10, 2019
Copy link
Collaborator

SimenB left a comment

Thanks! I just left a small nit, this LGTM!

Also, can you please update the versioned versions of this document?

@@ -240,6 +240,23 @@ const moduleName = require('../moduleName');
moduleName(); // Will return '42';
```

When using the `factory` parameter for an ES6 module with a default export, the `__esModule: true` property needs to be specified:

This comment has been minimized.

Copy link
@SimenB

SimenB Jun 10, 2019

Collaborator

Can you add something about how both Babel's and TypeScript's module transform looks for this property? Without that context, it might look really weird, I think 🙂

This comment has been minimized.

Copy link
@sargalias

sargalias Jun 11, 2019

Author Contributor

Actually had to research that :). Does something like that work?

This comment has been minimized.

Copy link
@SimenB

SimenB Jun 11, 2019

Collaborator

Yeah, I think so. Thanks!

This comment has been minimized.

Copy link
@sargalias

sargalias Jun 11, 2019

Author Contributor

Pleasure!

@SimenB SimenB requested review from jeysal and thymikee Jun 11, 2019
@SimenB
SimenB approved these changes Jun 11, 2019
@@ -0,0 +1,626 @@
---
id: version-24.6-jest-object

This comment has been minimized.

Copy link
@SimenB

SimenB Jun 11, 2019

Collaborator

I don't think you have to add a new one - not sure though!

This comment has been minimized.

Copy link
@sargalias

sargalias Jun 11, 2019

Author Contributor

I wasn't sure either, but the other files in versioned_docs do so I added it just in case :). I'm happy to change it if it's not ideal.

This comment has been minimized.

Copy link
@SimenB

SimenB Jun 11, 2019

Collaborator

Every PR is deployed, so you can remove this and check the deployed site on netlify

https://deploy-preview-8550--jest-preview.netlify.com/

If the versioned doc is incorrect, you can re-add this

This comment has been minimized.

Copy link
@thymikee

thymikee Jun 12, 2019

Collaborator

Looks like what you need to do is edit 22.x, 23.x and 24.0 versions of JestObjectAPI, separate 24.6 is not necessary

This comment has been minimized.

Copy link
@sargalias

sargalias Jun 12, 2019

Author Contributor

Ah that was a great idea. Editing the previous versions added the example there too which is still relevant for those versions. 👍

I also removed the file JestObjectAPI from the versioned_docs for 24.6, as it's not needed anymore like you mentioned.

I tested locally (with yarn start) and the changes are appearing for all versions.

sargalias added 2 commits Jun 12, 2019
This reverts commit ba17c08.

Updating the versioned_docs for version 24.6 is not necessary as we can
update an earlier version and the changes will propagate to the new
version.
The example for using __esModule: true is also relevant for those
versions, so it should be present.
@thymikee thymikee changed the title docs(JestObjectAPI jest-mock): Add example for ES6 modules docs(jest-mock): Add example for ES6 modules Jun 12, 2019
@thymikee thymikee merged commit 3748557 into facebook:master Jun 12, 2019
11 checks passed
11 checks passed
ci/circleci: lint-and-typecheck Your tests passed on CircleCI!
Details
ci/circleci: test-browser Your tests passed on CircleCI!
Details
ci/circleci: test-jest-circus Your tests passed on CircleCI!
Details
ci/circleci: test-node-10 Your tests passed on CircleCI!
Details
ci/circleci: test-node-11 Your tests passed on CircleCI!
Details
ci/circleci: test-node-6 Your tests passed on CircleCI!
Details
ci/circleci: test-node-8 Your tests passed on CircleCI!
Details
ci/circleci: test-or-deploy-website Your tests passed on CircleCI!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
deploy/netlify Deploy preview ready!
Details
facebook.jest #20190612.2 succeeded
Details
aliaksandr-yermalayeu added a commit to aliaksandr-yermalayeu/jest that referenced this pull request Jul 16, 2019
* docs(JestObjectAPI jest-mock): Add example for ES6 modules

Issue facebook#8543

* chore(CHANGELOG): Update CHANGELOG

* docs(JestObjectAPI jest-mock): Add explanation for __esModule: true

Issue facebook#8543

* docs(JestObjectAPI jest-mock): Update versioned_docs

Issue facebook#8543

* Revert "docs(JestObjectAPI jest-mock): Update versioned_docs"

This reverts commit ba17c08.

Updating the versioned_docs for version 24.6 is not necessary as we can
update an earlier version and the changes will propagate to the new
version.

* docs(JestObjectAPI jest-mock): Update previous versioned_docs

The example for using __esModule: true is also relevant for those
versions, so it should be present.
This was referenced Oct 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.