8000 feat!: Replaced ts-node with @swc-node/register. · ShogunPanda/cronometro@053a10c · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 053a10c

Browse files
committed
feat!: Replaced ts-node with @swc-node/register.
1 parent 11dd020 commit 053a10c

19 files changed

+632
-566
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ If you need to configure the script at runtime, use environment variables and op
2929

3030
### TypeScript
3131

32-
cronometro uses [ts-node](https://www.npmjs.com/package/ts-node) to compile TypeScript files on the fly.
32+
cronometro can run on TypeScript files. Type deletion happens on the fly with no additional configuration required.
3333

34-
ts-node and TypeScript are not installed automatically by cronometro (as they are listed in `peerDependencies`) so you need to do it manually.
35-
36-
To pass the `tsconfig.json` project file to use, use the `TS_NODE_PROJECT` environment variable.
34+
Use the `TS_NODE_PROJECT` environment variable to provide a TypeScript configuration.
3735

3836
### API use
3937

@@ -102,10 +100,10 @@ Each property value is a object with the following properties:
102100
import cronometro from 'cronometro'
103101

104102
const results = cronometro({
105-
test1: function () {
103+
await test1: function () {
106104
// Do something
107105
},
108-
test2: function () {
106+
await test2: function () {
109107
// Do something else
110108
}
111109
})

package.json

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@
3333
"format": "prettier -w src test",
3434
"lint": "eslint --cache --ext .js,.jsx,.ts,.tsx src test",
3535
"typecheck": "tsc -p . --emitDeclarationOnly",
36-
"test": "c8 -c test/config/c8-local.json tap test/*.test.ts",
37-
"test:ci": "c8 -c test/config/c8-ci.json tap --no-color test/*.test.ts",
36+
"test": "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": "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",
3838
"ci": "npm run build && npm run test:ci",
3939
"prepublishOnly": "npm run ci",
4040
"postpublish": "git push origin && git push origin -f --tags"
4141
},
4242
"dependencies": {
43+
"@swc-node/register": "^1.8.0",
4344
"acquerello": "^2.0.6",
4445
"hdr-histogram-js": "^3.0.0",
4546
"table": "^6.8.1"
@@ -55,14 +56,9 @@
5556
"concurrently": "^8.2.2",
5657
"prettier": "^3.2.4",
5758
"proxyquire": "^2.1.3",
58-
"tap": "^18.7.0",
59-
"ts-node": "^10.9.2",
6059
"typescript": "^5.3.3"
6160
},
6261
"engines": {
6362
"node": ">= 18.18.0"
64-
},
65-
"tap": {
66-
"extends": "./test/config/tap.yml"
6763
}
6864
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { isMainThread, Worker, workerData } from 'node:worker_threads'
22
import {
33
defaultOptions,
4-
type Result,
54
runnerPath,
65
type Callback,
76
type Context,
87
type Options,
98
type PrintOptions,
9+
type Result,
1010
type Results,
1111
type Tests
1212
} from './models.js'

src/models.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ export interface PrintOptions {
88
compareMode?: 'base' | 'previous'
99
}
1010

11+
export type SetupFunctionCallback = (err?: Error | null) => void
12+
1113
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
12-
export type SetupFunction = (cb: (err?: Error | null) => void) => Promise<any> | void
14+
export type SetupFunction = (cb: SetupFunctionCallback) => Promise<any> | void
1315

1416
export interface Options {
1517
iterations: number
@@ -33,8 +35,6 @@ export interface Test {
3335
after?: SetupFunction
3436
}
3537

36-
export type Callback = (err: Error | null, results: Results) => any
37-
3838
export type Percentiles = Record<string, number>
3939

4040
export interface Result {
@@ -49,6 +49,8 @@ export interface Result {
4949
percentiles: Percentiles
5050
}
5151

52+
export type Callback = (err: Error | null, results: Results) => any
53+
5254
export type Tests = Record<string, TestFunction | Test>
5355

5456
export type Results = Record<string, Result>
@@ -98,4 +100,10 @@ export const defaultOptions = {
98100

99101
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]
100102

101-
export const runnerPath = resolve(import.meta.url.replace('file://', '').replace(/models.(js|ts)/, ''), './runner.js')
103+
export const runnerPath = resolve(
104+
import.meta.url
105+
.replace('file://', '')
106+
.replace('/src/', '/dist/')
107+
.replace(/models.(js|ts)/, ''),
108+
'./runner.js'
109+
)

src/runner.ts

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* c8 ignore start */
12
import { isMainThread, parentPort, workerData } from 'node:worker_threads'
23
import { type WorkerContext } from './models.js'
34
import { runWorker } from './worker.js'
@@ -6,45 +7,31 @@ if (isMainThread) {
67
throw new Error('Do not run this file as main script.')
78
}
89

9-
// Register ts-node for TypeScript inclusion
10-
let chain: Promise<void> = Promise.resolve()
11-
12-
/* c8 ignore start */
1310
if (workerData.path.endsWith('.ts')) {
14-
const instance = Symbol.for('ts-node.register.instance')
15-
16-
if (!(instance in process)) {
17-
chain = import('ts-node').then(({ register }) => {
18-
register({ project: process.env.TS_NODE_PROJECT })
19-
})
20-
}
11+
await import('@swc-node/register/esm-register')
2112
}
2213

2314
// Require the script to set tests
24-
chain
25-
.then(() => {
26-
return import(workerData.path)
27-
})
28-
.then(module => {
29-
if (typeof module === 'function') {
30-
return module()
31-
} else if (typeof module.default === 'function') {
32-
return module.default()
33-
}
34-
})
35-
.then(() => {
36-
// Run the worker
37-
runWorker(
38-
workerData as WorkerContext,
39-
value => {
40-
parentPort!.postMessage({ type: 'cronometro.result', payload: value })
41-
},
42-
(code: number) => process.exit(code)
43-
)
44-
})
45-
.catch(error => {
46-
process.nextTick(() => {
47-
throw error
48-
})
15+
try {
16+
const module = await import(workerData.path)
17+
18+
if (typeof module === 'function') {
19+
await module()
20+
} else if (typeof module.default === 'function') {
21+
await module.default()
22+
}
23+
24+
// Run the worker
25+
runWorker(
26+
workerData as WorkerContext,
27+
value => {
28+
parentPort!.postMessage({ type: 'cronometro.result', payload: value })
29+
},
30+
(code: number) => process.exit(code)
31+
)
32+
} catch (error) {
33+
process.nextTick(() => {
34+
throw error
4935
})
36+
}
5037
/* c8 ignore stop */

src/worker.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,13 @@ function runTestIteration(context: TestContext): void {
101101
context.handler(null)
102102
}
103103
} catch (error) {
104+
/* c8 ignore start */
104105
// If a error was thrown, only handle if the original function length is 0, which means it's a sync error, otherwise propagate
105106
if (context.test.length === 0) {
106107
context.handler(error as Error)
107108
return
108109
}
110+
/* c8 ignore end */
109111

110112
throw error
111113
}

test/asyncImport.test.ts

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1-
/* eslint-disable @typescript-eslint/no-floating-promises */
2-
1+
import { deepStrictEqual, ifError, ok } from 'node:assert'
2+
import { test } from 'node:test'
33
import { isMainThread } from 'node:worker_threads'
4-
import t from 'tap'
54
import { cronometro, percentiles } from '../src/index.js'
65

7-
async function main(): Promise<void> {
8-
await new Promise(resolve => setTimeout(resolve, 100))
6+
await new Promise(resolve => setTimeout(resolve, 100))
97

10-
if (!isMainThread) {
11-
cronometro(
8+
if (!isMainThread) {
9+
cronometro(
10+
{
11+
single() {
12+
Buffer.alloc(10)
13+
},
14+
multiple() {
15+
Buffer.alloc(10)
16+
Buffer.alloc(20)
17+
}
18+
},
19+
() => false
20+
)
21+
} else {
22+
await test('Collecting results', async () => {
23+
const results = await cronometro(
1224
{
1325
single() {
1426
Buffer.alloc(10)
@@ -18,43 +30,24 @@ async function main(): Promise<void> {
1830
Buffer.alloc(20)
1931
}
2032
},
21-
() => false
33+
{ iterations: 10, print: false }
2234
)
23-
} else {
24-
t.test('Collecting results', async t => {
25-
const results = await cronometro(
26-
{
27-
single() {
28-
Buffer.alloc(10)
29-
},
30-
multiple() {
31-
Buffer.alloc(10)
32-
Buffer.alloc(20)
33-
}
34-
},
35-
{ iterations: 10, print: false }
36-
)
3735

38-
t.strictSame(Object.keys(results), ['single', 'multiple'])
36+
deepStrictEqual(Object.keys(results), ['single', 'multiple'])
3937

40-
for (const entry of Object.values(results)) {
41-
t.ok(entry.success)
42-
t.type(entry.error, 'undefined')
43-
t.equal(entry.size, 10)
44-
t.type(entry.min, 'number')
45-
t.type(entry.max, 'number')
46-
t.type(entry.mean, 'number')
47-
t.type(entry.stddev, 'number')
48-
t.type(entry.standardError, 'number')
38+
for (const entry of Object.values(results)) {
39+
ok(entry.success)
40+
ifError(entry.error)
41+
deepStrictEqual(entry.size, 10)
42+
deepStrictEqual(typeof entry.min, 'number')
43+
deepStrictEqual(typeof entry.max, 'number')
44+
deepStrictEqual(typeof entry.mean, 'number')
45+
deepStrictEqual(typeof entry.stddev, 'number')
46+
deepStrictEqual(typeof entry.standardError, 'number')
4947

50-
for (const percentile of percentiles) {
51-
t.type(entry.percentiles[percentile.toString()], 'number')
52-
}
48+
for (const percentile of percentiles) {
49+
ok(typeof entry.percentiles[percentile.toString()], 'number')
5350
}
54-
})
55-
}
51+
}
52+
})
5653
}
57-
58-
main()
59-
60-
export default main

test/callbacks.test.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
/* eslint-disable @typescript-eslint/no-floating-promises */
22

3-
import { isMainThread, Worker } from 'node:worker_threads'
4-
import t from 'tap'
3+
import { deepStrictEqual, match, ok } from 'node:assert'
4+
import { test } from 'node:test'
5+
import { Worker, isMainThread, parentPort } from 'node:worker_threads'
56
import { cronometro, type Result, type TestFunction } from '../src/index.js'
67

78
if (!isMainThread) {
9+
parentPort!.postMessage('another')
10+
811
cronometro(
912
{
1013
single() {
@@ -18,7 +21,7 @@ if (!isMainThread) {
1821
() => false
1922
)
2023
} else {
21-
t.test('Callbacks', async t => {
24+
test('Callbacks', async () => {
2225
await cronometro(
2326
{
2427
single() {
@@ -35,24 +38,24 @@ if (!isMainThread) {
3538
iterations: 10,
3639
print: false,
3740
onTestStart(name: string, data: any, worker: Worker) {
38-
t.match(name, /single|multiple|missing/)
39-
t.ok(data.index < 3)
40-
t.ok(worker instanceof Worker)
41+
match(name, /single|multiple|missing/)
42+
ok(data.index < 3)
43+
ok(worker instanceof Worker)
4144
},
4245
onTestEnd(name: string, result: Result, worker: Worker) {
4346
if (result.success) {
44-
t.same(name, 'single')
45-
t.ok(result.size > 0)
47+
deepStrictEqual(name, 'single')
48+
ok(result.size > 0)
4649
} else {
47-
t.same(name, 'multiple')
48-
t.same(result.error!.message, 'INVALID')
50+
deepStrictEqual(name, 'multiple')
51+
deepStrictEqual(result.error!.message, 'INVALID')
4952
}
50-
t.ok(worker instanceof Worker)
53+
ok(worker instanceof Worker)
5154
},
5255
onTestError(name: string, error: Error, worker: Worker) {
53-
t.same(name, 'missing')
54-
t.same(error.message, "Cannot read properties of undefined (reading 'test')")
55-
t.ok(worker instanceof Worker)
56+
deepStrictEqual(name, 'missing')
57+
deepStrictEqual(error.message, "Cannot read properties of undefined (reading 'test')")
58+
ok(worker instanceof Worker)
5659
}
5760
}
5861
)

test/config/c8-ci.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"check-coverage": true,
33
"reporter": ["text", "json"],
4+
"exclude": ["dist", "test"],
45
8718 "branches": 90,
56
"functions": 90,
67
"lines": 90,

test/config/c8-local.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2+
"exclude": ["dist", "test"],
23
"reporter": ["text", "html"]
34
}

test/config/tap.yml

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0