Skip to content

Commit

Permalink
chore: Updated dependencies and linted code.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShogunPanda committed Jan 26, 2022
1 parent edb4ad9 commit 8ee474b
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 100 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@
},
"dependencies": {
"acquerello": "^1.0.0",
"hdr-histogram-js": "^2.0.1",
"hdr-histogram-js": "^3.0.0",
"table": "^6.7.1"
},
"devDependencies": {
"@cowtech/eslint-config": "^7.14.5",
"@cowtech/esm-package-utils": "^0.9.1",
"@types/node": "^16.6.1",
"@cowtech/eslint-config": "^8.0.1",
"@cowtech/esm-package-utils": "^0.9.3",
"@types/node": "^17.0.2",
"@types/sinon": "^10.0.2",
"@types/tap": "^15.0.5",
"c8": "^7.8.0",
"prettier": "^2.3.2",
"proxyquire": "^2.1.3",
"sinon": "^11.1.2",
"sinon": "^12.0.1",
"tap": "^15.0.9",
"ts-node": "^10.2.0",
"typescript": "^4.3.5"
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

import { isMainThread, Worker, workerData } from 'worker_threads'
import { Callback, Context, defaultOptions, Options, PrintOptions, Result, Results, runnerPath, Tests } from './models'
import { Callback, Context, defaultOptions, Options, PrintOptions, Results, runnerPath, Tests } from './models'
import { printResults } from './print'

type PromiseResolver<T> = (value: T) => void
Expand Down Expand Up @@ -42,7 +42,7 @@ function run(context: Context): void {
}
})

worker.on('error', (error: Error) => {
worker.on('error', error => {
context.results[name] = {
success: false,
error,
Expand All @@ -60,7 +60,7 @@ function run(context: Context): void {
scheduleNextTest(context)
})

worker.on('message', (result: Result) => {
worker.on('message', result => {
context.results[name] = result
context.current++

Expand Down Expand Up @@ -96,7 +96,7 @@ export function cronometro(
let callback = cb as (err: Error | null, results?: Results) => void

if (!callback) {
promise = new Promise<Results>((resolve: PromiseResolver<Results>, reject: PromiseRejecter) => {
promise = new Promise<Results>((resolve, reject) => {
promiseResolve = resolve
promiseReject = reject
})
Expand Down
2 changes: 1 addition & 1 deletion src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface Test {
after?: SetupFunction
}

export type Callback = ((err: Error | null) => void) | ((err: null, results: Results) => any)
export type Callback = (err: Error | null, results: Results) => any

export interface Percentiles {
[key: string]: number
Expand Down
8 changes: 4 additions & 4 deletions src/print.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { clean, colorize } from 'acquerello'
import { table } from 'table'
import { Result, Results } from './models'
import { Results } from './models'

const styles = ['red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'gray']

Expand Down Expand Up @@ -29,8 +29,8 @@ export function printResults(results: Results, colors: boolean, compare: boolean
let standardErrorPadding = 0

const entries: Array<PrintInfo> = Object.entries(results)
.sort((a: [string, Result], b: [string, Result]) => (!a[1].success ? -1 : b[1].mean - a[1].mean))
.map(([name, result]: [string, Result]) => {
.sort((a, b) => (!a[1].success ? -1 : b[1].mean - a[1].mean))
.map(([name, result]) => {
if (!result.success) {
return {
name,
Expand Down Expand Up @@ -72,7 +72,7 @@ export function printResults(results: Results, colors: boolean, compare: boolean

let currentColor = 0

const rows: Array<Array<string>> = entries.map((entry: PrintInfo) => {
const rows: Array<Array<string>> = entries.map(entry => {
if (entry.error) {
const row = [
styler(`{{gray}}${entry.name}{{-}}`),
Expand Down
12 changes: 4 additions & 8 deletions src/runner.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { isMainThread, parentPort, workerData } from 'worker_threads'
import { runWorker } from './worker'

interface TsNodeModule {
register: (options: object) => void
}

if (isMainThread) {
throw new Error('Do not run this file as main script.')
}
Expand All @@ -17,7 +13,7 @@ if (workerData.path.endsWith('.ts')) {
const instance = Symbol.for('ts-node.register.instance')

if (!(instance in process)) {
chain = import('ts-node').then(({ register }: TsNodeModule) => {
chain = import('ts-node').then(({ register }) => {
register({ project: process.env.TS_NODE_PROJECT })
})
}
Expand All @@ -28,7 +24,7 @@ chain
.then(() => {
return import(workerData.path)
})
.then((module: any) => {
.then(module => {
if (typeof module === 'function') {
return module()
} else if (typeof module.default === 'function') {
Expand All @@ -37,9 +33,9 @@ chain
})
.then(() => {
// Run the worker
runWorker(workerData, (value: any) => parentPort!.postMessage(value), process.exit)
runWorker(workerData, value => parentPort!.postMessage(value), process.exit)
})
.catch((e: Error) => {
.catch(e => {
process.nextTick(() => {
throw e
})
Expand Down
2 changes: 1 addition & 1 deletion src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function handleTestIteration(context: TestContext, error?: Error | null): void {
max: histogram.maxValue,
mean: histogram.mean,
stddev: stdDev,
percentiles: percentiles.reduce((accu: Percentiles, percentile: number) => {
percentiles: percentiles.reduce((accu: Percentiles, percentile) => {
accu[percentile] = histogram.getValueAtPercentile(percentile)
return accu
}, {}),
Expand Down
6 changes: 2 additions & 4 deletions test/asyncImport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import t from 'tap'
import { isMainThread } from 'worker_threads'
import { cronometro, percentiles } from '../src'

type Test = typeof t

async function main(): Promise<void> {
await new Promise((resolve: (value: unknown) => void) => setTimeout(resolve, 100))
await new Promise(resolve => setTimeout(resolve, 100))

if (!isMainThread) {
cronometro(
Expand All @@ -23,7 +21,7 @@ async function main(): Promise<void> {
() => false
)
} else {
t.only('Collecting results', async (t: Test) => {
t.only('Collecting results', async t => {
const results = await cronometro(
{
single() {
Expand Down
8 changes: 3 additions & 5 deletions test/errorHandling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import t from 'tap'
import { isMainThread } from 'worker_threads'
import { cronometro, percentiles } from '../src'

type Test = typeof t

if (!isMainThread) {
cronometro(
{
Expand All @@ -19,7 +17,7 @@ if (!isMainThread) {
() => false
)
} else {
t.test('Errored tests handling', async (t: Test) => {
t.test('Errored tests handling', async t => {
const results = await cronometro(
{
single() {
Expand Down Expand Up @@ -59,11 +57,11 @@ if (!isMainThread) {
t.strictSame(results.multiple.percentiles, {})
})

t.test('Runner cannot be run in main thread', async (t: Test) => {
t.test('Runner cannot be run in main thread', async t => {
await t.rejects(import('../src/runner'), { message: 'Do not run this file as main script.' })
})

t.only('Runner reports setup errors', async (t: Test) => {
t.only('Runner reports setup errors', async t => {
const results = await cronometro(
{
notDefined() {
Expand Down
5 changes: 2 additions & 3 deletions test/fixture/sample.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import cronometro from '../../dist'
import { Results } from '../../src'

const pattern = /[123]/g
const replacements: { [key: string]: string } = { 1: 'a', 2: 'b', 3: 'c' }
Expand All @@ -11,14 +10,14 @@ const subject =
cronometro(
{
single() {
subject.replace(pattern, (m: string) => replacements[m])
subject.replace(pattern, m => replacements[m])
},
multiple() {
subject.replace(/1/g, 'a').replace(/2/g, 'b').replace(/3/g, 'c')
}
},
{ iterations: 5, errorThreshold: 0, print: { compare: true }, warmup: true },
(_err: Error | null, results: Results) => {
(_, results) => {
console.log(JSON.stringify(results, null, 2))
}
)
4 changes: 1 addition & 3 deletions test/genericErrorHandling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import t from 'tap'
import { isMainThread } from 'worker_threads'
import { cronometro } from '../src'

type Test = typeof t

if (!isMainThread) {
cronometro(undefined as any, () => false)
} else {
t.test('Generic error handling', async (t: Test) => {
t.test('Generic error handling', async t => {
const results = await cronometro(
{
single() {
Expand Down
6 changes: 2 additions & 4 deletions test/optionsValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import t from 'tap'
import { cronometro } from '../src'

type Test = typeof t

t.test('Options validation', async (t: Test) => {
t.test('Options validation', async t => {
await t.rejects(
() =>
cronometro(
Expand Down Expand Up @@ -34,7 +32,7 @@ t.test('Options validation', async (t: Test) => {
}
},
{ errorThreshold: -1 },
(err: Error | null) => {
err => {
t.type(err, 'Error')
t.equal(err!.message, 'The errorThreshold option must be a number between 0 and 100.')
}
Expand Down
20 changes: 9 additions & 11 deletions test/print.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import sinon from 'sinon'
import t from 'tap'
import { isMainThread } from 'worker_threads'
import { cronometro, defaultOptions, percentiles, Results } from '../src'
import { cronometro, defaultOptions, percentiles } from '../src'
import { setLogger } from '../src/print'

type Test = typeof t

function removeStyle(source: string): string {
// eslint-disable-next-line no-control-regex
return source.replace(/\x1b\[\d+m/g, '')
Expand Down Expand Up @@ -35,7 +33,7 @@ if (!isMainThread) {
} else {
t.afterEach(() => loggerSpy.resetHistory())

t.test('Printing - Default options', (t: Test) => {
t.test('Printing - Default options', t => {
cronometro(
{
single() {
Expand All @@ -48,7 +46,7 @@ if (!isMainThread) {
throw new Error('FAILED')
}
},
(err: Error | null, results: Results) => {
(err, results) => {
t.equal(err, null)
t.strictSame(Object.keys(results), ['single', 'multiple', 'error'])

Expand Down Expand Up @@ -89,7 +87,7 @@ if (!isMainThread) {
)
})

t.test('Printing - No colors', (t: Test) => {
t.test('Printing - No colors', t => {
cronometro(
{
single() {
Expand All @@ -103,7 +101,7 @@ if (!isMainThread) {
}
},
{ print: { colors: false } },
(err: Error | null) => {
err => {
t.equal(err, null)

const output = loggerSpy.getCall(0).args[0]
Expand All @@ -116,7 +114,7 @@ if (!isMainThread) {
)
})

t.test('Printing - Base compare', (t: Test) => {
t.test('Printing - Base compare', t => {
cronometro(
{
single() {
Expand All @@ -130,7 +128,7 @@ if (!isMainThread) {
}
},
{ print: { compare: true } },
(err: Error | null) => {
err => {
t.equal(err, null)

const output = removeStyle(loggerSpy.getCall(0).args[0])
Expand All @@ -147,7 +145,7 @@ if (!isMainThread) {
)
})

t.test('Printing - Previous compare', (t: Test) => {
t.test('Printing - Previous compare', t => {
cronometro(
{
single() {
Expand All @@ -161,7 +159,7 @@ if (!isMainThread) {
}
},
{ print: { compare: true, compareMode: 'previous' } },
(err: Error | null) => {
err => {
t.equal(err, null)

const output = removeStyle(loggerSpy.getCall(0).args[0])
Expand Down
4 changes: 1 addition & 3 deletions test/success.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import t from 'tap'
import { isMainThread } from 'worker_threads'
import { cronometro, percentiles } from '../src'

type Test = typeof t

if (!isMainThread) {
cronometro(
{
Expand All @@ -20,7 +18,7 @@ if (!isMainThread) {
() => false
)
} else {
t.only('Collecting results', async (t: Test) => {
t.only('Collecting results', async t => {
const results = await cronometro(
{
single() {
Expand Down
4 changes: 1 addition & 3 deletions test/unhandledErrorHandling.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import t from 'tap'
import { isMainThread } from 'worker_threads'
import { Callback, cronometro, percentiles } from '../src'

type Test = typeof t

if (!isMainThread) {
cronometro(
{
Expand All @@ -23,7 +21,7 @@ if (!isMainThread) {
() => false
)
} else {
t.test('Unhandled errored tests handling', async (t: Test) => {
t.test('Unhandled errored tests handling', async t => {
const results = await cronometro(
{
single() {
Expand Down
Loading

0 comments on commit 8ee474b

Please sign in to comment.