Skip to content

Commit 0d5f975

Browse files
committed
feat: Added skip option.
1 parent a30531a commit 0d5f975

24 files changed

+51
-58
lines changed

.swcrc

-10
This file was deleted.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ A test can be defined as a function containing the test to run or an object cont
5858
- `test`: The function containing the test to run. If omitted, the test will be a no-op.
5959
- `before`: A setup function to execute before starting test iteration.
6060
- `after`: A cleanup function to execute after all test iteration have been run.
61+
- `skip`: If a test should be skipped.
6162

6263
Each of the `test` functions above can be either a function, a function accepting a Node style callback or a function returning a promise (hence also async functions).
6364

package.json

+7-11
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@
2727
"exports": "./dist/index.js",
2828
"types": "./dist/index.d.ts",
2929
"scripts": {
30-
"dev": "swc --strip-leading-paths --delete-dir-on-start -s -w -d dist src",
31-
"build": "swc --strip-leading-paths --delete-dir-on-start -d dist src",
32-
"postbuild": "concurrently npm:lint npm:typecheck",
30+
"build": "tsc -p .",
31+
"postbuild": "npm run lint",
3332
"format": "prettier -w src test",
3433
"lint": "eslint --cache",
35-
"typecheck": "tsc -p . --emitDeclarationOnly",
36-
"test": "cross-env TS_NODE_PROJECT=tsconfig.test.json c8 -c test/config/c8-local.json node --import @swc-node/register/esm-register --test test/*.test.ts",
37-
"test:ci": "cross-env TS_NODE_PROJECT=tsconfig.test.json c8 -c test/config/c8-ci.json node --import @swc-node/register/esm-register --test-reporter=tap --test test/*.test.ts",
34+
"typecheck": "tsc -p . --noEmit",
35+
"test": "c8 -c test/config/c8-local.json node --env-file=test/config/env --test test/*.test.ts",
36+
"test:ci": "c8 -c test/config/c8-ci.json node --env-file=test/config/env --test test/*.test.ts",
3837
"ci": "npm run build && npm run test:ci",
3938
"prepublishOnly": "npm run ci",
4039
"postpublish": "git push origin && git push origin -f --tags"
@@ -46,13 +45,10 @@
4645
"table": "^6.9.0"
4746
},
4847
"devDependencies": {
49-
"@cowtech/eslint-config": "10.2.0",
50-
"@swc/cli": "^0.6.0",
51-
"@swc/core": "^1.10.7",
48+
"@cowtech/eslint-config": "10.4.0",
5249
"@types/node": "^22.10.2",
5350
"c8": "^10.1.3",
54-
"chokidar": "^4.0.3",
55-
"concurrently": "^9.1.1",
51+
"cleaner-spec-reporter": "^0.3.1",
5652
"cross-env": "^7.0.3",
5753
"eslint": "^9.17.0",
5854
"prettier": "^3.4.2",

src/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import {
99
type Result,
1010
type Results,
1111
type Tests
12-
} from './models.js'
13-
import { printResults } from './print.js'
12+
} from './models.ts'
13+
import { printResults } from './print.ts'
1414

1515
type PromiseResolver<T> = (value: T) => void
1616
type PromiseRejecter = (err: Error) => void
1717

18-
export * from './models.js'
18+
export * from './models.ts'
1919

2020
function scheduleNextTest(context: Context): void {
2121
// We still have work to do
@@ -106,7 +106,7 @@ export function cronometro(
106106
cb?: Callback
107107
): Promise<Results> | void {
108108
if (!isMainThread) {
109-
workerData.tests = Object.entries(tests)
109+
workerData.tests = Object.entries(tests).filter(test => test[1]?.skip !== true)
110110
return
111111
}
112112

@@ -174,7 +174,7 @@ export function cronometro(
174174
iterations,
175175
errorThreshold: errorThreshold / 100,
176176
print,
177-
tests: Object.entries(tests), // Convert tests to a easier to process [name, func] list,
177+
tests: Object.entries(tests).filter(test => test[1]?.skip !== true), // Convert tests to a easier to process [name, func] list,
178178
results: {},
179179
current: 0,
180180
callback,

src/models.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { type Histogram } from 'hdr-histogram-js'
2-
import { resolve, dirname } from 'node:path'
32
import { fileURLToPath } from 'node:url'
43
import { type Worker } from 'node:worker_threads'
54

@@ -27,12 +26,13 @@ export interface Options {
2726
export type StaticTest = () => any
2827
export type AsyncTest = (cb: Callback) => any
2928
export type PromiseTest = () => Promise<any>
30-
export type TestFunction = StaticTest | AsyncTest | PromiseTest
29+
export type TestFunction = (StaticTest | AsyncTest | PromiseTest) & { skip?: boolean }
3130

3231
export interface Test {
3332
test?: TestFunction
3433
before?: SetupFunction
3534
after?: SetupFunction
35+
skip?: boolean
3636
}
3737

3838
export type Percentiles = Record<string, number>
@@ -100,9 +100,4 @@ export const defaultOptions = {
100100

101101
export const percentiles = [0.001, 0.01, 0.1, 1, 2.5, 10, 25, 50, 75, 90, 97.5, 99, 99.9, 99.99, 99.999]
102102

103-
export const runnerPath = resolve(
104-
dirname(fileURLToPath(import.meta.url))
105-
.replace('src', 'dist')
106-
.replace(/models.(js|ts)/, ''),
107-
'./runner.js'
108-
)
103+
export const runnerPath = fileURLToPath(new URL(`./runner.${import.meta.url.slice(-2)}`, import.meta.url))

src/print.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { clean, colorize } from 'acquerello'
22
import { table } from 'table'
3-
import { type Results } from './models.js'
3+
import { type Results } from './models.ts'
44

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

src/runner.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* c8 ignore start */
22
import { isMainThread, parentPort, workerData } from 'node:worker_threads'
3-
import { type WorkerContext } from './models.js'
4-
import { runWorker } from './worker.js'
3+
import { type WorkerContext } from './models.ts'
4+
import { runWorker } from './worker.ts'
55

66
if (isMainThread) {
77
throw new Error('Do not run this file as main script.')

src/worker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
type TestContext,
77
type TestFunction,
88
type WorkerContext
9-
} from './models.js'
9+
} from './models.ts'
1010

1111
function noOp(): void {
1212
// No-op

test.env

-1
This file was deleted.

test/asyncImport.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { deepStrictEqual, ifError, ok } from 'node:assert'
22
import { test } from 'node:test'
33
import { isMainThread } from 'node:worker_threads'
4-
import { cronometro, percentiles } from '../src/index.js'
4+
import { cronometro, percentiles } from '../src/index.ts'
55

66
await new Promise(resolve => setTimeout(resolve, 100))
77

test/callbacks.test.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { deepStrictEqual, match, ok } from 'node:assert'
22
import { test } from 'node:test'
33
import { Worker, isMainThread, parentPort } from 'node:worker_threads'
4-
import { cronometro, type Result, type TestFunction } from '../src/index.js'
4+
import { cronometro, type Result, type TestFunction } from '../src/index.ts'
55

66
if (!isMainThread) {
77
parentPort!.postMessage('another')
@@ -14,7 +14,11 @@ if (!isMainThread) {
1414
multiple() {
1515
throw new Error('INVALID')
1616
},
17-
missing: undefined as unknown as TestFunction
17+
missing: undefined as unknown as TestFunction,
18+
skipped: {
19+
test() {},
20+
skip: true
21+
}
1822
},
1923
() => false
2024
)
@@ -30,6 +34,10 @@ if (!isMainThread) {
3034
},
3135
missing() {
3236
Buffer.alloc(10)
37+
},
38+
skipped: {
39+
test() {},
40+
skip: true
3341
}
3442
},
3543
{

test/config/c8-ci.json

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"check-coverage": true,
33
"reporter": ["text", "json"],
4-
"exclude": ["dist", "test"],
54
"branches": 90,
65
"functions": 90,
76
"lines": 90,

test/config/c8-local.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
2-
"exclude": ["dist", "test"],
3-
"reporter": ["text", "html"]
2+
"reporter": ["text", "html"],
3+
"all": true,
4+
"include": ["src/**/*.ts"],
5+
"exclude": ["**/node_modules/**", "test/**/*.ts"]
46
}

test/config/env

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NODE_OPTIONS="--experimental-strip-types --test-reporter=cleaner-spec-reporter --disable-warning=ExperimentalWarning"

test/errorHandling.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { deepStrictEqual, ifError, ok, rejects } from 'node:assert'
22
import { test } from 'node:test'
33
import { isMainThread } from 'node:worker_threads'
4-
import { cronometro, percentiles } from '../src/index.js'
4+
import { cronometro, percentiles } from '../src/index.ts'
55

66
if (!isMainThread) {
77
cronometro(
@@ -57,7 +57,7 @@ if (!isMainThread) {
5757
})
5858

5959
test('Runner cannot be run in main thread', async () => {
60-
await rejects(import('../src/runner.js'), { message: 'Do not run this file as main script.' })
60+
await rejects(import('../src/runner.ts'), { message: 'Do not run this file as main script.' })
6161
})
6262

6363
test('Runner reports setup errors', async () => {

test/genericErrorHandling.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { deepStrictEqual, ok } from 'node:assert'
22
import { test } from 'node:test'
33
import { isMainThread } from 'node:worker_threads'
4-
import { cronometro, type Tests } from '../src/index.js'
4+
import { cronometro, type Tests } from '../src/index.ts'
55

66
if (!isMainThread) {
77
cronometro(undefined as unknown as Tests, () => false)

test/notTestExportedInWorkers.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { deepStrictEqual, ok } from 'node:assert'
22
import { test } from 'node:test'
33
import { isMainThread } from 'node:worker_threads'
4-
import { cronometro } from '../src/index.js'
4+
import { cronometro } from '../src/index.ts'
55

66
if (isMainThread) {
77
test('Errors are properly handled when no tests are exported in the worker threads', async () => {

test/optionsValidation.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { deepStrictEqual, ok, rejects } from 'node:assert'
22
import { test } from 'node:test'
3-
import { cronometro } from '../src/index.js'
3+
import { cronometro } from '../src/index.ts'
44

55
test('Options validation', async () => {
66
await rejects(

test/print.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { deepStrictEqual, ifError, match, ok } from 'node:assert'
22
import { test } from 'node:test'
33
import { isMainThread } from 'node:worker_threads'
4-
import { cronometro, defaultOptions, percentiles } from '../src/index.js'
5-
import { setLogger } from '../src/print.js'
4+
import { cronometro, defaultOptions, percentiles } from '../src/index.ts'
5+
import { setLogger } from '../src/print.ts'
66

77
function removeStyle(source: string): string {
88
// eslint-disable-next-line no-control-regex

test/success.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { deepStrictEqual, ifError, ok } from 'node:assert'
22
import { test } from 'node:test'
33
import { isMainThread } from 'node:worker_threads'
4-
import { cronometro, percentiles } from '../src/index.js'
4+
import { cronometro, percentiles } from '../src/index.ts'
55

66
if (!isMainThread) {
77
cronometro(

test/testsCallbacks.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { deepStrictEqual, ifError, ok } from 'node:assert'
22
import { test } from 'node:test'
33
import { isMainThread } from 'node:worker_threads'
4-
import { cronometro, type SetupFunctionCallback } from '../src/index.js'
4+
import { cronometro, type SetupFunctionCallback } from '../src/index.ts'
55

66
if (!isMainThread) {
77
cronometro(

test/unhandledErrorHandling.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { deepStrictEqual, ifError, ok } from 'node:assert'
22
import { test } from 'node:test'
33
import { isMainThread } from 'node:worker_threads'
4-
import { cronometro, percentiles, type Callback } from '../src/index.js'
4+
import { cronometro, percentiles, type Callback } from '../src/index.ts'
55

66
if (!isMainThread) {
77
cronometro(

test/worker.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { deepStrictEqual, ifError, ok } from 'node:assert'
22
import { test } from 'node:test'
3-
import { percentiles, type AsyncTest, type Result } from '../src/index.js'
4-
import { runWorker } from '../src/worker.js'
3+
import { percentiles, type AsyncTest, type Result } from '../src/index.ts'
4+
import { runWorker } from '../src/worker.ts'
55

66
test('Worker execution - Handle sync functions that succeed', (t, done) => {
77
let mainCalls = 0

tsconfig.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"noUnusedLocals": true,
1616
"noUnusedParameters": true,
1717
"strictNullChecks": true,
18-
"useUnknownInCatchVariables": false
18+
"useUnknownInCatchVariables": false,
19+
"allowImportingTsExtensions": true,
20+
"rewriteRelativeImportExtensions": true
1921
},
2022
"include": ["src/*.ts"]
2123
}

0 commit comments

Comments
 (0)