From 8c460ed95c3117365e9b5e9eca3a3f069dd130da Mon Sep 17 00:00:00 2001 From: Connor McKelvey Date: Wed, 2 Mar 2022 11:44:47 -0700 Subject: [PATCH] make ctx.addCtx protected, add option for monitoring time to support non-node environments --- package.json | 2 +- src/context.ts | 14 +++++++++++--- src/runner.ts | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 7fab027..11db3bd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "start-testing", - "version": "1.0.1", + "version": "1.0.3", "description": "Start Testing is a lightweight Typescript Testing Library with 0 dependencies and a minimal yet flexible API to get you from concept to validation as fast as possible", "keywords": [ "typescript", diff --git a/src/context.ts b/src/context.ts index 72c9f29..f567b74 100644 --- a/src/context.ts +++ b/src/context.ts @@ -3,8 +3,13 @@ import { Logger } from './logger' export type Test = (t: Context, ...args: any[]) => Promise export type Tests = Record +interface PerformanceMonitor { + now(): number +} + export type ContextOptions = { logger: Logger + performance?: PerformanceMonitor } export class Context { @@ -15,8 +20,11 @@ export class Context { protected subContexts: Context[] = [] protected logger: Logger + private performance: PerformanceMonitor + constructor(protected readonly name: string, protected readonly opts: ContextOptions) { this.logger = opts.logger.new() + this.performance = this.opts.performance || Date } log(...message: any[]) { @@ -33,7 +41,7 @@ export class Context { } async run(name: string, test: Test): Promise { - const start = performance.now() + const start = this.performance.now() const ctx = this.addCtx(`${this.name} / ${name}`) try { ctx.logger.info(`TEST: ${ctx.name}...`) @@ -52,14 +60,14 @@ export class Context { ctx.logger.print(ctx.exception.stack) } } else { - const ms = Math.floor((performance.now() - start) * 1000) / 1000 + const ms = Math.ceil((this.performance.now() - start) * 1000) / 1000 ctx.logger.success(`PASSED! (${ms} ms)`) } ctx.logger.dump() } } - private addCtx(name: string) { + protected addCtx(name: string) { const ctx = this.new(name) this.subContexts.push(ctx) return ctx diff --git a/src/runner.ts b/src/runner.ts index 142f9da..304a75b 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -2,7 +2,7 @@ import { Context, Tests } from './context' import { Logger } from './logger' const defaultOptions = { - logger: new Logger() + logger: new Logger() } export class Runner extends Context {