Skip to content

Commit 2129c98

Browse files
committed
chore: updates files and library to testing library
1 parent 80327be commit 2129c98

File tree

6 files changed

+87
-36
lines changed

6 files changed

+87
-36
lines changed

.npmignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ template/*.iml
3333
#
3434
.vscode/
3535
template/.env
36-
template/.env*
36+
template/.env.prod
3737

3838
# node.js
3939
#

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hmarques98/react-native-template-typescript",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "A minimal template with good architecture and common packages to let you focus on writing features right away.",
55
"scripts": {},
66
"files": [

template/.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ENV=dev
2+
SENTRY_DSN="https://19f08009853b4b1a851090e742cf5fa3@o658389.ingest.sentry.io/5763830"
3+
BASE_URL=https://fakerapi.it/api/v1/

template/jest.config.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1+
// eslint-disable-next-line @typescript-eslint/no-var-requires
12
const jestPreset = require('@testing-library/react-native/jest-preset');
23

34
module.exports = {
4-
preset: 'jest-expo',
5-
setupFiles: [...jestPreset.setupFiles],
6-
setupFilesAfterEnv: ['./jest.setup.js'],
5+
testEnvironment: 'node',
6+
preset: 'react-native',
7+
setupFiles: [
8+
'./jest.setup.js',
9+
'./node_modules/react-native-gesture-handler/jestSetup.js',
10+
'./node_modules/react-native/jest/setup.js',
11+
'./node_modules/react-native/Libraries/Utilities/__mocks__/BackHandler.js',
12+
],
13+
setupFilesAfterEnv: ['@testing-library/jest-native/extend-expect'],
14+
clearMocks: true,
15+
moduleNameMapper: {
16+
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
17+
'<rootDir>/assetsTransformer.js',
18+
'\\.(css|less)$': '<rootDir>/assetsTransformer.js',
19+
'\\.svg': '<rootDir>/__mocks__/svgMocks.js',
20+
},
21+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
722
};

template/jest.setup.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import mockRNCNetInfo from '@react-native-community/netinfo/jest/netinfo-mock.js';
22
import mockAsyncStorage from '@react-native-async-storage/async-storage/jest/async-storage-mock';
3+
import React from 'react';
4+
import '@testing-library/jest-native/extend-expect';
5+
import { jest } from '@jest/globals';
6+
import { server } from './src/test/mocks/server';
37

48
jest.mock('@react-native-community/netinfo', () => mockRNCNetInfo);
59
jest.mock('@react-native-async-storage/async-storage', () => mockAsyncStorage);
@@ -9,6 +13,7 @@ jest.mock('@sentry/react-native', () => ({
913
}));
1014

1115
jest.mock('react-native-reanimated', () => {
16+
// eslint-disable-next-line @typescript-eslint/no-var-requires
1217
const Reanimated = require('react-native-reanimated/mock');
1318

1419
// The mock for `call` immediately calls the callback which is incorrect
@@ -32,6 +37,63 @@ global.requestAnimationFrame = (cb) => {
3237
};
3338

3439
jest.mock('react-native/Libraries/Components/Switch/Switch', () => {
40+
// eslint-disable-next-line @typescript-eslint/no-var-requires
3541
const mockComponent = require('react-native/jest/mockComponent');
3642
return mockComponent('react-native/Libraries/Components/Switch/Switch');
3743
});
44+
45+
jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');
46+
jest.mock('react-native-gesture-handler', () => {
47+
// eslint-disable-next-line @typescript-eslint/no-var-requires
48+
const View = require('react-native/Libraries/Components/View/View');
49+
return {
50+
Swipeable: View,
51+
DrawerLayout: View,
52+
State: {},
53+
ScrollView: View,
54+
Slider: View,
55+
Switch: View,
56+
TextInput: View,
57+
ToolbarAndroid: View,
58+
ViewPagerAndroid: View,
59+
DrawerLayoutAndroid: View,
60+
WebView: View,
61+
NativeViewGestureHandler: View,
62+
TapGestureHandler: View,
63+
FlingGestureHandler: View,
64+
ForceTouchGestureHandler: View,
65+
LongPressGestureHandler: View,
66+
PanGestureHandler: View,
67+
PinchGestureHandler: View,
68+
RotationGestureHandler: View,
69+
/* Buttons */
70+
RawButton: View,
71+
BaseButton: View,
72+
RectButton: View,
73+
BorderlessButton: View,
74+
/* Other */
75+
FlatList: View,
76+
gestureHandlerRootHOC: () => null,
77+
Directions: {},
78+
};
79+
});
80+
81+
//establish api mocking before all tests
82+
beforeAll(() => server.listen());
83+
84+
beforeEach(() => {
85+
global.fetch = jest.fn((...args) => {
86+
console.warn('global.fetch needs to be mocked in tests', ...args);
87+
throw new Error('global.fetch needs to be mocked in tests');
88+
});
89+
});
90+
91+
//clean up after the tests are finished
92+
afterAll(() => server.close());
93+
94+
afterEach(() => {
95+
global.fetch.mockRestore();
96+
//reset any requests handlers that we may add during the tests,
97+
//so they don't affect other tests.
98+
server.resetHandlers();
99+
});

template/package.json

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "template",
2+
"name": "StarWarsApp",
33
"version": "0.0.1",
44
"private": true,
55
"scripts": {
@@ -61,7 +61,7 @@
6161
"@storybook/addon-ondevice-actions": "^5.3.23",
6262
"@storybook/addon-ondevice-knobs": "^5.3.25",
6363
"@storybook/react-native": "^5.3.25",
64-
"@testing-library/jest-native": "^3.4.3",
64+
"@testing-library/jest-native": "^4.0.1",
6565
"@testing-library/react-hooks": "^5.1.0",
6666
"@testing-library/react-native": "^7.2.0",
6767
"@types/detox": "^17.14.0",
@@ -95,35 +95,6 @@
9595
"resolutions": {
9696
"@types/react": "^17"
9797
},
98-
"jest": {
99-
"testEnvironment": "node",
100-
"setupFiles": [
101-
"./jest.setup.js",
102-
"./node_modules/react-native-gesture-handler/jestSetup.js",
103-
"./node_modules/react-native/jest/setup.js",
104-
"./node_modules/react-native/Libraries/Utilities/__mocks__/BackHandler.js"
105-
],
106-
"setupFilesAfterEnv": [
107-
"@testing-library/jest-native/extend-expect"
108-
],
109-
"preset": "react-native",
110-
"moduleFileExtensions": [
111-
"ts",
112-
"tsx",
113-
"js",
114-
"jsx",
115-
"json",
116-
"node"
117-
],
118-
"moduleNameMapper": {
119-
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/assetsTransformer.js",
120-
"\\.(css|less)$": "/assetsTransformer.js",
121-
"\\.svg": "<rootDir>/__mocks__/svgMocks.js"
122-
},
123-
"transformIgnorePatterns": [
124-
"node_modules/(?!(jest-)?react-native|@react-native-community|@react-navigation)"
125-
]
126-
},
12798
"lint-staged": {
12899
"*.{js,ts,tsx}": [
129100
"eslint . --fix",

0 commit comments

Comments
 (0)