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

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
Copy link
Contributor

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
Copy link
Contributor

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
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 feature/8543-docs-jest-mock-api branch from 12ad487 to 29b3928 Compare June 10, 2019 19:50
@sargalias sargalias marked this pull request as ready for review June 10, 2019 20:18
Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

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:
Copy link
Member

Choose a reason for hiding this comment

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

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 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I think so. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pleasure!

@SimenB SimenB requested review from jeysal and thymikee June 11, 2019 17:58
@@ -0,0 +1,626 @@
---
id: version-24.6-jest-object
Copy link
Member

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 have to add a new one - not sure though!

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 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.

Copy link
Member

Choose a reason for hiding this comment

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

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

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.
@github-actions
Copy link

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants