-
Notifications
You must be signed in to change notification settings - Fork 29
[Breaking] Drop Enzyme support from Jest preset and make it opt-in #2175
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'merchant-center-application-template-starter': patch | ||
'@commercetools-frontend/sdk': patch | ||
--- | ||
|
||
Remove `enzyme` from tests and dependencies. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
--- | ||
'@commercetools-frontend/jest-preset-mc-app': major | ||
--- | ||
|
||
We [strongly recommend and believe](https://docs.commercetools.com/custom-applications/development/testing) to use React Testing Library for testing your Custom Application and therefore we are finally dropping support for `Enzyme`. | ||
|
||
You can still use Enzyme as an opt-in preset following the instructions below: | ||
|
||
### Enzyme 16 | ||
|
||
1. Install the necessary dependencies: | ||
|
||
``` | ||
yarn add \ | ||
@commercetools/enzyme-extensions \ | ||
@commercetools/jest-enzyme-matchers \ | ||
enzyme \ | ||
enzyme-adapter-react-16 \ | ||
enzyme-matchers \ | ||
enzyme-to-json \ | ||
jest-enzyme | ||
``` | ||
|
||
2. In your Jest config, instead of importing the preset `@commercetools-frontend/jest-preset-mc-app` you need to import `@commercetools-frontend/jest-preset-mc-app/enzyme/jest-preset-for-enzyme-16.js` | ||
|
||
For example: | ||
|
||
```js | ||
const jestPreset = require('@commercetools-frontend/jest-preset-mc-app'); | ||
const jestPresetForEnzyme16 = require('@commercetools-frontend/jest-preset-mc-app/enzyme/jest-preset-for-enzyme-16'); | ||
|
||
module.exports = { | ||
...jestPreset, | ||
...jestPresetForEnzyme16, | ||
}; | ||
``` | ||
|
||
### Enzyme 17 | ||
|
||
1. Install the necessary dependencies: | ||
|
||
``` | ||
yarn add \ | ||
@commercetools/enzyme-extensions \ | ||
@commercetools/jest-enzyme-matchers \ | ||
@wojtekmaj/enzyme-adapter-react-17 \ | ||
enzyme \ | ||
enzyme-matchers \ | ||
enzyme-to-json \ | ||
jest-enzyme | ||
``` | ||
|
||
2. In your Jest config, instead of importing the preset `@commercetools-frontend/jest-preset-mc-app` you need to import `@commercetools-frontend/jest-preset-mc-app/enzyme/jest-preset-for-enzyme-17.js` | ||
|
||
For example: | ||
|
||
```js | ||
const jestPreset = require('@commercetools-frontend/jest-preset-mc-app'); | ||
const jestPresetForEnzyme17 = require('@commercetools-frontend/jest-preset-mc-app/enzyme/jest-preset-for-enzyme-17'); | ||
|
||
module.exports = { | ||
...jestPreset, | ||
...jestPresetForEnzyme17, | ||
}; | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,5 @@ | ||
declare namespace jest { | ||
interface Matchers<R, T> { | ||
toHaveTextContent(selector: string): R; | ||
// The following matchers are part of https://www.npmjs.com/package/@commercetools/jest-enzyme-matchers | ||
toHaveProp(selector: string, expected?: unknown): R; | ||
toRender(selector: string): R; | ||
toRender(component: React.ComponentType): R; | ||
toHaveState(selector: string, expected: unknown): R; | ||
// The following matchers are part of https://www.npmjs.com/package/jest-dom | ||
toHaveText(selector: string): R; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/jest-preset-mc-app/enzyme/base-setup-test-framework-for-enzyme.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require('jest-enzyme'); | ||
|
||
const ShallowWrapper = require('enzyme/ShallowWrapper'); | ||
const configureEnzymeExtensions = require('@commercetools/enzyme-extensions'); | ||
const commerceToolsEnzymeMatchers = require('@commercetools/jest-enzyme-matchers'); | ||
|
||
expect.extend({ | ||
toBeComponentWithName(received, actual) { | ||
const pass = received && received.displayName === actual; | ||
const message = () => | ||
`expected ${received} ${pass ? 'not ' : ''} to be ${actual}`; | ||
return { message, pass }; | ||
}, | ||
}); | ||
|
||
expect.extend(commerceToolsEnzymeMatchers); | ||
configureEnzymeExtensions(ShallowWrapper); |
16 changes: 16 additions & 0 deletions
16
packages/jest-preset-mc-app/enzyme/create-jest-preset-for-enzyme.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const path = require('path'); | ||
const defaultPreset = require('../jest-preset'); | ||
|
||
const resolveRelativePath = (relativePath) => | ||
path.resolve(__dirname, relativePath); | ||
|
||
const createJestPresetForEnzyme = (version) => ({ | ||
...defaultPreset, | ||
snapshotSerializers: ['enzyme-to-json/serializer'], | ||
setupFilesAfterEnv: [ | ||
resolveRelativePath('../setup-test-framework.js'), | ||
resolveRelativePath(`./setup-test-framework-for-enzyme-${version}.js`), | ||
], | ||
}); | ||
Comment on lines
+7
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice abstraction |
||
|
||
module.exports = createJestPresetForEnzyme; |
3 changes: 3 additions & 0 deletions
3
packages/jest-preset-mc-app/enzyme/jest-preset-for-enzyme-16.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const createJestPresetForEnzyme = require('./create-jest-preset-for-enzyme'); | ||
|
||
module.exports = createJestPresetForEnzyme(17); |
3 changes: 3 additions & 0 deletions
3
packages/jest-preset-mc-app/enzyme/jest-preset-for-enzyme-17.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const createJestPresetForEnzyme = require('./create-jest-preset-for-enzyme'); | ||
|
||
module.exports = createJestPresetForEnzyme(16); |
5 changes: 5 additions & 0 deletions
5
packages/jest-preset-mc-app/enzyme/setup-test-framework-for-enzyme-16.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require('./base-setup-test-framework-for-enzyme'); | ||
const Enzyme = require('enzyme'); | ||
const Adapter = require('enzyme-adapter-react-16'); | ||
|
||
Enzyme.configure({ adapter: new Adapter(), disableLifecycleMethods: true }); |
5 changes: 5 additions & 0 deletions
5
packages/jest-preset-mc-app/enzyme/setup-test-framework-for-enzyme-17.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require('./base-setup-test-framework-for-enzyme'); | ||
const Enzyme = require('enzyme'); | ||
const Adapter = require('@wojtekmaj/enzyme-adapter-react-17'); | ||
|
||
Enzyme.configure({ adapter: new Adapter(), disableLifecycleMethods: true }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,10 @@ | ||
require('unfetch/polyfill'); | ||
require('jest-enzyme'); | ||
require('@testing-library/jest-dom/extend-expect'); | ||
require('./polyfills/intl'); | ||
|
||
const Enzyme = require('enzyme'); | ||
const DefaultEnzymeAdapter = require('enzyme-adapter-react-16'); | ||
const ShallowWrapper = require('enzyme/ShallowWrapper'); | ||
const { configure: configureRtl } = require('@testing-library/react'); | ||
const configureEnzymeExtensions = require('@commercetools/enzyme-extensions'); | ||
const commerceToolsEnzymeMatchers = require('@commercetools/jest-enzyme-matchers'); | ||
const loadConfig = require('./load-config'); | ||
|
||
const jestConfig = loadConfig(); | ||
|
||
Enzyme.configure({ | ||
adapter: jestConfig.EnzymeAdapter | ||
? jestConfig.EnzymeAdapter | ||
: new DefaultEnzymeAdapter(), | ||
disableLifecycleMethods: true, | ||
}); | ||
|
||
configureRtl(jestConfig.rtlConfig); | ||
|
||
expect.extend({ | ||
toBeComponentWithName(received, actual) { | ||
const pass = received && received.displayName === actual; | ||
const message = () => | ||
`expected ${received} ${pass ? 'not ' : ''} to be ${actual}`; | ||
return { message, pass }; | ||
}, | ||
}); | ||
|
||
expect.extend(commerceToolsEnzymeMatchers); | ||
configureEnzymeExtensions(ShallowWrapper); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.