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

[refactor] Migrate from Mocha+Chai to Jest #6079

Merged
merged 8 commits into from
Oct 15, 2018

Conversation

xtinec
Copy link
Contributor

@xtinec xtinec commented Oct 11, 2018

This change migrates all the existing unit tests

  • to Jest's global expect and matchers from chai's imported expect, asserts and matchers.
  • to Jest's describe/test from mocha's describe/it

The majority of the mechanical changes to tests are achieved through running jest-codemods. The only two note-worthy manual tweaks:

  1. Setting a testURL of http://localhost in jest config and adjusting a few tests to leverage this value instead of relying on about:blank.
  2. Re-enabling ExploreChartPanel_spec which was previously commented out as we cannot have empty tests with nothing in them with Jest. :)

This change also removes dependencies to Mocha and Chai.

@williaster @kristw @mistercrunch @betodealmeida

"test:one": "mocha --require ignore-styles --compilers js:babel-core/register --require spec/helpers/shim.js",
"tdd": "jest --watch",
"test": "jest",
"test:one": "jest",
Copy link
Contributor

Choose a reason for hiding this comment

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

should we remove this one since it's the same as test? I added test:one for mocha just a couple months ago.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, we can.

@@ -19,40 +18,40 @@ describe('AddSliceContainer', () => {
wrapper = shallow(<AddSliceContainer {...defaultProps} />);
});

it('uses table as default visType', () => {
expect(wrapper.state().visType).to.equal('table');
test('uses table as default visType', () => {
Copy link
Contributor

@williaster williaster Oct 12, 2018

Choose a reason for hiding this comment

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

it might be useful to also add the eslint-plugin-jest while adding jest for consistent styles from the start. for example within describe blocks it recommends using it over test. (that one is auto-fixable so shouldn't be much work 😄 )

I noticed a mix in the file changes here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Took a quick look at eslint-plugin-jest. I like the rules it advocates. Good suggestion!

Let me add it to this PR so we don't change it to test and the back to it. 😛

@@ -46,9 +45,6 @@ global.XMLHttpRequest = global.window.XMLHttpRequest;

global.sinon = require('sinon');
Copy link
Contributor

@williaster williaster Oct 12, 2018

Choose a reason for hiding this comment

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

In a future PR I think we can actually also remove sinon because jest has support for spies, stubs, etc. I doubt that's code-mod-able, tho.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally agree. I didn't do it in the same PR to limit the scope. :) Yeah, for sinon, we need to modify things manually.

Copy link
Contributor

@williaster williaster left a comment

Choose a reason for hiding this comment

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

LGTM! thanks again for doing this, it's amazing 🙌

sorry the cypress build is broken, I introduced a flaky test, will fix now.

@xtinec
Copy link
Contributor Author

xtinec commented Oct 12, 2018

Looks like I have some linting errors to fix as well. :) On it.

@kristw
Copy link
Contributor

kristw commented Oct 12, 2018

Ah. It seems like you have to deal with Istanbul as well.
npm run cover fails. That command is still calling mocha under the hood.
Perhaps time to get rid of babel-istanbul (deprecated) and substitute with babel-plugin-istanbul
https://github.com/istanbuljs/babel-plugin-istanbul

Rooting for you. This PR is gold 🥇

@williaster
Copy link
Contributor

williaster commented Oct 12, 2018

@kristw I agree we should remove babel-istanbul but do we need babel-plugin-istanbul?

jest computes coverage on its own, so we could change the coverage script to simply be
"cover": "jest --coverage"

note this is compatible with our current setup for coveralls, I mirrored this in superset-ui which uses jest

This change migrates all the existing unit tests
- to Jest's global expect and matchers from chai's imported expect, asserts and matchers.
- to Jest's describe/test from mocha's describe/it

The majority of the mechanical changes to tests are achieved through running jest-codemods. The only two note-worthy manual tweaks:
1. Setting a testURL of http://localhost in jest config and adjusting a few tests to leverage this value instead of relying on about:blank.
2. Re-enabling ExploreChartPanel_spec which was previously commented out as we cannot have empty tests with nothing in it with Jest. :)

This change also removes dependencies to Mocha and Chai.
…arn run lint --fix`

The only noteworthy change is the one in eslintrc for tests. The env has been updated from mocha to jest.
- One small fix in sqllab's Timer Spec for a test that is not using the spy it created for testing.
- Deletion of a duplicated test caught by eslint-plugin-jest.
- Remove dependency on stand-alone istanbul and babel-istanbul as they're built-into jest. Yes!
@kristw
Copy link
Contributor

kristw commented Oct 12, 2018

@williaster That is even better. No need for istanbul then.

@xtinec
Copy link
Contributor Author

xtinec commented Oct 12, 2018

@kristw @williaster yep, that's exactly what I did as well. "cover": "jest --coverage"
I added a config to jest to only compute coverage for files under src.

@xtinec xtinec force-pushed the migrate-from-mocha-to-jest branch 2 times, most recently from be6e39b to 1df4883 Compare October 13, 2018 01:40
@codecov-io
Copy link

codecov-io commented Oct 13, 2018

Codecov Report

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

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #6079   +/-   ##
=======================================
  Coverage   77.56%   77.56%           
=======================================
  Files          47       47           
  Lines        9484     9484           
=======================================
  Hits         7356     7356           
  Misses       2128     2128

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 e1e8778...1dfe55f. Read the comment docs.

… codecov

- remove dynamic import in shim.js now that it is set in babelrc for tests only.
Copy link
Contributor

@williaster williaster left a comment

Choose a reason for hiding this comment

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

🔥

@williaster williaster merged commit 9029701 into apache:master Oct 15, 2018
bipinsoniguavus pushed a commit to ThalesGroup/incubator-superset that referenced this pull request Dec 26, 2018
* [refactor] Migrate from Mocha+Chai to Jest

This change migrates all the existing unit tests
- to Jest's global expect and matchers from chai's imported expect, asserts and matchers.
- to Jest's describe/test from mocha's describe/it

The majority of the mechanical changes to tests are achieved through running jest-codemods. The only two note-worthy manual tweaks:
1. Setting a testURL of http://localhost in jest config and adjusting a few tests to leverage this value instead of relying on about:blank.
2. Re-enabling ExploreChartPanel_spec which was previously commented out as we cannot have empty tests with nothing in it with Jest. :)

This change also removes dependencies to Mocha and Chai.

* Remove the test:one command as it now does the same thing as test.

* Fixing lint errors. The diff looks large but is large done through `yarn run lint --fix`

The only noteworthy change is the one in eslintrc for tests. The env has been updated from mocha to jest.

* Adding eslint-plugin-jest and further modify tests.

- One small fix in sqllab's Timer Spec for a test that is not using the spy it created for testing.
- Deletion of a duplicated test caught by eslint-plugin-jest.

* - Make istanbul coverage work with Jest.

- Remove dependency on stand-alone istanbul and babel-istanbul as they're built-into jest. Yes!

* Attempt to fix dynamic imports in tests.

* run sequentially and log heap usage

* - tweaking maxworkers for travis and specifying coverageDirectory for codecov

- remove dynamic import in shim.js now that it is set in babelrc for tests only.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 0.28.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants