Skip to content

Commit

Permalink
make ctx.addCtx protected, add option for monitoring time to support …
Browse files Browse the repository at this point in the history
…non-node environments
  • Loading branch information
connormckelvey committed Mar 2, 2022
1 parent 840d65c commit 8c460ed
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
14 changes: 11 additions & 3 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import { Logger } from './logger'
export type Test = (t: Context, ...args: any[]) => Promise<void>
export type Tests = Record<string, Test>

interface PerformanceMonitor {
now(): number
}

export type ContextOptions = {
logger: Logger
performance?: PerformanceMonitor
}

export class Context {
Expand All @@ -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[]) {
Expand All @@ -33,7 +41,7 @@ export class Context {
}

async run(name: string, test: Test): Promise<void> {
const start = performance.now()
const start = this.performance.now()
const ctx = this.addCtx(`${this.name} / ${name}`)
try {
ctx.logger.info(`TEST: ${ctx.name}...`)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8c460ed

Please sign in to comment.