I created the react-native project and was doing some experiments with style guides and plugin, I created some mistakes to check how eslint error description work. Here is some code which I wrote and put an error(a comma) in app.js file.
app.js
import React, { Component } from 'react'
import { Text, View } from 'react-native'
const App = () => (, <---------------------- this is the error(a comma)
<View style={{ flex: 1 }}>
<Text>hi</Text>
</View>
)
export default App
Following is the error screenshot:
As we can see that in the error description of the eslint, there are some "[39m 36m" embedded in between. What could be the reason for this unstructured error message? please help.
Here is some other files attached.
.eslintrc file
{
"parser": "babel-eslint",
"extends": "airbnb",
"plugins": ["react", "flowtype", "jsx-a11y", "import"],
"rules": {
"react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }],
"react/prefer-stateless-function": [1, { "ignorePureComponents": true }],
"react/forbid-prop-types": [0, { "forbid": [] }],
"import/extensions": [1, "never", { "svg": "always" }],
"import/no-extraneous-dependencies": [
"error",
{
"devDependencies": true,
"optionalDependencies": false,
"peerDependencies": false
}
],
"semi": ["error", "never"]
},
"env": {
"jest": true
}
}
package.json
{
"name": "auth1",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest",
"lint": "eslint src",
"pretty":
"prettier --semi false --print-width 100 --single-quote --trailing-comma all --write \"src/**/*.js\"",
"precommit": "lint-staged && yarn test",
"flow": "flow",
"flow stop": "flow stop",
"flow status": "flow status",
"flow coverage": "flow coverage"
},
"lint-staged": {
"*.js": ["yarn pretty", "git add"]
},
"dependencies": {
"react": "16.3.0-alpha.1",
"react-native": "0.54.0"
},
"devDependencies": {
"babel-eslint": "^8.2.2",
"babel-jest": "22.4.1",
"babel-preset-flow": "^6.23.0",
"babel-preset-react-native": "4.0.0",
"eslint": "^4.18.2",
"eslint-config-airbnb": "^16.1.0",
"eslint-plugin-flowtype": "^2.46.1",
"eslint-plugin-import": "^2.9.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.7.0",
"flow-bin": "0.65.0",
"husky": "^0.14.3",
"jest": "22.4.2",
"lint-staged": "^7.0.0",
"prettier": "^1.11.1",
"react-test-renderer": "16.3.0-alpha.1"
},
"jest": {
"preset": "react-native"
}
}
Also, I installed eslint and Flow-bin, both can identifies the error(as you can see in screenshot above under problems tab), but do we need to use both and if not which one to prefer or if both have different purpose please help me understand.
Thanks in Advance.
You probably need to specify the --no-color option to eslint. Those are ANSI escape sequences, which is how color is added to the console messages. If you get the same problem in your terminal, that might also need configuring to handle ANSI, or use the CLI option to disable color.
eslint and flowtype serve different purposes. Flow will pick up certain syntax errors, but eslint will pick up more esoteric code constructs that can cause problems (assuming configured to do so).
Flow will interpret the types you specify and warn/error when you are trying to use incompatible types (in assignment, parameters, returns etc).
With color:
> eslint . "--color"
[4m/Users/xxxx/yyyy.js[24m
[2m7:13[22m [31merror[39m Parsing error: Unexpected token
[0m [90m 5 | [39m}[0m
[0m [90m 6 | [39m[0m
[0m[31m[1m>[22m[39m[90m 7 | [39m[36mfunction[39m f({[33m,[39m[0m
[0m [90m | [39m [31m[1m^[22m[39m[0m
[0m [90m 8 | [39m [90m// $FlowFixMe[39m[0m
[0m [90m 9 | [39m a [33m=[39m [35m1[39m[33m,[39m[0m
[0m [90m 10 | [39m}[33m:[39m [33mParams[39m) {[0m
[31m[1m✖ 1 problem (1 error, 0 warnings)[22m[39m
[31m[1m[22m[39m
Without color:
> eslint . "--no-color"
/Users/xxxx/yyyy.js
7:13 error Parsing error: Unexpected token
5 | }
6 |
> 7 | function f({,
| ^
8 | // $FlowFixMe
9 | a = 1,
10 | }: Params) {
✖ 1 problem (1 error, 0 warnings)
I think it is the issue with ESLINT(dbaeumer.vscode-eslint) plugin in vscode.
Related
I'm using Vue.js 3.
The following eslint-plugin-vue warning appears to warn using the rules in Vue.js 2.
<MyComponent v-model:propName="foo"/> This writing style is supported in Vue.js 3.
How to make it compatible with Vue.js 3?
<MyInputComponent
v-model:value="state.value"
/>
// [vue/valid-v-model] 'v-model' directives require no argument. eslint-plugin-vue [7, 9]
.eslintrc.js
module.exports = {
extends: [
'plugin:vue/vue3-recommended',
]
}
package.json
{
"name": "ProjectName",
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build"
},
"dependencies": {
"typescript": "^4.1.2",
"vue": "^3.0.2"
},
"devDependencies": {
"#vue/compiler-sfc": "^3.0.2",
"eslint-plugin-vue": "^7.1.0",
"vite": "^1.0.0-rc.8"
}
}
This worked for me - ignore the rule in .eslintrc.js:
rules: {
"vue/no-v-model-argument": "off",
},
Add these deps:
"#babel/core": "^7.12.10",
"#babel/eslint-parser": "^7.12.1",
"eslint": "^7.18.0",
install them:
npm i -D eslint #babel/core #babel/eslint-parser
change eslint.js
parser: '#babel/eslint-parser',
am using react native web and want to add react navigation 5 to the project. However when I try building the web version upon adding react-navigation I get
./node_modules/react-native-gesture-handler/DrawerLayout.js
SyntaxError: D:\Parentlane\ReactNativeWeb\node_modules\react-native-gesture-handler\DrawerLayout.js: Support for the experimental syntax 'flow' isn't currently enabled (30:8):
28 | const SETTLING = 'Settling';
29 |
> 30 | export type PropType = {
| ^
31 | children: any,
32 | drawerBackgroundColor?: string,
33 | drawerPosition: 'left' | 'right',
Add #babel/plugin-transform-flow-strip-types (https://git.io/vb49g) to the 'plugins' section of your Babel config to enable transformation.
I added plugin-transform-flow-strip-types as a dev dependency but that does not help.
below is my package.json
{
"name": "ReactNativeWeb",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"web": "react-scripts start",
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"dependencies": {
"#react-native-community/masked-view": "^0.1.10",
"#react-navigation/native": "^5.7.6",
"axios": "^0.20.0",
"babel-loader": "^8.1.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"qs": "^6.9.4",
"react": "16.13.1",
"react-dom": "^16.13.1",
"react-native": "0.63.2",
"react-native-gesture-handler": "^1.8.0",
"react-native-reanimated": "^1.13.1",
"react-native-safe-area-context": "^3.1.8",
"react-native-screens": "^2.11.0",
"react-native-vector-icons": "^7.1.0",
"react-native-web": "^0.13.12"
},
"devDependencies": {
"#babel/core": "^7.11.6",
"#babel/plugin-proposal-class-properties": "^7.10.4",
"#babel/plugin-transform-flow-strip-types": "^7.10.4",
"#babel/preset-env": "^7.11.5",
"#babel/preset-react": "^7.10.4",
"#babel/runtime": "^7.11.2",
"#react-native-community/eslint-config": "^2.0.0",
"metro-react-native-babel-preset": "^0.63.0",
"react-scripts": "^3.4.3",
"react-test-renderer": "16.13.1",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.0"
},
"jest": {
"preset": "react-native"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
babel.config.js is as follows:
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
};
could anyone tell me how to add the plugin inside babel.config.js?or is there any other way to resolve the above error?
Any suggestions would be great and let me know if any other detail is required for better understanding.
You could add it inside your babel config file. Include it in your plugins.
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: ['#babel/plugin-transform-flow-strip-types'],
};
And install it with npm like this
npm install --save-dev #babel/plugin-transform-flow-strip-types
Check the docs here -> https://babeljs.io/docs/en/babel-plugin-transform-flow-strip-types
Apparently this "helpful" message is the default error message when "neither Flow nor Typescript are enabled in Babel".
Personally I came across it when using a Typescript project and attempting to import something from within a monorepo but outside the current scope - it's then throwing this error when it encounters the TS keyword "interface".
Fixing this will probably depend on your own project setup - however, if you're not using Flow it's unlikely that adding any of the recommended Babel/flow plugins will help.
Add plugin plugin-transform-flow-strip-types in file babel.config.js and enable allowDeclareFields
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
...
['#babel/plugin-transform-flow-strip-types', { allowDeclareFields: true }]
],
};
For install plugin:
npm install --save-dev #babel/plugin-transform-flow-strip-types
//or
yarn add --dev #babel/plugin-transform-flow-strip-types
I've tried quite a few variations on this with no luck.
Situation: I want to render a React Native app that has a Mapbox map in it inside my Jest tests, so I can test the logic we wrote around it.
I've managed to reproduce the error I'm seeing in a mini repo: https://github.com/JKowalsky/mapbox-error-test-repo/tree/master
The mini repo is a react-native init default project that includes Mapbox and sets an access token, just so we can play with the dependency.
import MapboxGL from '#react-native-mapbox-gl/maps';
MapboxGL.setAccessToken('fakeyfakeytokentoken');
I'd expect the default __test__/App-test.js to still run, but it fails on the Mapbox include. It looked like a Babel problem, so I set up the following babel.config.js;
module.exports = function(api) {
api.cache(true);
return {
presets: [
'module:metro-react-native-babel-preset',
"#babel/preset-env",
"#babel/preset-flow"
],
plugins: [
"#babel/plugin-proposal-class-properties",
"#babel/plugin-syntax-dynamic-import"
]
};
};
And here's the package.json to run that:
{
"name": "mapboxerror",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest --no-cache",
"lint": "eslint ."
},
"dependencies": {
"#mapbox/geo-viewport": "^0.4.0",
"#react-native-mapbox-gl/maps": "^7.0.6",
"jsdom": "^15.1.1",
"mapbox": "^1.0.0-beta10",
"react": "16.9.0",
"react-native": "0.61.1"
},
"devDependencies": {
"#babel/core": "^7.6.2",
"#babel/plugin-proposal-class-properties": "^7.5.5",
"#babel/plugin-syntax-dynamic-import": "^7.2.0",
"#babel/preset-env": "^7.6.2",
"#babel/preset-flow": "^7.0.0",
"#babel/runtime": "^7.6.2",
"#react-native-community/eslint-config": "^0.0.5",
"babel-jest": "^24.9.0",
"eslint": "^6.4.0",
"jest": "^24.9.0",
"metro-react-native-babel-preset": "^0.56.0",
"react-test-renderer": "16.9.0"
},
"jest": {
"preset": "react-native"
}
}
I still get the unexpected token error:
/Users/jennifer/dev/mapbox-error-test-repo/node_modules/#react-native-mapbox-gl/maps/javascript/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import {Animated, NativeModules, PermissionsAndroid} from 'react-native';
SyntaxError: Unexpected token {
25 | } from 'react-native/Libraries/NewAppScreen';
26 |
> 27 | import MapboxGL from '#react-native-mapbox-gl/maps';
| ^
28 | MapboxGL.setAccessToken('fakeyfakeytokentoken');
29 |
30 | const App: () => React$Node = () => {
at ScriptTransformer._transformAndBuildScript (node_modules/#jest/transform/build/ScriptTransformer.js:537:17)
at ScriptTransformer.transform (node_modules/#jest/transform/build/ScriptTransformer.js:579:25)
at Object.<anonymous> (App.js:27:1)
Final curiosity: I've added a transform inside the jest config in package.json, but it then gives me a type error (this is not a typescript project.)
"jest": {
"preset": "react-native",
"transformIgnorePatterns": [
"node_modules/(?!(mapbox-gl|mapbox|#react-native-mapbox-gl))/"
]
}
Doing that gives me this error which is even more inscrutable:
yarn run v1.16.0
$ jest --no-cache
FAIL __tests__/App-test.js
● Test suite failed to run
TypeError: (0 , _typeof4.default) is not a function
at _typeof2 (node_modules/#babel/runtime/helpers/typeof.js:8:63)
at _typeof (node_modules/#babel/runtime/helpers/typeof.js:22:39)
at new Promise (node_modules/promise/lib/core.js:44:31)
at valuePromise (node_modules/promise/lib/es6-extensions.js:18:11)
at Object.<anonymous> (node_modules/promise/lib/es6-extensions.js:10:12)
at Object.<anonymous> (node_modules/promise/lib/index.js:9:1)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.192s
Ran all test suites.
error Command failed with exit code 1.
Any hints? I've been circling this issue for a while, and there isn't a ton of testing infrastructure support for Mapbox.
Alright, I actually managed to get this working. I had to mock both the Mapbox-gl and react-native EventEmitter dependencies in addition to adding a ton of babel transforms. The linked repository has been updated with a now passing unit test.
I have an Expo app and was using SDK 28. My team decided we should update to the latest version, that meant updating React Native (Since the latest SDK uses RN 0.57) and Babel.
When we updated our dependencies, and fixed our config files, Jest started to give us this error:
TypeError: Cannot read property 'fetch' of undefined
at node_modules/react-native/Libraries/vendor/core/whatwg-fetch.js:6:12
at Object.<anonymous> (node_modules/react-native/Libraries/vendor/core/whatwg-fetch.js:486:3)
at Object.<anonymous> (node_modules/jest-expo/src/setup.js:125:16)
After a few days debugging I found out this is related to babel-jest's pre-processor not working correctly, even though I followed their installation docs.
I dug around some more and found out that there's a workaround in this GitHub Issue thread.
Implementing the workaround, plus adding babel-hoist to my babel.config.js, made so that the tests started running.
However Jest's behavior is all wonky and the coverage data is not correct (it counts some lines as uncovered, even though we do have tests for them).
I want to know how to configure Jest properly for compatibility with Expo SDK 32.
These are the relevant config files (which are set to use the workaround mentioned previously).
package.json*
"dependencies": {
"#babel/preset-env": "^7.3.1",
"#expo/vector-icons": "6.3.1",
"expo": "^32.0.0",
"prop-types": "15.6.2",
"react": "16.5.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",
"sentry-expo": "~1.9.0"
...
},
"devDependencies": {
"#babel/core": "^7.2.2",
"babel-eslint": "9.0.0",
"babel-plugin-jest-hoist": "^24.0.0",
"babel-preset-expo": "^5.0.0",
"enzyme": "3.8.0",
"enzyme-adapter-react-16": "^1.8.0",
"jest-expo": "^32.0.0",
"metro-react-native-babel-preset": "^0.51.1",
"react-dom": "^16.5.1",
...
},
"jest": {
"preset": "jest-expo",
"transform": {
"^.+\\.js$": "<rootDir>/jest.preprocessor.js"
},
"setupFiles": [
"<rootDir>/src/jest.setup.js"
],
...
}
* Some dependecies were omitted.
babel.config.js
module.exports = {
presets: [
'babel-preset-expo',
'module:metro-react-native-babel-preset',
'module:react-native-dotenv',
[
'#babel/preset-env',
{
targets: {
node: 'current',
},
},
],
],
sourceMaps: true,
plugins: [
'jest-hoist',
'#babel/transform-react-jsx-source',
],
};
This is what solved the problem for me:
First thing, install yarn. Follow this link for instructions.
Second, ensure your package.json looks something like this:
"dependencies": {
"#expo/vector-icons": "9.0.0",
"expo": "^32.0.0",
"prop-types": "15.6.2",
"react": "16.5.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",
...
},
"devDependencies": {
"babel-preset-expo": "^5.0.0",
"jest-expo": "^32.0.0",
...
}
"scripts": {
"test": "jest",
...
},
"jest": {
"preset": "jest-expo",
"transform": {
"^.+\\.js$": "babel-jest"
},
}
Third, ensure your babel.config.js is setup correctly. Here's the one from my project running Expo's SDK 32:
module.exports = function (api) {
api.cache(true);
return {
presets: [
'babel-preset-expo',
'module:react-native-dotenv',
],
sourceMaps: true,
plugins: [
'#babel/transform-react-jsx-source',
],
};
};
Lastly, use yarn to install your packages (yarn install) and to run your tests yarn test.
Expo automatically do setup of jest.
I think you must do 'Expo init newProject', then read .babelrc and package.json
Below is result of expo init.
It works well.
// package.json
{
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"eject": "expo eject",
"test": "node ./node_modules/jest/bin/jest.js --watchAll"
},
"jest": {
"preset": "jest-expo"
},
"dependencies": {
"#expo/samples": "2.1.1",
"expo": "^32.0.0",
"react": "16.5.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",
"react-navigation": "^3.0.9"
},
"devDependencies": {
"babel-preset-expo": "^5.0.0",
"jest-expo": "^32.0.0"
},
"private": true
}
// babel.config.js
module.exports = function(api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
};
};
Like MANY others, I'm trying to upgrade my RN app to React-Native 0.56 and Babel is making everything so easy! (Not the sarcasm here) I could update RN, React to the latest and make my app compile and work, but the tests (jest) are not working anymore.
But not all the test. I manage to solve almost all the different kinds of issue, expect this one:
meeting actions › creates CLEAR_CURRENT_ATTACHMENTS when clearning current attachments
TypeError: eventActions.refreshEvents.mockImplementation is not a function
90 |
91 | beforeEach(() => {
> 92 | eventActions.refreshEvents.mockImplementation(() => ({ type: DUMMY_TYPE }));
| ^
93 | MockDate.set(A_DATE);
94 | store = mockStore({
95 | authentication: {
at Object.<anonymous> (src/calendar/actions/__tests__/meetingActions-test.js:92:32)
Test file with the test failing (Remove all the others test to simplify the example)
import moment from 'moment';
import MockDate from 'mockdate';
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import getToken from 'authentication/selectors/tokenSelector';
import * as actions from '../meetingActions';
import * as eventActions from '../eventActions';
import { getPlannerComments } from '../../selectors/scheduleModificationSelectors';
const {
CLEAR_CURRENT_ATTACHMENTS,
} = actions;
jest.mock('../eventActions.js');
jest.mock('../../../authentication/selectors/tokenSelector');
jest.mock('../../selectors/scheduleModificationSelectors');
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
describe('meeting actions', () => {
const A_TOKEN = 'token12345';
const AN_ID = 'h1234';
const A_DATE = moment('2016-08-22T21:00:00.000Z');
const AN_MEETING_EVENT = {
start: A_DATE,
end: A_DATE,
guests: [],
newGuests: [],
newAttachments: [],
contactsToAddToGuestList: [],
groupsToAddToGuestList: [],
listsToAddToGuestList: [],
};
const AN_ACCOUNT = { id: AN_ID };
const DUMMY_TYPE = 'Dummy type';
const PLANNER_COMMENTS = {};
let store;
beforeEach(() => {
eventActions.refreshEvents.mockImplementation(() => ({ type: DUMMY_TYPE }));
MockDate.set(A_DATE);
store = mockStore({
authentication: {
token: A_TOKEN,
},
session: {
account: AN_ACCOUNT,
},
meeting: AN_MEETING_EVENT,
event: {
attendees: [],
},
});
getToken.mockImplementation(() => A_TOKEN);
getPlannerComments.mockImplementation(() => PLANNER_COMMENTS);
});
it('creates CLEAR_CURRENT_ATTACHMENTS when clearing current info', () =>
store.dispatch(actions.clearCurrentInfo())
.then(() => {
expect(store.getActions()).toContainEqual({ type: CLEAR_CURRENT_ATTACHMENTS });
}));
afterEach(() => {
MockDate.reset();
});
});
I have over 800 tests that failed because of this error. From what I understood of the problem, it comes from the fact that the jest.mock
jest.mock('../domain/Attachment') // For example
is not actually mocking the class. So Attachment looks like the normal class. And so the mockImplementation of a function cause the undefined is not a function
{ [Function: Record]
iconOf: [Function: iconOf],
iconColorOf: [Function: iconColorOf],
getCleanName: [Function: getCleanName],
open: [Function: _callee2],
parse: [Function: parse] }
Package.json
{
"name": "MY APP",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest",
"ios": "react-native run-ios",
"android": "react-native run-android",
"test:watch": "npm run test -- --watch",
"lint": "eslint src --max-warnings=0",
"install-dep": "npm install && cd ios && pod install"
},
"rnpm": {
"assets": [
"./src/main/themes/fonts/assets"
]
},
"jest": {
"moduleDirectories": [
"node_modules",
"src"
],
"transform": {
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
},
"preset": "react-native",
"collectCoverage": true,
"setupTestFrameworkScriptFile": "<rootDir>/setup-jasmine-env.js",
"moduleFileExtensions": [
"js",
"json",
"es6",
"ios.js",
"android.js"
],
"setupFiles": [
"./testenv.js"
],
"transformIgnorePatterns": [
"node_modules/?!(react-native)"
]
},
"dependencies": {
"bluebird": "^3.4.6",
"buffer": "^5.0.0",
"color": "^2.0.0",
"deepmerge": "^1.5.2",
"diacritics": "^1.2.3",
"immutable": "^3.8.2",
"linkify-it": "^2.0.3",
"lodash.debounce": "^4.0.8",
"moment": "^2.17.0",
"prop-types": "^15.6.0",
"react": "16.4.1",
"react-native": "0.56.0",
[...] // Lots of dependencies not related to the problem
"react-navigation": "1.5.11",
"react-redux": "^5.0.7",
"redux": "^3.7.2",
"redux-thunk": "^2.2.0",
"reselect": "^3.0.1",
"socket.io-client": "2.1.0"
},
"devDependencies": {
"#babel/core": "^7.0.0",
"babel-core": "^6.26.3",
"babel-eslint": "7.2.3",
"babel-jest": "^23.4.2",
"babel-plugin-module-resolver": "^3.1.1",
"babel-preset-react-native": "5.0.2",
"eslint": "^3.11.1",
"eslint-config-airbnb": "^13.0.0",
"eslint-import-resolver-babel-module": "5.0.0-beta.0",
"eslint-plugin-import": "^2.12.0",
"eslint-plugin-jsx-a11y": "^2.2.3",
"eslint-plugin-react": "^6.7.1",
"eslint-plugin-react-native": "^2.2.0",
"fetch-mock": "^5.1.2",
"istanbul": "0.4.5",
"jasmine-reporters": "^2.2.0",
"jest": "^23.5.0",
"jest-cli": "23.5.0",
"mockdate": "^2.0.1",
"moment-timezone": "^0.5.20",
"react-dom": "16.4.2",
"react-test-renderer": "16.4.2",
"redux-mock-store": "1.3.0",
"regenerator-runtime": "^0.12.1",
"remote-redux-devtools": "^0.5.7"
}
}
Note that like multiple issue in Jest, Babel and React-Native GitHubs, I have the Transform.
"transform": {
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
},
.babelrc
{
"presets": ["react-native"],
"plugins": [
[
"module-resolver",
{
"root": ["./src"],
"extensions": [".js", ".ios.js", ".android.js"]
}
]
]
}
Anyone has any idea?
If you look at the example here, you'll see that mockImplementation can only be called on a jest.fn() object. In your case, you'll need to first mock refreshEvents and then call mockImplementation on that mock:
...
eventActions.refreshEvents = jest.fn();
eventActions.refreshEvents.mockImplementation(() => ({ type: DUMMY_TYPE }));
...
If you want to keep the code you've posted as it is, then you'll have to create a subdirectory __mock__ adjacent to your mocked out module and create a file that mocks functions in that module within that subdirectory as explained here.
UPDATE:
jest.mock calls are automatically hoisted to the top of the file with the babel-jest transformer, which is what you need. Note that you are already using a jest transformer. From the docs:
If you are using the babel-jest transformer and want to use an additional code preprocessor, keep in mind that when "transform" is overwritten in any way the babel-jest is not loaded automatically anymore. If you want to use it to compile JavaScript code it has to be explicitly defined.
That means you have to explicitly include the babel-jest transform in your jest config in package.json. That should fix things.