Increase Jest unit test coverage for the Swaps feature to ~43%#10934
Increase Jest unit test coverage for the Swaps feature to ~43%#10934brad-decker merged 12 commits intoMetaMask:developfrom dan437:swaps-jest-tests-v2
Conversation
|
CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes. |
ui/app/pages/swaps/index.test.js
Outdated
There was a problem hiding this comment.
This is a great way to wait until a mocked API call is done.
ui/app/__mocks__/react-router-dom.js
Outdated
There was a problem hiding this comment.
presumably when we expand test coverage in our other areas of the repo we'll need to mock this out in those files to override the pathname?
There was a problem hiding this comment.
That's right. My assumption is that most of the tests will be fine with this path, but if they need to use a different one, they can just do this:
jest.mock('react-router-dom', () => {
const originalModule = jest.requireActual('react-router-dom');
return {
...originalModule,
useHistory: jest.fn(),
useLocation: jest.fn(() => {
return {
pathname: '/feature-z/page-1',
};
}),
}
});
There was a problem hiding this comment.
Is it possible for us to test what the URL will go to here instead of just checking the link text? It'd help to ensure that changes made to the URL but not the text don't pass through our test cases.
There was a problem hiding this comment.
Great point, will add an assertion for a URL.
There was a problem hiding this comment.
I've checked the code and we don't render a link, but open a new tab via an onClick event on a div element:
onClick={() => global.platform.openTab({ url: blockExplorerUrl })}
I will be testing events in following PRs, so I would prefer to do it there.
There was a problem hiding this comment.
I am assuming this snapshot exists just to confirm that the loading-swaps-quotes-stories-metadata file isn't touched, or if it is that it is intentional?
There was a problem hiding this comment.
Exactly. If there would be any change in loading-swaps-quotes-stories-metadata.js, a test would fail with an outdated snapshot. It's just a double-check that someone didn't accidentally modify it + it was showing 0% test coverage, which was lowering our overall coverage.
.eslintrc.js
Outdated
There was a problem hiding this comment.
Why did this get turned off?
There was a problem hiding this comment.
Great question! That's because I used module.exports in the link below: https://github.com/MetaMask/metamask-extension/pull/10934/files#diff-8fd96322fc664541ebc0326e3c5963e55db825809abb977c6ffad8be7555cbd1R7
After which I started getting these errors:

I will give it little more time to use export instead of module.exports when re-exporting imported things. Then we hopefully wouldn't need to turn import/named off for tests.
There was a problem hiding this comment.
So, I've changed it to export only:
export { createSwapsMockStore } from './mock-store';
export { renderWithProvider } from './rendering';
export { setBackgroundConnection } from './background';
export * as MOCKS from './mocks';
export * as CONSTANTS from './constants';
But even while tests are green, I'm getting the same ESLint issue when re-exporting:

That's why I would prefer to keep 'import/named': 'off',, just for tests.
There was a problem hiding this comment.
looks legit. import-js/eslint-plugin-import#1998
package.json
Outdated
There was a problem hiding this comment.
why the removal of the collectCoverageFrom flag
There was a problem hiding this comment.
Explanation
This PR increases Swaps coverage from 25% to about 43%, which is getting us one step closer to 90% coverage.
Manual testing steps
npm run test:coverage:jestScreenshots
Swaps Jest unit test coverage is getting greener (43.12% for Lines coverage):
