From b177a348b7b3de09c8e813791df4b78dedbd8b0a Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Wed, 28 Aug 2024 16:13:26 +0200 Subject: [PATCH] Simple tests for timedExecution --- src/lib/index.test.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/lib/index.test.ts b/src/lib/index.test.ts index dbc9154..b6aa671 100644 --- a/src/lib/index.test.ts +++ b/src/lib/index.test.ts @@ -1,4 +1,4 @@ -import safePath from '$lib'; +import { safePath, timedExecution } from '$lib'; import { describe, it, expect } from 'vitest'; describe('safe path', () => { @@ -30,3 +30,21 @@ describe('safe path', () => { expect(safePath('./uplodas', '..foobar..')).toBe(true); }); }); + +describe('timedExecution', () => { + const asyncIdentity = async (v) => v; + const identity = (v) => v; + + it('works with async', async () => { + const { executionTime, result } = await timedExecution(() => asyncIdentity(5)); + expect(result).toBe(5); + // execution time is always positive + expect(executionTime).toBeGreaterThanOrEqual(0); + }); + it('works with sync', async () => { + const { executionTime, result } = await timedExecution(() => identity(5)); + expect(result).toBe(5); + // execution time is always positive + expect(executionTime).toBeGreaterThanOrEqual(0); + }); +});