I'm writing tests using Jest & Enzyme in React Native to fully test my components behaviors and inner functions.
Shallow test seems ok but when using mount it just throws
Here's my package.json.
{
"name": "Despirithium",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"buildandroid": "react-native bundle --platform=android --entry-file=index.android.js --bundle-output='android/app/src/main/assets/index.android.bundle?dev=false",
"test": "jest --watch"
},
"dependencies": {
"axios": "^0.15.*",
"dpath": "^2.0.2",
"evosphere-router": "0.1.9",
"immutable": "^3.8.1",
"intl": "^1.2.5",
"lodash": "^4.16.4",
"node-uuid": "^1.4.7",
"normalizr": "^2.2.1",
"react": "15.3.2",
"react-native": "0.36.0",
"react-native-drawer": "^2.3.0",
"react-native-i18n": "^0.1.1",
"react-native-image-picker": "^0.22.12",
"react-native-router-flux": "^3.35.0",
"react-native-tabs": "^1.0.9",
"react-redux": "^4.4.5",
"redux": "^3.6.0",
"redux-immutable": "^3.0.8",
"redux-logger": "^2.7.0",
"redux-thunk": "^2.1.0",
"string-template": "^1.0.0",
"tcomb-form-native": "^0.6.1"
},
"devDependencies": {
"babel-core": "^6.14.0",
"babel-jest": "^17.0.2",
"babel-plugin-transform-object-rest-spread": "^6.8.0",
"babel-plugin-transform-react-jsx": "^6.8.0",
"babel-plugin-transform-remove-console": "^6.8.0",
"babel-polyfill": "^6.13.0",
"babel-preset-latest": "6.14.0",
"babel-preset-react-native": "^1.9.0",
"babel-preset-react-native-stage-0": "^1.0.1",
"babel-register": "^6.14.0",
"enzyme": "^2.6.0",
"enzyme-to-json": "^1.3.0",
"faussaire": "^0.2.2",
"faussaire-util": "^0.1.0",
"jest": "^17.0.3",
"jest-enzyme": "^2.0.0",
"jest-react-native": "^17.0.3",
"react-addons-test-utils": "15.3.2",
"react-dom": "15.3.2",
"react-native-fs": "^2.0.1-rc.2",
"react-native-mock": "^0.2.7",
"react-test-renderer": "15.3.2",
"updtr": "^0.2.1"
},
"jest": {
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
"setupTestFrameworkScriptFile": "node_modules/jest-enzyme/lib/index.js",
"testEnvironment": "jsdom",
"moduleFileExtensions": [
"js",
"json",
"es6"
],
"preset": "jest-react-native"
}
}
For now my test is pretty simple.
global.Promise = require.requireActual('promise');
import 'react-native';
import React from 'react';
import { mount, shallow } from 'enzyme';
import { mountToJson } from 'enzyme-to-json';
import { LoginScene } from '../../../../../../src/bundles/application/ui/scene/public/LoginScene';
import configureStore from '../../../../../../src/configureStore';
import { Provider } from 'react-redux';
import { getState, getInstance } from '../../../../../../src/testing/enzymeUtils';
test('LoginScene', async () => {
const store = await configureStore();
let loginScene;
const wrapper = mount(
<Provider store={store}>
<LoginScene/>
</Provider>
);
expect(mountToJson(wrapper)).toMatchSnapshot();
loginScene = wrapper.find(LoginScene);
getInstance(loginScene)._handleFieldChange('username', 'despirithium');
getInstance(loginScene)._handleFieldChange('password', 'azerty');
expect(getState(wrapper.find(LoginScene)).username).toBe('despirithium');
expect(getState(wrapper.find(LoginScene)).password).toBe('azerty');
});
I would have shallow rendered this but since I need to wrap my scene into the Provider it's not possible.
You may need to wrap Provider in a div. "...Components must return a single root element..." -https://facebook.github.io/react/docs/components-and-props.html
Related
Finally after many steps of troubleshooting rails is rendering our NavBar component, but not the base App component where everything is imported. Wondering what I might need to consider if there's a gem version not working properly like uglifier? Out of my two react_components rendered it finds the NavBar fine.
index.html.erb:
<%= react_component('NavBar', { isUserAuthed: #is_user_authed }) %>
<%= react_component('App') %>
App.jsx:
import React from "react";
import 'fontsource-roboto';
import Home from "../components/Home";
class App extends React.Component {
render() {
return (
<>
<Home/>
</>
)
}
}
export default App;
application.js:
// app/javascript/packs/application.js
import Rails from "#rails/ujs"
import Turbolinks from "turbolinks"
import * as ActiveStorage from "#rails/activestorage"
import "channels"
var componentRequireContext = require.context("components", true);
var ReactRailsUJS = require("react_ujs");
ReactRailsUJS.useContext(componentRequireContext);
Rails.start()
Turbolinks.start()
ActiveStorage.start()
package.json:
{
"name": "my-webpack-project",
"private": true,
"dependencies": {
"#babel/plugin-proposal-class-properties": "^7.16.5",
"#babel/plugin-proposal-decorators": "^7.16.4",
"#babel/plugin-proposal-object-rest-spread": "^7.16.0",
"#babel/plugin-proposal-private-methods": "^7.16.0",
"#babel/plugin-proposal-private-property-in-object": "^7.16.0",
"#babel/plugin-syntax-dynamic-import": "^7.8.3",
"#babel/plugin-syntax-jsx": "^7.16.0",
"#babel/plugin-transform-destructuring": "^7.16.0",
"#babel/plugin-transform-regenerator": "^7.16.0",
"#babel/plugin-transform-runtime": "^7.16.4",
"#babel/preset-env": "^7.16.5",
"#date-io/date-fns": "^2.9.1",
"#date-io/moment": "^2.9.0",
"#jest/globals": "^26.3.0",
"#material-ui/core": "^4.11.0",
"#material-ui/icons": "^4.9.1",
"#material-ui/pickers": "^3.2.10",
"#rails/actioncable": "^6.1.4-1",
"#rails/activestorage": "^6.1.4-1",
"#rails/ujs": "^6.1.4-1",
"#rails/webpacker": "5.4.3",
"babel-plugin-transform-react-remove-prop-types": "^0.4.24",
"caniuse-lite": "^1.0.30001249",
"clsx": "^1.1.1",
"date-fns": "^2.16.1",
"fetch-mock": "^9.10.6",
"fontsource-roboto": "^3.0.3",
"html-webpack-plugin": "^4.5.2",
"isomorphic-fetch": "^2.2.1",
"jquery": "^3.5.0",
"leaflet": "^1.6.0",
"lodash": "^4.17.20",
"mapbox-gl": "^1.8.1",
"moment": "^2.27.0",
"node": "16",
"node-fetch": "^2.6.0",
"npm": "^6.14.7",
"popper.js": "^1.16.1",
"postcss-cssnext": "^3.1.0",
"prop-types": "^15.7.2",
"query-string": "^6.13.1",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-leaflet": "^2.6.1",
"react-password-strength": "^2.4.0",
"react-router-dom": "^5.1.2",
"react_ujs": "^2.6.1",
"recharts": "^2.0.9",
"sass": "^1.37.5",
"turbolinks": "^5.2.0",
"webpack": "^4.46.0",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3",
"uglify-js": "git://github.com/mishoo/UglifyJS2#harmony"
},
"devDependencies": {
"#babel/core": "^7.16.5",
"#webpack-cli/generators": "^2.4.1",
"autoprefixer": "^10.4.0",
"babel-loader": "^8.2.3",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"css-loader": "^5.2.7",
"jest": "^26.0.1",
"mini-css-extract-plugin": "^1.6.2",
"node-sass": "^7.0.0",
"post-loader": "^2.0.0",
"postcss": "^8.4.4",
"sass-loader": "^10.2.0",
"style-loader": "^2.0.0"
},
"version": "1.0.0",
"description": "My webpack project",
"scripts": {
"build": "webpack --mode=production --node-env=production",
"build:dev": "webpack --mode=development",
"build:prod": "webpack --mode=production --node-env=production",
"watch": "webpack --watch",
"serve": "webpack serve"
}
}
Do you have this in your config?
$ tail -n 12 webpack.config.js
})
],
module: {
rules: [
{
test: /\.(js|jsx|ts|tsx|)$/,
exclude: /node_modules/,
use: ['babel-loader'],
}
]
}
}
https://github.com/reactjs/react-rails/blob/master/README.md#3-install-react-and-some-other-required-npm-packages
Update the Babel configuration in the package.json file:
"babel": {
"presets": [
- "./node_modules/shakapacker/package/babel/preset.js"
+ "./node_modules/shakapacker/package/babel/preset.js",
+ "#babel/preset-react"
]
},
This could be a very easy question, however I've not been able to find the answer for it anywhere.
How do I use a default locale with react-intl? Specifying a default message every time gets really messy. The way that I'm doing it at the moment doesn't seem to pick up the default message when the browser uses a locale that doesn't have a translation.
How I'm using it at the moment:
import React from 'react'
import { Route } from 'react-router-dom'
import { IntlProvider } from 'react-intl'
import { Provider } from 'react-redux'
import { MuiThemeProvider, CssBaseline } from '#material-ui/core'
// import translations
import da_translations from './translations/da.json'
import de_translations from './translations/de.json'
import el_translations from './translations/el.json'
import en_translations from './translations/en.json'
import defaultTheme from './themes/default'
import {
QueryParamProvider,
ExtendedStringifyOptions,
transformSearchStringJsonSafe,
} from 'use-query-params'
import configureStore from './redux/configure-store'
import { Routes } from './routes'
const store = configureStore()
const App = (): JSX.Element => {
const state = { locale: navigator.language.slice(0, 2) }
const messages: { [index: string]: any } = {
da: da_translations,
de: de_translations,
el: el_translations,
en: en_translations,
}
const queryStringifyOptions: ExtendedStringifyOptions = {
transformSearchString: transformSearchStringJsonSafe,
}
return (
<Provider store={store}>
<IntlProvider
defaultLocale={'en'}
locale={state.locale}
messages={messages[state.locale]}
>
<QueryParamProvider
ReactRouterRoute={Route}
stringifyOptions={queryStringifyOptions}
>
<MuiThemeProvider theme={defaultTheme}>
<CssBaseline />
<Routes />
</MuiThemeProvider>
</QueryParamProvider>
</IntlProvider>
</Provider>
)
}
export default App
edit:
package.json
{
"name": "myproj",
"version": "0.8.47",
"description": "frontend",
"private": false,
"main": "dist/index.js",
"dependencies": {
"#babel/register": "^7.12.1",
"#date-io/date-fns": "^1.3.13",
"#google-cloud/translate": "^6.0.3",
"#material-ui/core": "^4.11.1",
"#material-ui/icons": "4.2.1",
"#material-ui/lab": "^4.0.0-alpha.56",
"#material-ui/pickers": "^3.2.10",
"#types/axios": "^0.14.0",
"#types/history": "^4.7.3",
"#types/jest": "^24.9.1",
"#types/lodash": "^4.14.158",
"#types/node": "^12.12.7",
"#types/react": "^16.9.11",
"#types/react-dom": "^16.9.4",
"#types/react-html-parser": "^2.0.1",
"#types/react-intl": "^3.0.0",
"#types/react-redux": "^7.1.9",
"#types/react-resizable": "^1.7.2",
"#types/react-router-dom": "^5.1.2",
"#types/reselect": "^2.2.0",
"#types/seamless-immutable": "^7.1.13",
"accept-language": "^3.0.18",
"ag-grid-community": "^23.1.1",
"ag-grid-react": "^23.1.1",
"axios": "^0.19.1",
"chai": "^4.2.0",
"chartist": "0.10.1",
"classnames": "^2.2.6",
"clsx": "^1.1.0",
"date-fns": "^2.9.0",
"flexsearch": "^0.6.32",
"fs-extra": "^9.0.1",
"history": "4.9.0",
"immutable": "^4.0.0-rc.12",
"js-yaml": "^3.14.0",
"lodash": "^4.17.19",
"log4js": "^6.3.0",
"loglevel": "^1.6.6",
"minimist": "^1.2.5",
"perfect-scrollbar": "1.4.0",
"prop-types": "15.7.2",
"qs": "^6.9.4",
"query-string": "^6.13.1",
"react": "^16.12.0",
"react-chartist": "0.13.3",
"react-dom": "^16.12.0",
"react-google-maps": "9.4.5",
"react-html-parser": "^2.0.2",
"react-intl": "^3.11.0",
"react-redux": "^7.2.1",
"react-resizable": "^1.10.1",
"react-router-dom": "^5.1.2",
"react-scripts": "4.0.0",
"react-swipeable-views": "0.13.3",
"redux": "^4.0.5",
"redux-observable": "^1.2.0",
"reselect": "^4.0.0",
"seamless-immutable": "^7.1.4",
"use-query-params": "^1.1.6"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env jest-environment-jsdom-sixteen --verbose",
"eject": "react-scripts eject",
"install:clean": "rm -rf node_modules/ && rm -rf package-lock.json && npm install && npm start",
"lint:check": "eslint . --ext=js,jsx; exit 0",
"lint:fix": "eslint . --ext=js,jsx --fix; exit 0",
"build-package-css": "cp src/assets/css/material-dashboard-react.css dist/material-dashboard-react.css",
"build-package": "npm run build-package-css && babel src --out-dir dist"
},
"keywords": [],
"author": "tb",
"license": "MIT",
"homepage": "./",
"optionalDependencies": {
"#types/googlemaps": "3.37.3",
"#types/markerclustererplus": "2.1.33",
"ajv": "6.10.2"
},
"devDependencies": {
"#testing-library/dom": "^7.24.2",
"#testing-library/jest-dom": "^5.11.1",
"#testing-library/react": "^10.4.7",
"#testing-library/user-event": "^12.1.4",
"#types/chai": "^4.2.13",
"#types/classnames": "^2.2.10",
"#types/enzyme": "^3.10.5",
"#types/enzyme-adapter-react-16": "^1.0.6",
"#types/gulp": "^4.0.7",
"#types/jsdom": "^16.2.2",
"#typescript-eslint/eslint-plugin": "^4.7.0",
"#typescript-eslint/parser": "^4.7.0",
"axios-mock-adapter": "^1.18.2",
"cross-env": "^5.1.4",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.2",
"eslint-config-prettier": "6.0.0",
"eslint-plugin-prettier": "3.1.0",
"eslint-plugin-react": "^7.21.5",
"eslint-plugin-react-hooks": "^4.2.0",
"gulp": "^4.0.2",
"gulp-append-prepend": "1.0.8",
"gulp-filter": "^6.0.0",
"gulp-prettier": "^3.0.0",
"husky": "^4.3.0",
"jest-environment-jsdom-sixteen": "^1.0.3",
"jsdom": "^16.2.2",
"prettier": "^2.1.2",
"pretty-quick": "^3.1.0",
"react-intl-translations-manager": "^5.0.3",
"ts-node": "^9.0.0",
"typescript": "^3.9.7"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": []
},
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged"
}
},
"jest": {
"transformIgnorePatterns": [
"node_modules/(?!(#ui5|lit-html)).*\\.js$"
]
}
}
I am trying to use react-redux hooks, however I get the error:
TypeError: (0 , _reactRedux.useDispatch) is not a function
Any ideas?
...
import { useDispatch, useSelector } from "react-redux";
import { StackActions, NavigationActions } from "react-navigation";
import CustomImage from "./CustomImage";
const VideoEpisode = props => {
const { id, title, description, video, preview, push, testLink } = props;
const dispatch = useDispatch();
return (
<TouchableHighlight
....
onPress={() => {
const resetAction = StackActions.reset({
...
});
dispatch(resetAction);
}}
>
...
</TouchableHighlight>
);
};
export default VideoEpisode;
package.json
{
"name": "iaapp",
"version": "0.0.1",
"scripts": {
"start": "react-native start",
"android": "react-native run-android",
"ios": "react-native run-ios",
"name": "Innovative Anatomy",
"flow": "flow",
"test": "node ./node_modules/jest/bin/jest.js --watchAll"
},
"jest": {
"preset": "react-native"
},
"dependencies": {
"#babel/plugin-proposal-decorators": "^7.4.4",
"#babel/runtime": "^7.0.0",
"#react-native-community/async-storage": "^1.6.1",
"react": "16.8.5",
"react-dom": "^16.8.6",
"react-native": "0.59.10",
"react-native-circular-progress": "^1.1.0",
"react-native-component-fade": "^1.0.2",
"react-native-elements": "^1.1.0",
"react-native-fade": "^1.0.3",
"react-native-fade-in-view": "^1.0.5",
"react-native-gesture-handler": "^1.1.0",
"react-native-responsive-image": "^2.3.1",
"react-native-share": "^1.2.1",
"react-native-splash-screen": "^3.2.0",
"react-native-svg": "^9.5.1",
"react-native-vector-icons": "^6.6.0",
"react-native-view-shot": "^2.6.0",
"react-native-webview": "^5.12.1",
"react-native-webview-bridge": "^0.40.1",
"react-navigation": "^3.0.9",
"react-navigation-backhandler": "^1.3.2",
"react-navigation-transitions": "^1.0.11",
"react-redux": "^7.1.0",
"redux": "^4.0.1",
"redux-persist": "^5.10.0",
"redux-thunk": "^2.3.0"
},
"devDependencies": {
"#babel/cli": "^7.5.5",
"#babel/core": "^7.5.5",
"#babel/preset-flow": "^7.0.0",
"jest": "^24.8.0",
"metro-react-native-babel-preset": "^0.54.1",
"prettier": "^1.18.2",
"react-native-dotenv": "^0.2.0",
"redux-logger": "^3.0.6"
},
"rnpm": {
"assets": [
"./assets/fonts"
]
},
"private": true
}
I had a similar problem with react.createcontext, and the only thing that seemed to work was to delete the node_modules folder and reinstall them (npm install).
Just make sure your package.json is configured correctly
Try upgrading your redux version { useDispatch } don't work on older ones
I also came around the same problem, tried everything deleting the node modules, clearing cache installing appropriate react-redux version but nothing worked. Later I found that the action which I was passing was not in proper format. Recheck the action action passed to dispatch function.
in our project we use react-intl for internationalisation. We are using jest as the testing framework and we decided to move our unit test utility library from enzyme to react-testing-library. But now, since there is no shallow rendering, I have to provide <IntlProvider ... /> in the test myself, no problem. But even if I do that, when testing terminal says Invalid hook call. Hooks can only be called inside of the body of a function component but when I run the project locally, no errors just runs normally as expected. P.S in the tests, Apollo provider works normally.
FirstNameFor.test.tsx
import { IntlProvider } from 'react-intl';
import { createMockClient } from 'mock-apollo-client';
import { ApolloProvider as ApolloProviderHooks } from '#apollo/react-hooks';
import { ApolloProvider } from 'react-apollo';
const wrapper = (mockProps, mockClient) => {
return (
<ApolloProvider client={mockClient}>
<ApolloProviderHooks client={mockClient}>
<IntlProvider locale="en">
<FirstNameForm
handleNext={mockProps.handleNext}
onboardingState={mockProps.onboardingState}
setHelpContent={mockProps.setHelpContent}
updateOnboardingState={mockProps.updateOnboardingState}
/>
</IntlProvider>
</ApolloProviderHooks>
</ApolloProvider>
);
};
describe('FirstNameForm Container', () => {
afterEach(() => {
jest.clearAllMocks();
cleanup();
});
it('should match snapshot', () => {
// Arrange
const mockProps = {
handleNext: jest.fn(),
onboardingState: {},
setHelpContent: jest.fn(),
updateOnboardingState: jest.fn(),
};
const mockClient = createMockClient();
// Act
const component = render(wrapper(mockProps, mockClient));
// Assert
expect(component).toMatchSnapshot();
});
});
FirstNameForm.tsx
export const FirstNameForm: React.FC<StepContentProps> = props => {
const [showMessageMutation] = useShowMessageMutation();
const intl = useIntl();
const formik = useFormik<FirstNameFormValues>({
...,
});
return <FirstNameFormComponent formik={formik} intl={intl} />;
};
Terminal Screenshot
package.json
{
"private": true,
"proxy": "http://localhost:5000",
"version": "0.1.57",
"scripts": {
"dev": "yarn graphql:generate && SKIP_PREFLIGHT_CHECK=true react-app-rewired start",
"build": "SKIP_PREFLIGHT_CHECK=true react-app-rewired build",
"testonly": "SKIP_PREFLIGHT_CHECK=true react-app-rewired test --env=jsdom",
"coverage": "SKIP_PREFLIGHT_CHECK=true react-app-rewired test --env=jsdom --coverage",
},
"jest": {
"coveragePathIgnorePatterns": [
"/node_modules/",
"src/types/",
"src/config/analytics/mp.d.ts",
"src/config/analytics/ua.d.ts"
],
"snapshotSerializers": [
"enzyme-to-json/serializer"
]
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"resolutions": {
"#types/react": "16.8.17"
},
"dependencies": {
"#apollo/react-hooks": "3.1.3",
"animated-scroll-to": "1.2.2",
"apollo-cache": "1.3.2",
"apollo-cache-inmemory": "1.6.3",
"apollo-client": "2.6.3",
"apollo-link": "1.2.12",
"apollo-link-context": "1.0.18",
"apollo-link-error": "1.1.11",
"apollo-link-http": "1.5.15",
"apollo-link-ws": "1.0.18",
"apollo-utilities": "1.3.2",
"big.js": "5.2.2",
"copy-to-clipboard": "3.2.0",
"core-js": "3.0.1",
"date-fns": "1.30.1",
"downloadjs": "1.4.7",
"formik": "2.1.1",
"graphql": "14.3.0",
"graphql-subscriptions": "1.1.0",
"graphql-tag": "2.10.1",
"graphql-tools": "4.0.4",
"history": "4.9.0",
"intl": "1.2.5",
"js-cookie": "2.2.0",
"lodash": "4.17.11",
"owasp-password-strength-test": "1.3.0",
"qrcode.react": "0.9.3",
"query-string": "6.5.0",
"react": "16.8.6",
"react-apollo": "3.1.3",
"react-dom": "16.8.6",
"react-google-recaptcha": "1.1.0",
"react-intl": "3.1.8",
"react-router-dom": "5.1.0",
"recompose": "0.30.0",
"regenerator-runtime": "0.13.2",
"subscriptions-transport-ws": "0.9.16",
"validator": "10.11.0",
"victory": "34.0.0",
"yup": "0.27.0"
},
"devDependencies": {
"#apollo/react-testing": "3.1.3",
"#babel/cli": "7.4.4",
"#babel/core": "7.4.4",
"#babel/preset-env": "7.4.4",
"#graphql-codegen/add": "1.8.1",
"#graphql-codegen/cli": "1.8.1",
"#graphql-codegen/fragment-matcher": "1.8.1",
"#graphql-codegen/typescript": "1.8.1",
"#graphql-codegen/typescript-operations": "1.8.1",
"#graphql-codegen/typescript-react-apollo": "1.8.1",
"#testing-library/react": "10.0.4",
"#testing-library/jest-dom": "5.8.0",
"#types/testing-library__react": "10.0.1",
"#types/big.js": "4.0.5",
"#types/downloadjs": "1.4.1",
"#types/enzyme": "3.10.5",
"#types/graphql": "14.2.0",
"#types/js-cookie": "2.2.2",
"#types/node": "12.0.0",
"#types/owasp-password-strength-test": "1.3.0",
"#types/prop-types": "15.7.1",
"#types/qrcode.react": "0.8.2",
"#types/query-string": "6.3.0",
"#types/react": "16.8.17",
"#types/react-dom": "16.8.4",
"#types/react-google-recaptcha": "1.0.0",
"#types/react-router-dom": "5.1.0",
"#types/react-test-renderer": "16.9.0",
"#types/recompose": "0.30.6",
"#types/validator": "10.11.0",
"#types/victory": "33.1.0",
"#types/ws": "6.0.1",
"#types/yup": "0.26.13",
"babel-loader": "8.0.6",
"enzyme": "3.11.0",
"enzyme-adapter-react-16": "1.15.2",
"enzyme-to-json": "3.4.4",
"jest-canvas-mock": "2.2.0",
"jest-localstorage-mock": "2.4.0",
"merge-graphql-schemas": "1.5.8",
"npm-run-all": "4.1.5",
"patch-package": "6.1.2",
"postinstall-postinstall": "2.0.0",
"react-app-rewired": "2.1.3",
"react-scripts": "3.3.0",
"react-test-renderer": "16.8.6",
"source-map-explorer": "1.8.0",
"ts-jest": "25.3.1",
"ts-node": "8.1.0",
"typescript": "3.5.3"
}
}
The error comes from React itself, and most likely has nothing to do with react-intl.
There's a good chance you have multiple instances of React in conflict, for some reason only occurring in the test environment. I suspect it might have to do with react-app-rewired, not sure though.
The steps from the official help doc should help reveal whether duplicate Reacts is the source of the problem.
If that is the reason, here's some of the top solutions from the community:
Modify your webpack config to resolve the react alias (discussion)
resolve: {
alias: {
react: path.resolve('./node_modules/react'),
}
},
Modify your webpack config to set react as an external (PR example)
externals: {
react: 'react'
}
Or one of these other miscellaneous approaches
I am trying to add the #DragDropContext decorator on my class but I am getting an error when I load it in my browser.
My component looks like:
import React from 'react';
import Link from 'react-router-dom';
import { connect } from 'react-redux';
import { DragDropContext } from 'react-dnd';
import { HTML5Backend } from 'react-dnd-html5-backend';
import PropTypes from 'prop-types';
#DragDropContext(HTML5Backend)
class UserShowView extends React.Component {
}
export default connect(mapStateToProps)(UserShowView);
This is what the error looks like in chrome console:
bundle.js:977 Uncaught Error: Expected the backend to be a function or
an ES6 module exporting a default function. Read more:
http://react-dnd.github.io/react-dnd/docs-drag-drop-context.html
My babelrc file:
{
"plugins": ["transform-decorators-legacy"],
"presets": ["react", "es2015", "stage-0"],
"env": {
"development": {
"presets": ["react-hmre"]
}
}
}
packages.json:
{
"name": "frontend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node tools/srcServer.js",
"test": "NODE_ENV=test jest",
"lint": "eslint --max-warnings=0 src test",
"test:watch": "NODE_ENV=test jest --watch"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^8.2.1",
"babel-loader": "^7.1.2",
"babel-plugin-react-display-name": "^2.0.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-react-hmre": "^1.1.1",
"babel-preset-stage-0": "^6.24.1",
"babel-register": "^6.26.0",
"colors": "^1.1.2",
"compression": "^1.7.1",
"eslint": "^4.15.0",
"eslint-config-airbnb": "^16.1.0",
"eslint-plugin-babel": "^4.1.2",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.5.1",
"eslint-watch": "^3.1.3",
"express": "^4.16.2",
"jest": "^22.0.6",
"node-sass": "^4.7.2",
"npm-run-all": "^4.1.2",
"redux-devtools": "^3.4.1",
"sass-loader": "^6.0.6",
"webpack": "^3.10.0",
"webpack-dev-middleware": "^2.0.4",
"webpack-dev-server": "^2.10.1",
"webpack-hot-middleware": "^2.21.0"
},
"dependencies": {
"axios": "^0.17.1",
"history": "^4.7.2",
"lodash": "^4.17.5",
"moment": "^2.20.1",
"react": "^16.2.0",
"react-dnd": "^2.5.4",
"react-dnd-html5-backend": "^2.5.4",
"react-dom": "^16.2.0",
"react-redux": "^5.0.6",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
"react-router-redux": "^4.0.8",
"redux": "^3.7.2",
"redux-thunk": "^2.2.0"
},
}
What am I doing wrong or am I missing a babel preset?
Is my "transform-decorators-legacy" even working?
As the error messages shows, #DragDropContext(HTML5Backend) is expecting a module or function.
From the documentation you should be importing the HTML5Backend module with:
import HTML5Backend from 'react-dnd-html5-backend';
Since you are using
import { HTML5Backend } from 'react-dnd-html5-backend';
You are trying to import a specific export that doesn't exist thus DragDropContext showing the error.
Importing with curly brackets is for specific exports vs without curly brackets which is for default exports.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
for more information on different ways of importing modules and functions.
Just remove the curly braces around HTML5Backend
import HTML5Backend from 'react-dnd-html5-backend';
http://react-dnd.github.io/react-dnd/docs-drag-drop-context.html#usage