From 350e91eb53506629998c15376f18298e46f48e5a Mon Sep 17 00:00:00 2001 From: yanguoyu <841185308@qq.com> Date: Sun, 18 Sep 2022 09:48:01 +0800 Subject: [PATCH 1/9] fix: New instance for every test case to avoid side effect --- packages/neuron-wallet/tests/services/monitor.test.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/neuron-wallet/tests/services/monitor.test.ts b/packages/neuron-wallet/tests/services/monitor.test.ts index 166a03af4e..acc871486f 100644 --- a/packages/neuron-wallet/tests/services/monitor.test.ts +++ b/packages/neuron-wallet/tests/services/monitor.test.ts @@ -99,7 +99,7 @@ function wait(times: number) { }) } describe('base monitor', () => { - const monitor = new MonitorTest() + let monitor: MonitorTest beforeEach(() => { isLivingMock.mockReset() @@ -107,6 +107,9 @@ describe('base monitor', () => { }) describe('start monitor', () => { + beforeEach(() => { + monitor = new MonitorTest() + }) afterEach(async () => { await monitor.stopMonitor() }) @@ -125,7 +128,7 @@ describe('base monitor', () => { expect(restartMock).toHaveBeenCalled() }) it('isLiving timeout', async () => { - isLivingMock.mockImplementation(() => wait(200)) + isLivingMock.mockImplementation(() => wait(1000)) await monitor.startMonitor(100) await wait(200) expect(isLivingMock).toHaveBeenCalled() @@ -133,7 +136,7 @@ describe('base monitor', () => { }) it('not living wait restart', async () => { isLivingMock.mockResolvedValue(true).mockResolvedValueOnce(false) - restartMock.mockImplementation(() => wait(400)) + restartMock.mockImplementation(() => wait(1000)) await monitor.startMonitor(100) await wait(800) expect(isLivingMock).toHaveBeenCalled() @@ -142,7 +145,6 @@ describe('base monitor', () => { it('start monitor with first', async () => { isLivingMock.mockResolvedValue(true).mockResolvedValueOnce(false) await monitor.startMonitor(1000, true) - await wait(500) expect(isLivingMock).toHaveBeenCalled() expect(restartMock).toHaveBeenCalled() }) From 9ceb8b3da2644f2af546c492805b0bfad7338aee Mon Sep 17 00:00:00 2001 From: yanguoyu <841185308@qq.com> Date: Sun, 18 Sep 2022 10:17:17 +0800 Subject: [PATCH 2/9] fix: Add monitor name for test case --- packages/neuron-wallet/tests/services/monitor.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/neuron-wallet/tests/services/monitor.test.ts b/packages/neuron-wallet/tests/services/monitor.test.ts index acc871486f..c855931aa4 100644 --- a/packages/neuron-wallet/tests/services/monitor.test.ts +++ b/packages/neuron-wallet/tests/services/monitor.test.ts @@ -129,6 +129,7 @@ describe('base monitor', () => { }) it('isLiving timeout', async () => { isLivingMock.mockImplementation(() => wait(1000)) + monitor.name = 'isLiving timeout' await monitor.startMonitor(100) await wait(200) expect(isLivingMock).toHaveBeenCalled() @@ -137,6 +138,7 @@ describe('base monitor', () => { it('not living wait restart', async () => { isLivingMock.mockResolvedValue(true).mockResolvedValueOnce(false) restartMock.mockImplementation(() => wait(1000)) + monitor.name = 'not living wait restart' await monitor.startMonitor(100) await wait(800) expect(isLivingMock).toHaveBeenCalled() From e325bae8f6016a9b44f0024ade1019e0509ef33e Mon Sep 17 00:00:00 2001 From: yanguoyu <841185308@qq.com> Date: Sun, 18 Sep 2022 12:12:13 +0800 Subject: [PATCH 3/9] fix: Add logs for test case --- packages/neuron-wallet/tests/services/monitor.test.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/neuron-wallet/tests/services/monitor.test.ts b/packages/neuron-wallet/tests/services/monitor.test.ts index c855931aa4..7796246c85 100644 --- a/packages/neuron-wallet/tests/services/monitor.test.ts +++ b/packages/neuron-wallet/tests/services/monitor.test.ts @@ -134,15 +134,18 @@ describe('base monitor', () => { await wait(200) expect(isLivingMock).toHaveBeenCalled() expect(restartMock).toHaveBeenCalledTimes(1) + console.log('isLiving timeout end') }) it('not living wait restart', async () => { isLivingMock.mockResolvedValue(true).mockResolvedValueOnce(false) - restartMock.mockImplementation(() => wait(1000)) + restartMock.mockImplementation(() => wait(800)) monitor.name = 'not living wait restart' await monitor.startMonitor(100) await wait(800) expect(isLivingMock).toHaveBeenCalled() expect(restartMock).toHaveBeenCalledTimes(1) + await wait(400) + console.log('not living wait restart end') }) it('start monitor with first', async () => { isLivingMock.mockResolvedValue(true).mockResolvedValueOnce(false) From a86e6647d52fbf8a09b83f9200b563e3decc997c Mon Sep 17 00:00:00 2001 From: yanguoyu <841185308@qq.com> Date: Sun, 18 Sep 2022 17:32:58 +0800 Subject: [PATCH 4/9] fix: Move reset to inner beforeEach --- packages/neuron-wallet/tests/services/monitor.test.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/neuron-wallet/tests/services/monitor.test.ts b/packages/neuron-wallet/tests/services/monitor.test.ts index 7796246c85..76d543d740 100644 --- a/packages/neuron-wallet/tests/services/monitor.test.ts +++ b/packages/neuron-wallet/tests/services/monitor.test.ts @@ -101,14 +101,11 @@ function wait(times: number) { describe('base monitor', () => { let monitor: MonitorTest - beforeEach(() => { - isLivingMock.mockReset() - restartMock.mockReset() - }) - describe('start monitor', () => { beforeEach(() => { monitor = new MonitorTest() + isLivingMock.mockReset() + restartMock.mockReset() }) afterEach(async () => { await monitor.stopMonitor() From 97f7b97a2f15d09222e63b31b48186ebdc0bc311 Mon Sep 17 00:00:00 2001 From: yanguoyu <841185308@qq.com> Date: Sun, 18 Sep 2022 17:43:55 +0800 Subject: [PATCH 5/9] fix: move console --- packages/neuron-wallet/tests/services/monitor.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/neuron-wallet/tests/services/monitor.test.ts b/packages/neuron-wallet/tests/services/monitor.test.ts index 76d543d740..71bfe2bc03 100644 --- a/packages/neuron-wallet/tests/services/monitor.test.ts +++ b/packages/neuron-wallet/tests/services/monitor.test.ts @@ -129,9 +129,9 @@ describe('base monitor', () => { monitor.name = 'isLiving timeout' await monitor.startMonitor(100) await wait(200) + console.log('isLiving timeout end') expect(isLivingMock).toHaveBeenCalled() expect(restartMock).toHaveBeenCalledTimes(1) - console.log('isLiving timeout end') }) it('not living wait restart', async () => { isLivingMock.mockResolvedValue(true).mockResolvedValueOnce(false) @@ -139,10 +139,10 @@ describe('base monitor', () => { monitor.name = 'not living wait restart' await monitor.startMonitor(100) await wait(800) + console.log('not living wait restart end') expect(isLivingMock).toHaveBeenCalled() expect(restartMock).toHaveBeenCalledTimes(1) await wait(400) - console.log('not living wait restart end') }) it('start monitor with first', async () => { isLivingMock.mockResolvedValue(true).mockResolvedValueOnce(false) From d181a2ddaf70be218e705038be752d08a840f70a Mon Sep 17 00:00:00 2001 From: yanguoyu <841185308@qq.com> Date: Sun, 18 Sep 2022 19:26:17 +0800 Subject: [PATCH 6/9] fix: Fix test case --- .../tests/services/monitor.test.ts | 34 ++++++++----------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/packages/neuron-wallet/tests/services/monitor.test.ts b/packages/neuron-wallet/tests/services/monitor.test.ts index 71bfe2bc03..b6b83f4021 100644 --- a/packages/neuron-wallet/tests/services/monitor.test.ts +++ b/packages/neuron-wallet/tests/services/monitor.test.ts @@ -101,12 +101,13 @@ function wait(times: number) { describe('base monitor', () => { let monitor: MonitorTest + beforeEach(() => { + monitor = new MonitorTest() + isLivingMock.mockReset() + restartMock.mockReset() + }) + describe('start monitor', () => { - beforeEach(() => { - monitor = new MonitorTest() - isLivingMock.mockReset() - restartMock.mockReset() - }) afterEach(async () => { await monitor.stopMonitor() }) @@ -125,30 +126,25 @@ describe('base monitor', () => { expect(restartMock).toHaveBeenCalled() }) it('isLiving timeout', async () => { - isLivingMock.mockImplementation(() => wait(1000)) - monitor.name = 'isLiving timeout' - await monitor.startMonitor(100) - await wait(200) - console.log('isLiving timeout end') + isLivingMock.mockImplementation(() => wait(200)) + await monitor.startMonitor(200) + await wait(400) expect(isLivingMock).toHaveBeenCalled() expect(restartMock).toHaveBeenCalledTimes(1) }) it('not living wait restart', async () => { - isLivingMock.mockResolvedValue(true).mockResolvedValueOnce(false) - restartMock.mockImplementation(() => wait(800)) - monitor.name = 'not living wait restart' + isLivingMock.mockResolvedValue(false) + restartMock.mockImplementation(() => wait(1000)) await monitor.startMonitor(100) await wait(800) - console.log('not living wait restart end') expect(isLivingMock).toHaveBeenCalled() expect(restartMock).toHaveBeenCalledTimes(1) - await wait(400) }) it('start monitor with first', async () => { - isLivingMock.mockResolvedValue(true).mockResolvedValueOnce(false) - await monitor.startMonitor(1000, true) - expect(isLivingMock).toHaveBeenCalled() - expect(restartMock).toHaveBeenCalled() + isLivingMock.mockResolvedValue(false) + await monitor.startMonitor(10000, true) + expect(isLivingMock).toHaveBeenCalledTimes(1) + expect(restartMock).toHaveBeenCalledTimes(1) }) it('twice start monitor', async () => { isLivingMock.mockReset() From 3f993198377013353bc38f9bd43190181ee35bd6 Mon Sep 17 00:00:00 2001 From: yanguoyu <841185308@qq.com> Date: Sun, 18 Sep 2022 20:02:00 +0800 Subject: [PATCH 7/9] fix: Fix test case --- packages/neuron-wallet/tests/services/monitor.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/neuron-wallet/tests/services/monitor.test.ts b/packages/neuron-wallet/tests/services/monitor.test.ts index b6b83f4021..3c88e285ab 100644 --- a/packages/neuron-wallet/tests/services/monitor.test.ts +++ b/packages/neuron-wallet/tests/services/monitor.test.ts @@ -135,8 +135,9 @@ describe('base monitor', () => { it('not living wait restart', async () => { isLivingMock.mockResolvedValue(false) restartMock.mockImplementation(() => wait(1000)) + monitor.name = 'not living wait restart' await monitor.startMonitor(100) - await wait(800) + await wait(400) expect(isLivingMock).toHaveBeenCalled() expect(restartMock).toHaveBeenCalledTimes(1) }) From fb2d38f29aa37c822bf9b395d17efb2a006e5127 Mon Sep 17 00:00:00 2001 From: yanguoyu <841185308@qq.com> Date: Sun, 18 Sep 2022 21:20:03 +0800 Subject: [PATCH 8/9] fix: Wait test case execute. --- packages/neuron-wallet/tests/services/monitor.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/neuron-wallet/tests/services/monitor.test.ts b/packages/neuron-wallet/tests/services/monitor.test.ts index 3c88e285ab..c4d4a04cbf 100644 --- a/packages/neuron-wallet/tests/services/monitor.test.ts +++ b/packages/neuron-wallet/tests/services/monitor.test.ts @@ -129,6 +129,8 @@ describe('base monitor', () => { isLivingMock.mockImplementation(() => wait(200)) await monitor.startMonitor(200) await wait(400) + await monitor.stopMonitor() + await wait(400) expect(isLivingMock).toHaveBeenCalled() expect(restartMock).toHaveBeenCalledTimes(1) }) @@ -138,6 +140,8 @@ describe('base monitor', () => { monitor.name = 'not living wait restart' await monitor.startMonitor(100) await wait(400) + await monitor.stopMonitor() + await wait(400) expect(isLivingMock).toHaveBeenCalled() expect(restartMock).toHaveBeenCalledTimes(1) }) From 8b40161bfaf7ce09198d17139712c8467a81be20 Mon Sep 17 00:00:00 2001 From: yanguoyu <841185308@qq.com> Date: Sun, 18 Sep 2022 22:55:50 +0800 Subject: [PATCH 9/9] fix: Fix test case --- packages/neuron-wallet/tests/services/monitor.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/neuron-wallet/tests/services/monitor.test.ts b/packages/neuron-wallet/tests/services/monitor.test.ts index c4d4a04cbf..2a50598ac2 100644 --- a/packages/neuron-wallet/tests/services/monitor.test.ts +++ b/packages/neuron-wallet/tests/services/monitor.test.ts @@ -128,7 +128,7 @@ describe('base monitor', () => { it('isLiving timeout', async () => { isLivingMock.mockImplementation(() => wait(200)) await monitor.startMonitor(200) - await wait(400) + await wait(300) await monitor.stopMonitor() await wait(400) expect(isLivingMock).toHaveBeenCalled()