-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Adds SDK unit tests and CI task (#105)
* WIP test setup * WIP test setup * mocked native bridge * mock async storage * setup tests done * mock asyncstorage * added basic unit tests * Cleanup * end of file new line * add more checks * prettier * add prettier job * add path * prettier * fix paths * setup working directory * working directory fix * working directory * fix prettier
- Loading branch information
1 parent
1b68259
commit a398d24
Showing
15 changed files
with
272 additions
and
172 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -18,4 +18,28 @@ jobs: | |
- name: Run npm Install | ||
run: npm install | ||
|
||
# TODO: Check Lint | ||
- name: Unit Tests | ||
run: npm run test | ||
|
||
# Checks code formatting, fails if there are changes after applying prettier. | ||
# Based on this example here: | ||
# https://github.com/creyD/prettier_action?tab=readme-ov-file#example-4-dry-run | ||
prettier: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
with: | ||
# Make sure the actual branch is checked out when running on pull requests | ||
ref: ${{ github.head_ref }} | ||
# Make sure the value of GITHUB_TOKEN will not be persisted in repo's config | ||
persist-credentials: false | ||
|
||
- name: Prettify code | ||
uses: creyD/[email protected] | ||
with: | ||
# "dry" causes that if any file is modified, the job fails | ||
dry: True | ||
# "write" performs changes in place | ||
prettier_options: --write sdk/**/*.js sdk/**/*.ts sdk/**/*.tsx | ||
github_token: ${{ secrets.PERSONAL_GITHUB_TOKEN }} |
17 changes: 17 additions & 0 deletions
17
sdk/__mocks__/@react-native-async-storage/async-storage.js
This file contains 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 @@ | ||
export * from '@react-native-async-storage/async-storage/jest/async-storage-mock'; | ||
|
||
var storage = {}; | ||
|
||
export default { | ||
getItem: (item, value = null) => { | ||
return new Promise((resolve, reject) => { | ||
storage[item] ? resolve(storage[item]) : resolve(value); | ||
}); | ||
}, | ||
setItem: (item, value) => { | ||
return new Promise((resolve, reject) => { | ||
storage[item] = value; | ||
resolve(value); | ||
}); | ||
} | ||
}; |
This file contains 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 @@ | ||
import { NativeModules } from 'react-native'; | ||
|
||
NativeModules.RaygunNativeBridge = { | ||
DEVICE_ID: '1234567890' | ||
}; |
This file contains 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,60 @@ | ||
import AsyncStorage from '@react-native-async-storage/async-storage'; | ||
import { init, sendError } from '../src/RaygunClient'; | ||
import { RaygunClientOptions } from '../src/Types'; | ||
|
||
describe('RaygunClient', () => { | ||
beforeAll(() => { | ||
const options: RaygunClientOptions = { | ||
apiKey: 'ABCD', | ||
version: '1.2.3', | ||
logLevel: 'off', | ||
enableCrashReporting: true, | ||
enableRealUserMonitoring: false, | ||
disableNativeCrashReporting: true | ||
}; | ||
init(options); | ||
|
||
global.fetch = jest.fn(() => | ||
Promise.resolve({ | ||
status: 200 | ||
}) | ||
); | ||
}); | ||
|
||
beforeEach(() => { | ||
fetch.mockClear(); | ||
}); | ||
|
||
it('should send error correctly', async () => { | ||
const error = new Error('Test error'); | ||
await sendError(error); | ||
|
||
// fetch should be called once | ||
expect(fetch).toHaveBeenCalledTimes(1); | ||
|
||
// Check url correct | ||
expect(fetch.mock.calls[0][0]).toBe('https://api.raygun.com/entries?apiKey=ABCD'); | ||
|
||
// Capture body from fetch and check if correct | ||
const body = JSON.parse(fetch.mock.calls[0][1].body); | ||
expect(body.Details.Error.Message).toBe('Test error'); | ||
|
||
// Check if the version is correct | ||
expect(body.Details.Version).toBe('1.2.3'); | ||
}); | ||
|
||
it('should fail to send error', async () => { | ||
fetch.mockImplementationOnce(() => Promise.reject('API is down')); | ||
const error = new Error('Failed error'); | ||
await sendError(error); | ||
|
||
expect(fetch).toHaveBeenCalledTimes(1); | ||
|
||
// failed to send error should be stored in AsyncStorage | ||
const storedErrors = await AsyncStorage.getItem('raygun4reactnative_local_storage'); | ||
expect(storedErrors).not.toBeNull(); | ||
|
||
const errors = JSON.parse(storedErrors); | ||
expect(errors[0].Details.Error.Message).toBe('Failed error'); | ||
}); | ||
}); |
This file contains 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,4 @@ | ||
module.exports = { | ||
presets: ['module:metro-react-native-babel-preset'], | ||
plugins: ['babel-plugin-syntax-hermes-parser'] | ||
}; |
This file contains 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,10 +1,10 @@ | ||
module.exports = { | ||
testEnvironment: 'node', | ||
roots: ['<rootDir>/src'], | ||
preset: 'react-native', | ||
transform: { | ||
'^.+\\.tsx?$': 'ts-jest' | ||
'^.+\\.(js|jsx|ts|tsx)$': ['babel-jest', { rootMode: 'upward' }] | ||
}, | ||
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$', | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'], | ||
clearMocks: true | ||
transformIgnorePatterns: [ | ||
'node_modules/(?!(react-native|@react-native|react-navigation|@react-navigation|@react-native-community|@react-native-firebase|@react-navigation/stack|@react-navigation/bottom-tabs|@react-navigation/drawer|@react-navigation/native|@react-navigation/material-bottom-tabs|@react-navigation/material-top-tabs|@react-navigation/stack|@react-navigation/web))' | ||
], | ||
setupFiles: ['./__mocks__/RaygunNativeBridge.js'] | ||
}; |
This file contains 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 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.