Skip to content

Commit de83081

Browse files
authored
Enhancement/unit tests (#290)
* Added unit tests for the entire library
1 parent a7ae972 commit de83081

38 files changed

+8666
-69
lines changed

.circleci/config.yml

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,96 @@
11
version: 2
22
jobs:
3+
test_module:
4+
working_directory: ~/project
5+
docker:
6+
- image: circleci/node:8
7+
steps:
8+
- checkout:
9+
path: ~/project
10+
11+
- restore_cache:
12+
key: yarn-v1-{{ checksum "yarn.lock" }}-{{ arch }}
13+
14+
- restore_cache:
15+
key: node-v1-{{ checksum "package.json" }}-{{ arch }}
16+
17+
- run: yarn install
18+
19+
- save_cache:
20+
key: yarn-v1-{{ checksum "yarn.lock" }}-{{ arch }}
21+
paths:
22+
- ~/.cache/yarn
23+
24+
- save_cache:
25+
key: node-v1-{{ checksum "package.json" }}-{{ arch }}
26+
paths:
27+
- node_modules
28+
29+
- run:
30+
name: jest tests
31+
command: |
32+
mkdir -p test-results/jest
33+
yarn run test
34+
environment:
35+
JEST_JUNIT_OUTPUT: test-results/jest/junit.xml
36+
37+
- persist_to_workspace:
38+
root: ~/project
39+
paths:
40+
- node_modules
41+
42+
- store_test_results:
43+
path: test-results
44+
45+
- store_artifacts:
46+
path: test-results
47+
48+
test_sample:
49+
working_directory: ~/project
50+
docker:
51+
- image: circleci/node:8
52+
steps:
53+
- checkout:
54+
path: ~/project
55+
56+
- restore_cache:
57+
key: yarn-v1-{{ checksum "InstabugSample/yarn.lock" }}-{{ arch }}
58+
59+
- restore_cache:
60+
key: node-v1-{{ checksum "InstabugSample/package.json" }}-{{ arch }}
61+
62+
- run: cd InstabugSample && yarn install
63+
64+
- save_cache:
65+
key: yarn-v1-{{ checksum "InstabugSample/yarn.lock" }}-{{ arch }}
66+
paths:
67+
- ~/.cache/yarn
68+
69+
- save_cache:
70+
key: node-v1-{{ checksum "InstabugSample/package.json" }}-{{ arch }}
71+
paths:
72+
- InstabugSample/node_modules
73+
74+
- run:
75+
name: jest tests
76+
command: |
77+
cd InstabugSample
78+
mkdir -p test-results/jest
79+
yarn test
80+
environment:
81+
JEST_JUNIT_OUTPUT: test-results/jest/junit.xml
82+
83+
- persist_to_workspace:
84+
root: ~/project
85+
paths:
86+
- InstabugSample/node_modules
87+
88+
- store_test_results:
89+
path: InstabugSample/test-results
90+
91+
- store_artifacts:
92+
path: InstabugSample/test-results
93+
394
publish:
495
macos:
596
xcode: "10.1.0"
@@ -10,11 +101,18 @@ jobs:
10101
- run: cd Escape/.build/release; cp -f Escape /usr/local/bin/escape
11102
- run: Escape react-native publish
12103

104+
105+
13106
workflows:
14107
version: 2
15108
publish:
16109
jobs:
110+
- test_module
111+
- test_sample
17112
- hold:
113+
requires:
114+
- test_module
115+
- test_sample
18116
type: approval
19117
filters:
20118
branches:
@@ -25,3 +123,4 @@ workflows:
25123
filters:
26124
branches:
27125
only: master
126+

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ DerivedData/
2929
xcuserdata/
3030

3131

32+
coverage/
3233
*node_modules/
3334
*Pods/
3435
*Podfile.lock

.npmignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ android/local.properties
1111
ios/.DS_Store
1212

1313
build/
14-
InstabugSample
14+
InstabugSample/
15+
node_modules/
16+
coverage/

InstabugSample/__tests__/App.js

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* @format
3+
* @lint-ignore-every XPLATJSCOPYRIGHT1
4+
*/
5+
6+
import 'react-native';
7+
import { NativeModules, Platform } from 'react-native';
8+
import '../../jest/mockInstabug';
9+
import Instabug from 'instabug-reactnative';
10+
import IBGEventEmitter from 'instabug-reactnative/utils/IBGEventEmitter';
11+
import IBGConstants from 'instabug-reactnative/utils/InstabugConstants';
12+
// import console = require('console');
13+
14+
jest.mock('../node_modules/instabug-reactnative/utils/XhrNetworkInterceptor', () => {
15+
return {
16+
enableInterception: jest.fn(),
17+
setOnDoneCallback: jest.fn()
18+
}
19+
})
20+
21+
describe('Test global error handler', () => {
22+
23+
const sendJSCrash = jest.spyOn(NativeModules.Instabug, 'sendJSCrash');
24+
25+
beforeEach(() => {
26+
// var InstabugUtils = require('../utils/InstabugUtils');
27+
});
28+
29+
it('should call sendJSCrash when platform is ios', () => {
30+
31+
Platform.OS = 'ios';
32+
const handler = global.ErrorUtils.getGlobalHandler();
33+
handler({ name: 'TypeError', message: 'This is a type error.' }, false);
34+
const expected = {
35+
message: 'TypeError - This is a type error.',
36+
os: 'ios',
37+
platform: 'react_native',
38+
exception: []
39+
}
40+
expect(sendJSCrash).toHaveBeenCalledWith(expected);
41+
42+
});
43+
44+
it('should call sendJSCrash when platform is android', () => {
45+
46+
Platform.OS = 'android';
47+
const handler = global.ErrorUtils.getGlobalHandler();
48+
handler({ name: 'TypeError', message: 'This is a type error.' }, false);
49+
const expected = {
50+
message: 'TypeError - This is a type error.',
51+
os: 'android',
52+
platform: 'react_native',
53+
exception: []
54+
}
55+
expect(sendJSCrash).toHaveBeenCalledWith(JSON.stringify(expected));
56+
57+
});
58+
59+
it('should emit event IBGSendUnhandledJSCrash when platform is android and onReportSubmitHandler is set', (done) => {
60+
61+
Platform.OS = 'android';
62+
Instabug._isOnReportHandlerSet = jest.fn(() => true);
63+
const handler = global.ErrorUtils.getGlobalHandler();
64+
IBGEventEmitter.addListener(IBGConstants.SEND_UNHANDLED_CRASH, (actual) => {
65+
const expected = {
66+
message: 'TypeError - This is a type error.',
67+
os: 'android',
68+
platform: 'react_native',
69+
exception: []
70+
};
71+
expect(actual).toEqual(expected);
72+
done();
73+
});
74+
handler({ name: 'TypeError', message: 'This is a type error.' }, false);
75+
76+
77+
});
78+
79+
80+
});

InstabugSample/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
"react-test-renderer": "16.6.3"
2020
},
2121
"jest": {
22-
"preset": "react-native"
22+
"preset": "react-native",
23+
"transformIgnorePatterns": [
24+
"node_modules/(?!react-native|react-navigation)/"
25+
]
2326
}
2427
}

0 commit comments

Comments
 (0)