React-Native tests failed - reactjs

i searched on stackoverflow but i didn't find my problem.
I dev an apps only on android, using React-Native-cookies's native module, and now i have to create some unit test. I am using Jest, and i make one.
But now, everytime i want to test it, it is not possible because i didn't configured my xCode project. It doesn't take sense because i haven't changed evything on the ios app. Here is my error :
> jest
PASS __tests__\index.ios.js
FAIL __tests__\index.android.js
● Test suite failed to run
Invariant Violation: react-native-cookies: Add RNCookieMangerIOS.h and RNCookieManagerIOS.m to your Xcode project
at invariant (node_modules/invariant/invariant.js:42:15)
at Object.<anonymous> (node_modules/react-native-cookies/index.js:10:1)
at Object.<anonymous> (App/App/Cookie.js:7:25)
at Object.<anonymous> (App/App/Mattermost.js:11:13)
FAIL App\App\__tests__\Mattermost.test.js
● Test suite failed to run
Invariant Violation: react-native-cookies: Add RNCookieMangerIOS.h and RNCookieManagerIOS.m to your Xcode project
at invariant (node_modules/invariant/invariant.js:42:15)
at Object.<anonymous> (node_modules/react-native-cookies/index.js:10:1)
at Object.<anonymous> (App/App/Cookie.js:7:25)
at Object.<anonymous> (App/App/Mattermost.js:11:13)
Test Suites: 2 failed, 1 passed, 3 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.547s, estimated 3s
Ran all test suites.
npm ERR! Test failed. See above for more details.
So i wonder if someone could help me? Thanks
EDIT : forgetted to post the unit test :
import Mattermost from "../Mattermost";
let mattermost = new Mattermost();
test('Rendering Test', () => {
mattermost.setState({
MMAUTHTOKEN : "test debug",
});
expect(mattermost.render.contain(<WebView ref={'webview'} automaticallyAdjustContentInsets={false} style={Style.webview} source={{uri: URLMATTERMOST}} javaScriptEnabled={true} onNavigationStateChange={this.onNavigationStateChange.bind(this)} startInLoadingState={true} scalesPageToFit={true}/>)).to.equal(true);
});

Related

React Native : Invariant Violation: `new NativeEventEmitter()` requires a non-null argument

I am writing jest test case for a particular file, the file has NativeModule import in it.
While running the test case, I am getting the following error:
● Test suite failed to run
Invariant Violation: `new NativeEventEmitter()` requires a non-null argument.
at invariant (node_modules/invariant/invariant.js:40:15)
at new NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:45:7)
at Object.<anonymous> (node_modules/react-native-quantum-metric-library/index.js:1:322)
After googling the above error, I did few changes by adding the following code inside the test file.
jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter');
After adding the above line then I got the following error:
Test suite failed to run
TypeError: Cannot set properties of undefined (setting 'eventType')
at Object.<anonymous> (node_modules/react-native-quantum-metric-library/index.js:1:367)
After googling again, I modified the above code with the following code.
jest.mock('react-native/Libraries/EventEmitter/NativeEventEmitter.js', () => {
const {EventEmitter} = require('events');
return EventEmitter;
});
After above modification I am getting the below error.
● Test suite failed to run
TypeError: _reactNative.NativeEventEmitter is not a constructor
at Object.<anonymous> (node_modules/react-native-quantum-metric-library/index.js:1:322)
Can anyone please help me to resolve the above issue.

Receiving error 'SyntaxError: Unexpected identifier' for import statement using Jest with React

Previously, Jest worked fine with import statements; however, upon installing the dependency 'react-router-transition' I am unable to perfrom any tests and receive the following error:
`
FAIL src/pages/Costs/Costs.test.js
● Test suite failed to run
/Users/***/Code/cmp/cmp-client-portal/node_modules/react-router-transition/lib/index.js:5
import React, { cloneElement, createElement, useEffect, useRef, useState } from 'react';
^^^^^
SyntaxError: Unexpected identifier
> 1 | import { spring } from "react-router-transition"
| ^
2 | // we need to map the `scale` prop we define below
3 | // to the transform style property
4 | function mapStyles(styles) {
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> (src/utils/transitions/fadeInOutBounce.js:1:1)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 2.83s
Ran all test suites matching /src/pages/Costs/i.`
Solution was to update react scripts to the latest version.
The old version of react-scripts clearly had a misconfigured babel loader for jest, and was therefore not correctly transforming code during testing.

How can I fix my test suites failure to run because of the tcomb-form-native library for React Native?

I am trying to perform unit testing and integration testing for a React Native application. When running yarn test --coverage several of my test suites fail to run because of the tcomb-form-native library.
I receive this specific error:
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import t from "./lib";
SyntaxError: Unexpected identifier
13 | // import { gray } from 'ansi-colors';
14 |
> 15 | import t from 'tcomb-form-native';
| ^
16 |
17 | const Form = t.form.Form;
18 |
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
at Object.<anonymous> (screens/ReservationScreen.js:15:1)
I updated the package.json file following the Tests section from the library's GitHub, https://github.com/gcanti/tcomb-form-native, but continue to get the same error.
I'm confused by this line from the tcomb-form-native ReadMe Tests description:
"ios.js" <<<<<<<<<<<< this needs to be defined!
I created the React Native app using Expo so I don't have an ios.js file. The only difference under the "jest": from the example code in the GitHub is that I also have "preset": "jest-expo", defined.
What changes do I need to make to my package.json file or my test suites in order for my test suites to run? (They don't have to pass, just simply run.)
Edit:
I tried adding the following code to package.json based on the top answer from https://github.com/gcanti/tcomb-form-native/issues/443:
"transformIgnorePatterns": [
"node_modules/(?!react-native|tcomb-form-native)"
]`
I added the code without the original suggested edits from the library's GitHub ReadMe and added it IN ADDITION to the suggested edits. I am still receiving SyntaxError: Unexpected identifier when I run tests.
Edit #2:
I changed changed import t from 'tcomb-form-native'; to var t = require('tcomb-form-native'); and I'm still receiving SyntaxError: Unexpected identifier.

Yarn test fails because of 'createEvent' not found (create-react-app)

I've a simple react project created with create-react-app. Just after created the project the yarn test command worked fine, then I started to add some actual react code and I eventually got this error, just after the test is executed:
PASS src/test/App.test.js
✓ renders without crashing (28ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.729s, estimated 1s
Ran all test suites related to changed files.
Watch Usage
› Press a to run all tests.
› Press p to filter by a filename regex pattern.
› Press t to filter by a test name regex pattern.
› Press q to quit watch mode.
› Press Enter to trigger a test run.
/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react-scripts/scripts/test.js:20
throw err;
^
TypeError: Cannot read property 'createEvent' of undefined
at Object.invokeGuardedCallbackDev (/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react-dom/cjs/react-dom.development.js:577:25)
at invokeGuardedCallback (/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react-dom/cjs/react-dom.development.js:436:27)
at renderRoot (/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react-dom/cjs/react-dom.development.js:10386:7)
at performWorkOnRoot (/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react-dom/cjs/react-dom.development.js:11000:24)
at performWork (/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react-dom/cjs/react-dom.development.js:10952:7)
at requestWork (/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react-dom/cjs/react-dom.development.js:10861:7)
at scheduleWorkImpl (/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react-dom/cjs/react-dom.development.js:10744:11)
at scheduleWork (/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react-dom/cjs/react-dom.development.js:10706:12)
at Object.enqueueSetState (/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react-dom/cjs/react-dom.development.js:6204:7)
at App.Object.<anonymous>.Component.setState (/Users/mpegorar/Development/SpringApps/fluxy_frontend/node_modules/react/cjs/react.development.js:226:16)
at then.then.jsonData (/Users/mpegorar/Development/SpringApps/fluxy_frontend/src/components/App.js:25:9)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
The test is actually executed and it changes if I put some error inside, but the tool exit while usually it would stay up and running continuing executing tests based on the code.
The line in my code that seems to cause this error is the set state here described:
componentDidMount() {
const headers = new Headers()
headers.append('Content-Type', 'application/hal+json')
fetch(`${url}/listEnvironmentResources`, { headers }).then(
function(response){
return response.json();
}
).then(jsonData => {
this.setState({environmentRecords: jsonData._embedded.environmentRecordResources});
});
}
Previously the test was not working because of this error, fixed as for the accepted answer.
The only reference I found about the error is this, but it didn't solve my problem.
Any idea how can I fix this? Looks like some missing dependency but can't find which one.

Jest issue - ReactElement.js: Failed to get mock metadata

I've a very simple test
/** #jsx React.DOM */
var store = require('../simpleStore');
var TestUtils = require('react/addons').addons.TestUtils;
describe("my tests",function(){
it('simple test',function(){
var x=10,
y=20;
expect(x).not.toBe(y);
})
})
Test works fine as long as I dont require TestUtils.
The moment I add any node_module reference, I start seeing below error
Using Jest CLI v0.8.0, jasmine1
FAIL build/stores/__tests__/simple-test.js
● Runtime Error
Error: ../stores/__tests__/simple-test.js:
../../node_modules/react/addons.js:
../../node_modules/react/lib/ReactWithAddons.js:
../../node_modules/react/lib/LinkedStateMixin.js:
../../node_modules/react/lib/ReactLink.js:
../../node_modules/react/lib/React.js:
../../node_modules/react/lib/ReactDOM.js:
../../node_modules/react/lib/ReactDOMTextComponent.js:
../../node_modules/react/lib/ReactComponentBrowserEnvironment.js:
../../node_modules/react/lib/ReactDOMIDOperations.js:
../../node_modules/react/lib/ReactMount.js:
../../node_modules/react/lib/ReactElement.js: Failed to get mock metadata:
../../node_modules/react/lib/canDefineProperty.js
npm ERR! Test failed. See above for more details.
I'm sure a lot of people are using jest and I'm missing something silly..appreaciate any ideas to fix this issue?
Try to disable mocking for react - in your jset configuraction in package.json:
"unmockedModulePathPatterns": [
"<rootDir>/node_modules/react",
"<rootDir>/node_modules/fbjs"
]

Resources