From cc76406c52e534c709e0081320586e4eafb2c98d Mon Sep 17 00:00:00 2001 From: JoseLion Date: Fri, 26 Apr 2024 22:23:13 -0500 Subject: [PATCH] chore(tests): Use fake timers --- .mocharc.json | 3 ++- package.json | 2 +- test/hooks.ts | 6 ++++++ test/setup.ts | 11 +++++++++++ test/unit/lib/mockNative.test.tsx | 2 -- test/unit/register.test.tsx | 17 +++++++++++++++-- tsconfig.json | 2 +- 7 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 test/setup.ts diff --git a/.mocharc.json b/.mocharc.json index 38b1eff..14b8fa3 100644 --- a/.mocharc.json +++ b/.mocharc.json @@ -1,10 +1,11 @@ { "$schema": "https://json.schemastore.org/mocharc", - "exit": true, "extension": ["ts", "tsx"], "recursive": true, "require": [ "ts-node/register", + "src/register.ts", + "test/setup.ts", "test/hooks.ts" ], "spec": ["test/**/*.test.*"] diff --git a/package.json b/package.json index dcb256e..e33ab5e 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "compile": "tsc", "lint": "eslint . --report-unused-disable-directives", "release": "semantic-release", - "test": "RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS=true NODE_ENV=test mocha" + "test": "NODE_ENV=test mocha" }, "packageManager": "yarn@4.1.1", "dependencies": { diff --git a/test/hooks.ts b/test/hooks.ts index ed79f3c..080954f 100644 --- a/test/hooks.ts +++ b/test/hooks.ts @@ -5,5 +5,11 @@ export function mochaHooks(): Mocha.RootHookObject { afterEach() { Sinon.restore(); }, + beforeEach() { + Sinon.useFakeTimers({ + advanceTimeDelta: 0, + shouldAdvanceTime: true, + }); + }, }; } diff --git a/test/setup.ts b/test/setup.ts new file mode 100644 index 0000000..030ffe0 --- /dev/null +++ b/test/setup.ts @@ -0,0 +1,11 @@ +import { userEvent } from "@testing-library/react-native"; +import Sinon from "sinon"; + +process.env.RNTL_SKIP_AUTO_DETECT_FAKE_TIMERS = "true"; + +const newUserEvent = userEvent.setup({ + advanceTimers: delay => Sinon.clock.tickAsync(delay).then(), + delay: 0, +}); + +Object.assign(userEvent, newUserEvent); diff --git a/test/unit/lib/mockNative.test.tsx b/test/unit/lib/mockNative.test.tsx index 834bab8..fea6205 100644 --- a/test/unit/lib/mockNative.test.tsx +++ b/test/unit/lib/mockNative.test.tsx @@ -1,5 +1,3 @@ -import "../../../src/register"; - import { expect } from "@assertive-ts/core"; import { render } from "@testing-library/react-native"; import { ReactElement, useEffect, useRef, useState } from "react"; diff --git a/test/unit/register.test.tsx b/test/unit/register.test.tsx index e4e7cfc..275688d 100644 --- a/test/unit/register.test.tsx +++ b/test/unit/register.test.tsx @@ -1,5 +1,3 @@ -import "../../src/register"; - import { expect } from "@assertive-ts/core"; import { render, waitFor, userEvent } from "@testing-library/react-native"; import { ReactElement, useCallback, useRef, useState } from "react"; @@ -9,6 +7,7 @@ import { Rect, Svg } from "react-native-svg"; function TestScreen(): ReactElement { const [animated, setAnimated] = useState(false); + const [greet, setGreet] = useState("Hello!"); const enterLeft = useRef(new Animated.Value(100, { useNativeDriver: true })).current; const movePoint = useRef(new Animated.ValueXY({ x: 0, y: 0 }, { useNativeDriver: true })).current; @@ -32,6 +31,10 @@ function TestScreen(): ReactElement { }); }, []); + const changeGreet = useCallback(() => { + setGreet("I said hello!!!"); + }, []); + return ( @@ -59,6 +62,8 @@ function TestScreen(): ReactElement { {`Animated view: ${animated}`} +