Skip to content

Commit e2ac0f9

Browse files
committed
feat(internal): prepareStackTrace
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 4926126 commit e2ac0f9

File tree

6 files changed

+85
-1
lines changed

6 files changed

+85
-1
lines changed

.eslintrc.base.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ const config = {
676676
'@typescript-eslint/no-base-to-string': [
677677
2,
678678
{
679-
ignoredTypeNames: ['RegExp']
679+
ignoredTypeNames: ['Error', 'RegExp', 'URL', 'URLSearchParams']
680680
}
681681
],
682682
'@typescript-eslint/no-floating-promises': [

__tests__/setup/chai.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@
55
*/
66

77
import chai from 'chai'
8+
import chaiString from 'chai-string'
89

910
// configure chai
1011
chai.config.includeStack = true
1112
chai.config.truncateThreshold = 0
13+
14+
/**
15+
* initialize chai plugins.
16+
*
17+
* @see https://github.com/onechiporenko/chai-string
18+
*/
19+
chai.use(chaiString)

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"@flex-development/tutils": "6.0.0-alpha.7",
7676
"@graphql-eslint/eslint-plugin": "3.13.0",
7777
"@types/chai": "4.3.4",
78+
"@types/chai-string": "1.4.2",
7879
"@types/conventional-changelog": "3.1.1",
7980
"@types/conventional-changelog-config-spec": "2.1.2",
8081
"@types/conventional-changelog-writer": "4.0.1",
@@ -92,6 +93,7 @@
9293
"@vitest/coverage-c8": "0.26.2",
9394
"@vitest/ui": "0.26.2",
9495
"chai": "4.3.7",
96+
"chai-string": "1.5.0",
9597
"conventional-changelog-cli": "2.2.2",
9698
"conventional-recommended-bump": "6.1.0",
9799
"cspell": "6.18.0",
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @file Unit Tests - prepareStackTrace
3+
* @module create-error-node/internal/tests/unit/prepareStackTrace
4+
*/
5+
6+
import { ErrorCode } from '#src/enums'
7+
import type { NodeError } from '#src/types'
8+
import testSubject from '../prepare-stack-trace'
9+
10+
describe('unit:internal/prepareStackTrace', () => {
11+
it('should return error with new stack trace', () => {
12+
// Arrange
13+
const error: NodeError<TypeError> = Object.assign(new Error(), {
14+
code: ErrorCode.ERR_INSPECTOR_NOT_WORKER,
15+
message: 'Current thread is not a worker'
16+
})
17+
18+
// Act + Expect
19+
expect(testSubject(error).stack).to.startWith(error.toString())
20+
})
21+
})
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @file Internal - prepareStackTrace
3+
* @module create-node-error/internal/prepareStackTrace
4+
*/
5+
6+
import type { NodeError } from '#src/types'
7+
8+
/**
9+
* Adds a stack trace to the given `error`.
10+
*
11+
* Does not preserve previously written traces.
12+
*
13+
* @template T - Error base type
14+
*
15+
* @param {NodeError<T>} error - Error to evaluate
16+
* @return {NodeError<T>} `error` with new stack trace
17+
*/
18+
function prepareStackTrace<T extends Error = Error>(
19+
error: NodeError<T>
20+
): NodeError<T> {
21+
// add stack trace
22+
Error.captureStackTrace(error, prepareStackTrace)
23+
24+
// make sure stack trace is formatted properly
25+
error.stack = error.stack!.replace(
26+
new RegExp(`^${error.name}: ${error.message}`),
27+
error.toString()
28+
)
29+
30+
return error
31+
}
32+
33+
export default prepareStackTrace

yarn.lock

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ __metadata:
10021002
"@flex-development/tutils": "npm:6.0.0-alpha.7"
10031003
"@graphql-eslint/eslint-plugin": "npm:3.13.0"
10041004
"@types/chai": "npm:4.3.4"
1005+
"@types/chai-string": "npm:1.4.2"
10051006
"@types/conventional-changelog": "npm:3.1.1"
10061007
"@types/conventional-changelog-config-spec": "npm:2.1.2"
10071008
"@types/conventional-changelog-writer": "npm:4.0.1"
@@ -1019,6 +1020,7 @@ __metadata:
10191020
"@vitest/coverage-c8": "npm:0.26.2"
10201021
"@vitest/ui": "npm:0.26.2"
10211022
chai: "npm:4.3.7"
1023+
chai-string: "npm:1.5.0"
10221024
conventional-changelog-cli: "npm:2.2.2"
10231025
conventional-recommended-bump: "npm:6.1.0"
10241026
cspell: "npm:6.18.0"
@@ -1738,6 +1740,15 @@ __metadata:
17381740
languageName: node
17391741
linkType: hard
17401742

1743+
"@types/chai-string@npm:1.4.2":
1744+
version: 1.4.2
1745+
resolution: "@types/chai-string@npm:1.4.2"
1746+
dependencies:
1747+
"@types/chai": "npm:*"
1748+
checksum: c272b47a9ee36ec43b337ac4995f4b0d6f942b4042c464a9211efae8cebeaa07212e06ff6196d397e44c7c3996f79ba67a0d3733c2989eb55ee71c0bb2f2ca26
1749+
languageName: node
1750+
linkType: hard
1751+
17411752
"@types/chai-subset@npm:^1.3.3":
17421753
version: 1.3.3
17431754
resolution: "@types/chai-subset@npm:1.3.3"
@@ -2689,6 +2700,15 @@ __metadata:
26892700
languageName: node
26902701
linkType: hard
26912702

2703+
"chai-string@npm:1.5.0":
2704+
version: 1.5.0
2705+
resolution: "chai-string@npm:1.5.0"
2706+
peerDependencies:
2707+
chai: ^4.1.2
2708+
checksum: d39e07eb8311f99130c3fd404ef86bc09b4b4b4da153d61ec80875da48d06ead90fafd13692eaf3197b672679212603cd4e9dd3b9f30313d6f6fa56d40ac7517
2709+
languageName: node
2710+
linkType: hard
2711+
26922712
"chai@npm:4.3.7, chai@npm:^4.3.7":
26932713
version: 4.3.7
26942714
resolution: "chai@npm:4.3.7"

0 commit comments

Comments
 (0)