Skip to content

Commit 7df7e41

Browse files
authored
feat: rename invariant to assert and hide in stacktrace (#442)
* chore: rename invariant to assert * hide assert in stackTrace
1 parent a4b1bd2 commit 7df7e41

File tree

5 files changed

+64
-31
lines changed

5 files changed

+64
-31
lines changed

src/bench.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import {
2222
import { BenchEvent } from './event'
2323
import { Task } from './task'
2424
import {
25+
assert,
2526
defaultConvertTaskResultForConsoleTable,
26-
invariant,
2727
performanceNow,
2828
runtime,
2929
runtimeVersion,
@@ -274,7 +274,7 @@ export class Bench extends EventTarget implements BenchLike {
274274
* @returns the tasks array
275275
*/
276276
runSync (): Task[] {
277-
invariant(
277+
assert(
278278
this.concurrency === null,
279279
'Cannot use `concurrency` option when using `runSync`'
280280
)

src/task.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import type {
1414

1515
import { BenchEvent } from './event'
1616
import {
17+
assert,
1718
getStatisticsSorted,
18-
invariant,
1919
isFnAsyncResource,
2020
isPromiseLike,
2121
isValidSamples,
@@ -156,7 +156,7 @@ export class Task extends EventTarget {
156156

157157
for (const hookName of hookNames) {
158158
if (this.#fnOpts[hookName] != null) {
159-
invariant(
159+
assert(
160160
typeof this.#fnOpts[hookName] === 'function',
161161
`'${hookName}' must be a function if provided`
162162
)
@@ -229,15 +229,15 @@ export class Task extends EventTarget {
229229
return this
230230
}
231231

232-
invariant(
232+
assert(
233233
this.#bench.concurrency === null,
234234
'Cannot use `concurrency` option when using `runSync`'
235235
)
236236
this.#result = startedTaskResult
237237
this.dispatchEvent(new BenchEvent('start', this))
238238

239239
const setupResult = this.#bench.setup(this, 'run')
240-
invariant(
240+
assert(
241241
!isPromiseLike(setupResult),
242242
'`setup` function must be sync when using `runSync()`'
243243
)
@@ -249,7 +249,7 @@ export class Task extends EventTarget {
249249
)
250250

251251
const teardownResult = this.#bench.teardown(this, 'run')
252-
invariant(
252+
assert(
253253
!isPromiseLike(teardownResult),
254254
'`teardown` function must be sync when using `runSync()`'
255255
)
@@ -289,7 +289,7 @@ export class Task extends EventTarget {
289289
this.dispatchEvent(new BenchEvent('warmup', this))
290290

291291
const setupResult = this.#bench.setup(this, 'warmup')
292-
invariant(
292+
assert(
293293
!isPromiseLike(setupResult),
294294
'`setup` function must be sync when using `runSync()`'
295295
)
@@ -301,7 +301,7 @@ export class Task extends EventTarget {
301301
)
302302

303303
const teardownResult = this.#bench.teardown(this, 'warmup')
304-
invariant(
304+
assert(
305305
!isPromiseLike(teardownResult),
306306
'`teardown` function must be sync when using `runSync()`'
307307
)
@@ -404,7 +404,7 @@ export class Task extends EventTarget {
404404
if (this.#fnOpts.beforeAll) {
405405
try {
406406
const beforeAllResult = this.#fnOpts.beforeAll.call(this, mode)
407-
invariant(
407+
assert(
408408
!isPromiseLike(beforeAllResult),
409409
'`beforeAll` function must be sync when using `runSync()`'
410410
)
@@ -423,7 +423,7 @@ export class Task extends EventTarget {
423423
try {
424424
if (this.#fnOpts.beforeEach) {
425425
const beforeEachResult = this.#fnOpts.beforeEach.call(this, mode)
426-
invariant(
426+
assert(
427427
!isPromiseLike(beforeEachResult),
428428
'`beforeEach` function must be sync when using `runSync()`'
429429
)
@@ -436,7 +436,7 @@ export class Task extends EventTarget {
436436
} finally {
437437
if (this.#fnOpts.afterEach) {
438438
const afterEachResult = this.#fnOpts.afterEach.call(this, mode)
439-
invariant(
439+
assert(
440440
!isPromiseLike(afterEachResult),
441441
'`afterEach` function must be sync when using `runSync()`'
442442
)
@@ -459,7 +459,7 @@ export class Task extends EventTarget {
459459
if (this.#fnOpts.afterAll) {
460460
try {
461461
const afterAllResult = this.#fnOpts.afterAll.call(this, mode)
462-
invariant(
462+
assert(
463463
!isPromiseLike(afterAllResult),
464464
'`afterAll` function must be sync when using `runSync()`'
465465
)
@@ -500,7 +500,7 @@ export class Task extends EventTarget {
500500
const fnResult = this.#fn.call(this)
501501
let taskTime = this.#bench.now() - taskStart
502502

503-
invariant(
503+
assert(
504504
!isPromiseLike(fnResult),
505505
'task function must be sync when using `runSync()`'
506506
)

src/utils.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,18 @@ export function getStatisticsSorted (samples: SortedSamples, retainSamples = fal
462462
* @param message - the error message to throw if the condition is false
463463
* @throws {Error} if the condition is false
464464
*/
465-
export const invariant = (condition: boolean, message: string): void => {
465+
export const assert = (condition: boolean, message: string): void => {
466466
if (!condition) {
467-
throw new Error(message)
467+
const stackTraceLimit = Error.stackTraceLimit
468+
try {
469+
Error.stackTraceLimit = 0
470+
const error = new Error(message)
471+
Error.stackTraceLimit = stackTraceLimit
472+
stackTraceLimit !== 0 && Error.captureStackTrace(error, assert)
473+
throw error
474+
} finally {
475+
Error.stackTraceLimit = stackTraceLimit
476+
}
468477
}
469478
}
470479

test/utils-assert.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { expect, test } from 'vitest'
2+
3+
import { assert } from '../src/utils'
4+
5+
test('assert - true', () => {
6+
expect(() => {
7+
assert(true, 'everything fine')
8+
}).not.toThrow()
9+
})
10+
11+
test('assert - false', () => {
12+
expect(() => {
13+
assert(false, 'something went wrong')
14+
}).toThrowError(new Error('something went wrong'))
15+
})
16+
17+
test('assert - false stack trace', () => {
18+
/**
19+
*
20+
*/
21+
function testFunction () {
22+
assert(false, 'stack trace test')
23+
}
24+
25+
try {
26+
testFunction()
27+
} catch (error) {
28+
expect(error).toBeInstanceOf(Error)
29+
if (error instanceof Error) {
30+
expect(error.stack).toBeDefined()
31+
if (error.stack) {
32+
const stackLines = error.stack.split('\n')
33+
expect(stackLines[0]).toBe('Error: stack trace test')
34+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
35+
expect((stackLines[1]!).trim().startsWith('at testFunction')).toBe(true)
36+
}
37+
}
38+
}
39+
})

test/utils-invariant.test.ts

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

0 commit comments

Comments
 (0)