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

Expect to jest transformer #39

Merged
merged 27 commits into from
Jul 18, 2017
Merged

Expect to jest transformer #39

merged 27 commits into from
Jul 18, 2017

Conversation

skovhus
Copy link
Owner

@skovhus skovhus commented Mar 16, 2017

@mjackson https://www.npmjs.com/package/expect is being integrated into Jest. This codemod automates the upgrade path. It is extending the work started in cpojer/js-codemod#66

Background: jestjs/jest#1679

/cc @cpojer

Step 1

Step 2

Bugs

  • console.error does't have calls property when using jest spies:
  it('warns when passed a history prop', () => {
    const history = {}
    const node = document.createElement('div')
    expect.spyOn(console, 'error')

    ReactDOM.render((
      <MemoryRouter history={history} />
    ), node)

    expect(console.error.calls.length).toBe(1)
    expect(console.error.calls[0].arguments[0]).toMatch(
      /<MemoryRouter.*import \{ Router }/)
    expect.restoreSpies()
  })

@skovhus skovhus changed the title Initial version of expect to jest transformer Expect to jest transformer Mar 16, 2017
@mjackson
Copy link

Thank you, @skovhus :) How can I try this out? I'm not familiar with how to run a code mod. Is there a doc somewhere?

@skovhus
Copy link
Owner Author

skovhus commented Mar 16, 2017

Hi @mjackson.

I'll update you when the codemod is ready. But this repo has a little CLI that should make it fairly easy to run on an existing codebase. It wraps jscodeshift. See https://github.com/skovhus/jest-codemods#usage-cli

But I might try the codemod out on some of your repos with expect. ; )

@skovhus skovhus force-pushed the expect branch 4 times, most recently from 16f3779 to 032ef66 Compare March 16, 2017 22:05
@skovhus
Copy link
Owner Author

skovhus commented Mar 16, 2017

@mjackson for projects like React History and React Router it seems that you guys are using Expect+Karma+Mocha to execute your tests in Chrome or other browsers.

As Jest is executed in node only, these projects might not be good candidates for testing this codemod. Unless if you are interested in switching to a node only test environment? Let me know what you think : )

@cpojer
Copy link

cpojer commented Mar 17, 2017

This is fantastic.

@skovhus The new version of expect must definitely also be made to work on the web. So this codemod should enable people to switch from the previous version of expect to the new version. One way to try that out would be to run the codemod on react history/react router and then add jest-matchers and change const expect = require('jest-matchers') and make sure that works in the browser.

We may have to provide a single-file build for jest-matchers, that's still to be decided.

The idea is to ship this breaking change sometime in mid April.

@skovhus
Copy link
Owner Author

skovhus commented Mar 17, 2017

Thanks for the input! So essentially two uses cases: for upgrading expect and for upgrading to use Jest as test runner.

The output of these two cases can be made compatible if we leave the jest-matchers input in there.

So if we import expect from jest-matchers even for users just running on node... that might make sense, as we don't need two codemods or configuration.

@cpojer
Copy link

cpojer commented Mar 17, 2017

Hmm, here would be my recommendation:

  • Transform with Jest:
  • expect.spyOn => jest.spyOn, expect.spy => jest.fn
  • require('expect') gets removed.
  • Transform without Jest:
  • expect.spyOn => mock.spyOn, expect.spy => mock.fn by including jest-mock.

The module jest-matches will be renamed to expect, so the require statement does not need to changed. Inside of Jest, we don't need to require it because it is globally available.

@skovhus
Copy link
Owner Author

skovhus commented Mar 17, 2017

Two codemods (or configuration) it is. 👍🏻

@cpojer
Copy link

cpojer commented Mar 17, 2017

btw. this plugin should help insert requires for the second one: https://github.com/cpojer/js-codemod/tree/master/extensions/imports

@skovhus skovhus force-pushed the expect branch 2 times, most recently from f98db17 to ce6be63 Compare March 21, 2017 22:06
@skovhus skovhus force-pushed the expect branch 4 times, most recently from e32e2cb to 409030f Compare April 17, 2017 21:42
@brantphoto
Copy link

hi @skovhus I'm excited to use this codemod. We'll be using this exact scenario to move to Jest at my work. Thanks for making it!

@skovhus
Copy link
Owner Author

skovhus commented Apr 17, 2017

@brantphoto so happy to hear that! : )

I'll let you know when you can try it out. 👍

@skovhus
Copy link
Owner Author

skovhus commented Apr 24, 2017

Standalone mode is a bit blocked on jestjs/jest#3360

@cpojer
Copy link

cpojer commented Apr 28, 2017

Hey Kenneth! What's the status of this PR? Are you going to merge it? Except for the blocker inside of Jest, is there anything else left that we need to do?

@skovhus
Copy link
Owner Author

skovhus commented Apr 28, 2017

Nope. Planning to merge it when the standalone mode works for React Router.

Everything else should be fine. 😀

@skovhus
Copy link
Owner Author

skovhus commented Jun 28, 2017

@cpojer is the plan still to rename jest-matchers to expect (and release as version 2)?

Do we want to do that before this codemod goes out? Or should we just change this codemod to migrating from expect@1 to jest-matchers?

@cpojer
Copy link

cpojer commented Jun 28, 2017

We'll publish expect@20 or @21, yes. I'm not sure when yet, but that's the plan.

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

Successfully merging this pull request may close these issues.

4 participants