Skip to content

Commit

Permalink
test: configuring jest files to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-logan committed Sep 3, 2024
1 parent 77b03bb commit 4ac4011
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 21 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ module.exports = {
root: true,
extends: ["@react-native", "universe/native", "plugin:react/jsx-runtime"],
ignorePatterns: [".eslintrc.js"],
overrides: [
{
files: ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"],
extends: ["plugin:testing-library/react"],
},
],
rules: {
"react-hooks/exhaustive-deps": "warn",
"no-console": "warn",
Expand Down
20 changes: 4 additions & 16 deletions __tests__/App.test.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
/**
* @format
*/

import "react-native";
import { it } from "@jest/globals";
import renderer from "react-test-renderer";

import App from "../App";

// Note: import explicitly to use the types shiped with jest.

// Note: test renderer must be required after react-native.

it("renders correctly", () => {
renderer.create(<App />);
describe("App", () => {
it("should render correctly", () => {
expect(true).toBeTruthy();
});
});
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/** @type {import("jest").Config} */
module.exports = {
preset: "react-native",
setupFilesAfterEnv: ["./jest/setupFilesAfterEnv.ts"],
setupFiles: ["./jest/setupFiles.js"],
};
33 changes: 33 additions & 0 deletions jest/setupFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// include this line for mocking react-native-gesture-handler
import "react-native-gesture-handler/jestSetup";

// include this section and the NativeAnimatedHelper section for mocking react-native-reanimated
jest.mock("react-native-reanimated", () => {
const Reanimated = require("react-native-reanimated/mock");

// The mock for `call` immediately calls the callback which is incorrect
// So we override it with a no-op
Reanimated.default.call = () => {};

return Reanimated;
});

// Silence the warning: Animated: `useNativeDriver` is not supported because the native animated module is missing
jest.mock("react-native/Libraries/Animated/NativeAnimatedHelper");

const mockNavigation = jest.fn();
const mockGoBack = jest.fn();
jest.mock("@react-navigation/native", () => {
const actualNavigation = jest.requireActual("@react-navigation/native");
return {
...actualNavigation,
useNavigation: () => ({
navigate: mockNavigation,
goBack: mockGoBack,
}),
};
});

jest.mock("@react-native-async-storage/async-storage", () =>
require("@react-native-async-storage/async-storage/jest/async-storage-mock")
);
1 change: 1 addition & 0 deletions jest/setupFilesAfterEnv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "@testing-library/react-native/extend-expect";
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest",
"prettier": "prettier --write .",
"doctor": "npx @react-native-community/cli doctor",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx"
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"test": "jest --verbose",
"test:coverage": "jest --coverage",
"test:verbose": "jest --verbose",
"test:verbose:coverage": "jest --verbose --coverage",
"test:watch": "jest --watchAll"
},
"dependencies": {
"@react-native-async-storage/async-storage": "^1.19.3",
Expand Down Expand Up @@ -50,8 +54,10 @@
"@react-native/eslint-config": "0.74.85",
"@react-native/metro-config": "0.74.85",
"@react-native/typescript-config": "0.74.85",
"@testing-library/react-native": "^12.6.1",
"@tsconfig/react-native": "^3.0.0",
"@types/crypto-js": "^4.1.3",
"@types/jest": "^29.5.12",
"@types/react": "^18.2.6",
"@types/react-native-base64": "^0.2.1",
"@types/react-native-vector-icons": "^6.4.16",
Expand All @@ -60,6 +66,7 @@
"babel-jest": "^29.6.3",
"eslint": "^8",
"eslint-config-universe": "^13.0.0",
"eslint-plugin-testing-library": "^6.3.0",
"jest": "^29.6.3",
"metro-react-native-babel-preset": "0.76.8",
"prettier": "2.8.8",
Expand Down
11 changes: 11 additions & 0 deletions src/components/Loading/__tests__/Loading.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { render, screen } from "@testing-library/react-native";

import Loading from "..";

describe("Loading Component", () => {
it("should render correctly", () => {
render(<Loading />);

expect(screen.getByTestId(/loading-indicator/)).toBeTruthy();
});
});
1 change: 1 addition & 0 deletions src/components/Loading/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default function Loading(): JSX.Element {

return (
<ActivityIndicator
testID="loading-indicator"
style={[styles.indicator, { backgroundColor }]}
size={64}
color="#74b9ff"
Expand Down
55 changes: 52 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2204,6 +2204,15 @@
deepmerge "^4.3.1"
svgo "^3.0.2"

"@testing-library/react-native@^12.6.1":
version "12.6.1"
resolved "https://registry.yarnpkg.com/@testing-library/react-native/-/react-native-12.6.1.tgz#dbb6b00246a3cffbffa18bbbb618fab968c0de28"
integrity sha512-ykXKeQEkqJ/lk0dqGfEspODjV5skmUh/6vVtyGZQsneFrcqGZJrO4uGhmChICLbnEKkstxXkjXeyoB7z8k9InQ==
dependencies:
jest-matcher-utils "^29.7.0"
pretty-format "^29.7.0"
redent "^3.0.0"

"@trysound/sax@0.2.0":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@trysound/sax/-/sax-0.2.0.tgz#cccaab758af56761eb7bf37af6f03f326dd798ad"
Expand Down Expand Up @@ -2283,6 +2292,14 @@
dependencies:
"@types/istanbul-lib-report" "*"

"@types/jest@^29.5.12":
version "29.5.12"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-29.5.12.tgz#7f7dc6eb4cf246d2474ed78744b05d06ce025544"
integrity sha512-eDC8bTvT/QhYdxJAulQikueigY5AsdBRH2yDKW3yveW7svY3+DzN84/2NUgkw10RTiJbWqZrTtoGVdYlvFJdLw==
dependencies:
expect "^29.0.0"
pretty-format "^29.0.0"

"@types/json-schema@^7.0.9":
version "7.0.15"
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.15.tgz#596a1747233694d50f6ad8a7869fcb6f56cf5841"
Expand Down Expand Up @@ -2482,7 +2499,7 @@
"@typescript-eslint/types" "7.18.0"
"@typescript-eslint/typescript-estree" "7.18.0"

"@typescript-eslint/utils@^5.10.0":
"@typescript-eslint/utils@^5.10.0", "@typescript-eslint/utils@^5.58.0":
version "5.62.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/utils/-/utils-5.62.0.tgz#141e809c71636e4a75daa39faed2fb5f4b10df86"
integrity sha512-n8oxjeb5aIbPFEtmQxQYOLI0i9n5ySBEY/ZEHHZqKQSFnxio1rv6dthascc9dLuwrL0RC5mPCxB7vnAVGAYWAQ==
Expand Down Expand Up @@ -3906,6 +3923,13 @@ eslint-plugin-react@^7.30.1, eslint-plugin-react@^7.34.1:
string.prototype.matchall "^4.0.11"
string.prototype.repeat "^1.0.0"

eslint-plugin-testing-library@^6.3.0:
version "6.3.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-testing-library/-/eslint-plugin-testing-library-6.3.0.tgz#9c31a9941a860efdff3c06180151ab9c8142f685"
integrity sha512-GYcEErTt6EGwE0bPDY+4aehfEBpB2gDBFKohir8jlATSUvzStEyzCx8QWB/14xeKc/AwyXkzScSzMHnFojkWrA==
dependencies:
"@typescript-eslint/utils" "^5.58.0"

eslint-scope@5.1.1, eslint-scope@^5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-5.1.1.tgz#e786e59a66cb92b3f6c1fb0d508aab174848f48c"
Expand Down Expand Up @@ -4061,7 +4085,7 @@ exit@^0.1.2:
resolved "https://registry.yarnpkg.com/exit/-/exit-0.1.2.tgz#0632638f8d877cc82107d30a0fff1a17cba1cd0c"
integrity sha512-Zk/eNKV2zbjpKzrsQ+n1G6poVbErQxJ0LBOJXaKZ1EViLzH+hrLu9cdXI4zw9dBQJslwBEpbQ2P1oS7nDxs6jQ==

expect@^29.7.0:
expect@^29.0.0, expect@^29.7.0:
version "29.7.0"
resolved "https://registry.yarnpkg.com/expect/-/expect-29.7.0.tgz#578874590dcb3214514084c08115d8aee61e11bc"
integrity sha512-2Zks0hf1VLFYI1kbh0I5jP3KHHyCHpkfyHBzsSXRFgl/Bg9mWYfMW8oD+PdMPlEwy5HNsR9JutYy6pMeOh61nw==
Expand Down Expand Up @@ -4562,6 +4586,11 @@ imurmurhash@^0.1.4:
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha512-JmXMZ6wuvDmLiHEml9ykzqO6lwFbof0GG4IkcGaENdCRDDmMVnny7s5HsIgHCbaq0w2MyPhDqkhTUgS2LU2PHA==

indent-string@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/indent-string/-/indent-string-4.0.0.tgz#624f8f4497d619b2d9768531d58f4122854d7251"
integrity sha512-EdDDZu4A2OyIK7Lr/2zG+w5jmbuk1DVBnEwREQvBzspBJkCEbRa8GxU1lghYcaGJCnRWibjDXlq779X1/y5xwg==

inflight@^1.0.4:
version "1.0.6"
resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9"
Expand Down Expand Up @@ -5878,6 +5907,11 @@ mimic-fn@^2.1.0:
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-2.1.0.tgz#7ed2c2ccccaf84d3ffcb7a69b57711fc2083401b"
integrity sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==

min-indent@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/min-indent/-/min-indent-1.0.1.tgz#a63f681673b30571fbe8bc25686ae746eefa9869"
integrity sha512-I9jwMn07Sy/IwOj3zVkVik2JTvgpaykDZEigL6Rx6N9LbMywwUSMtxET+7lVoDLLd3O3IXwJwvuuns8UB/HeAg==

minimatch@^3.0.2, minimatch@^3.0.4, minimatch@^3.0.5, minimatch@^3.1.1, minimatch@^3.1.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.1.2.tgz#19cd194bfd3e428f049a70817c038d89ab4be35b"
Expand Down Expand Up @@ -6352,7 +6386,7 @@ pretty-format@^26.5.2, pretty-format@^26.6.2:
ansi-styles "^4.0.0"
react-is "^17.0.1"

pretty-format@^29.7.0:
pretty-format@^29.0.0, pretty-format@^29.7.0:
version "29.7.0"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-29.7.0.tgz#ca42c758310f365bfa71a0bda0a807160b776812"
integrity sha512-Pdlw/oPxN+aXdmM9R00JVC9WVFoCLTKJvDVLgmJ+qAffBMxsV85l/Lu7sNx4zSzPyoL2euImuEwHhOXdEgNFZQ==
Expand Down Expand Up @@ -6694,6 +6728,14 @@ recast@^0.21.0:
source-map "~0.6.1"
tslib "^2.0.1"

redent@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/redent/-/redent-3.0.0.tgz#e557b7998316bb53c9f1f56fa626352c6963059f"
integrity sha512-6tDA8g98We0zd0GvVeMT9arEOnTw9qM03L9cJXaCjrip1OO764RDBLBfrB4cwzNGDj5OA5ioymC9GkizgWJDUg==
dependencies:
indent-string "^4.0.0"
strip-indent "^3.0.0"

reflect.getprototypeof@^1.0.4:
version "1.0.6"
resolved "https://registry.yarnpkg.com/reflect.getprototypeof/-/reflect.getprototypeof-1.0.6.tgz#3ab04c32a8390b770712b7a8633972702d278859"
Expand Down Expand Up @@ -7263,6 +7305,13 @@ strip-final-newline@^2.0.0:
resolved "https://registry.yarnpkg.com/strip-final-newline/-/strip-final-newline-2.0.0.tgz#89b852fb2fcbe936f6f4b3187afb0a12c1ab58ad"
integrity sha512-BrpvfNAE3dcvq7ll3xVumzjKjZQ5tI1sEUIKr3Uoks0XUl45St3FlatVqef9prk4jRDzhW6WZg+3bk93y6pLjA==

strip-indent@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/strip-indent/-/strip-indent-3.0.0.tgz#c32e1cee940b6b3432c771bc2c54bcce73cd3001"
integrity sha512-laJTa3Jb+VQpaC6DseHhF7dXVqHTfJPCRDaEbid/drOhgitgYku/letMUqOXFoWV0zIIUbjpdH2t+tYj4bQMRQ==
dependencies:
min-indent "^1.0.0"

strip-json-comments@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-3.1.1.tgz#31f1281b3832630434831c310c01cccda8cbe006"
Expand Down

0 comments on commit 4ac4011

Please sign in to comment.