├── .editorconfig ├── .github ├── FUNDING.yml ├── ISSUE_TEMPLATE │ └── ---bug-report.yml └── workflows │ ├── ci.yml │ └── codeql.yml ├── .gitignore ├── CODEOWNERS ├── CODE_OF_CONDUCT.md ├── LICENCE ├── README.md ├── build.config.ts ├── eslint.config.js ├── package.json ├── playground ├── index.js └── package.json ├── pnpm-lock.yaml ├── pnpm-workspace.yaml ├── renovate.json ├── src └── index.ts ├── test ├── __snapshots__ │ ├── bun.json5 │ ├── deno.json5 │ ├── jiti.json5 │ └── vitest.json5 └── index.test.ts ├── tsconfig.json └── vitest.config.ts /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | end_of_line = lf 5 | insert_final_newline = true 6 | trim_trailing_whitespace = true 7 | charset = utf-8 8 | 9 | [*.js] 10 | indent_style = space 11 | indent_size = 2 12 | 13 | [{package.json,*.yml,*.cjson}] 14 | indent_style = space 15 | indent_size = 2 16 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [danielroe] 2 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/---bug-report.yml: -------------------------------------------------------------------------------- 1 | name: 🐛 Bug report 2 | description: Something's not working 3 | labels: [bug] 4 | body: 5 | - type: textarea 6 | validations: 7 | required: true 8 | attributes: 9 | label: 🐛 The bug 10 | description: What isn't working? Describe what the bug is. 11 | - type: input 12 | validations: 13 | required: true 14 | attributes: 15 | label: 🛠️ To reproduce 16 | description: A reproduction of the bug via https://stackblitz.com/github/unjs/errx/tree/main/playground 17 | placeholder: https://stackblitz.com/[...] 18 | - type: textarea 19 | validations: 20 | required: true 21 | attributes: 22 | label: 🌈 Expected behaviour 23 | description: What did you expect to happen? Is there a section in the docs about this? 24 | - type: textarea 25 | attributes: 26 | label: ℹ️ Additional context 27 | description: Add any other context about the problem here. 28 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: ci 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - main 7 | push: 8 | branches: 9 | - main 10 | - renovate/* 11 | 12 | jobs: 13 | lint: 14 | runs-on: ubuntu-latest 15 | 16 | steps: 17 | - uses: actions/checkout@v4 18 | - run: npm i -g --force corepack && corepack enable 19 | - uses: actions/setup-node@v4 20 | with: 21 | node-version: 20 22 | cache: pnpm 23 | 24 | - name: 📦 Install dependencies 25 | run: pnpm install 26 | 27 | - name: 🔠 Lint project 28 | run: pnpm lint 29 | 30 | test: 31 | runs-on: ubuntu-latest 32 | 33 | steps: 34 | - uses: actions/checkout@v4 35 | - run: npm i -g --force corepack && corepack enable 36 | - uses: actions/setup-node@v4 37 | with: 38 | node-version: 20 39 | cache: pnpm 40 | 41 | - name: 📦 Install dependencies 42 | run: pnpm install 43 | 44 | - name: 🛠 Build project 45 | run: pnpm build 46 | 47 | - name: 💪 Test types 48 | run: pnpm test:types 49 | 50 | - name: 🧪 Test project 51 | run: pnpm test:unit -- --coverage 52 | 53 | - name: 🟩 Coverage 54 | uses: codecov/codecov-action@v5 55 | -------------------------------------------------------------------------------- /.github/workflows/codeql.yml: -------------------------------------------------------------------------------- 1 | name: CodeQL 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: [main] 8 | schedule: 9 | - cron: '31 15 * * 5' 10 | 11 | jobs: 12 | analyze: 13 | name: Analyze 14 | runs-on: ubuntu-latest 15 | permissions: 16 | actions: read 17 | contents: read 18 | security-events: write 19 | 20 | strategy: 21 | fail-fast: false 22 | matrix: 23 | language: [javascript] 24 | 25 | steps: 26 | - name: Checkout 27 | uses: actions/checkout@v4 28 | 29 | - name: Initialize CodeQL 30 | uses: github/codeql-action/init@v3 31 | with: 32 | languages: ${{ matrix.language }} 33 | queries: +security-and-quality 34 | 35 | - name: Autobuild 36 | uses: github/codeql-action/autobuild@v3 37 | 38 | - name: Perform CodeQL Analysis 39 | uses: github/codeql-action/analyze@v3 40 | with: 41 | category: '/language:${{ matrix.language }}' 42 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | node_modules 3 | coverage 4 | .vscode 5 | .DS_Store 6 | .eslintcache 7 | *.log* 8 | *.env* 9 | -------------------------------------------------------------------------------- /CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @danielroe 2 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to make participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | - Using welcoming and inclusive language 12 | - Being respectful of differing viewpoints and experiences 13 | - Gracefully accepting constructive criticism 14 | - Focusing on what is best for the community 15 | - Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | - The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | - Trolling, insulting/derogatory comments, and personal or political attacks 21 | - Public or private harassment 22 | - Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | - Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies within all project spaces, and it also applies when an individual is representing the project or its community in public spaces. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at [daniel@roe.dev](mailto:daniel@roe.dev). All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 44 | 45 | [homepage]: https://www.contributor-covenant.org 46 | 47 | For answers to common questions about this code of conduct, see https://www.contributor-covenant.org/faq 48 | -------------------------------------------------------------------------------- /LICENCE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Daniel Roe 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # errx 2 | 3 | [![npm version][npm-version-src]][npm-version-href] 4 | [![npm downloads][npm-downloads-src]][npm-downloads-href] 5 | [![Github Actions][github-actions-src]][github-actions-href] 6 | [![Codecov][codecov-src]][codecov-href] 7 | 8 | > Zero dependency library to capture and parse stack traces in Node, Bun, Deno and more. 9 | 10 | ## Usage 11 | 12 | Install package: 13 | 14 | ```sh 15 | # npm 16 | npm install errx 17 | 18 | # pnpm 19 | pnpm install errx 20 | ``` 21 | 22 | ```js 23 | import { captureRawStackTrace, captureStackTrace, parseRawStackTrace } from 'errx' 24 | 25 | // returns raw string stack trace 26 | captureRawStackTrace() 27 | // returns parsed stack trace 28 | captureStackTrace() 29 | 30 | console.log(captureStackTrace()) 31 | // [{ 32 | // function: undefined, 33 | // source: 'file:///code/unjs/errx/playground/index.js', 34 | // line: '5', 35 | // column: '13' 36 | // }] 37 | ``` 38 | 39 | ## 💻 Development 40 | 41 | - Clone this repository 42 | - Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable` 43 | - Install dependencies using `pnpm install` 44 | - Run interactive tests using `pnpm dev` 45 | 46 | ## License 47 | 48 | Made with ❤️ 49 | 50 | Published under [MIT License](./LICENCE). 51 | 52 | 53 | 54 | [npm-version-src]: https://img.shields.io/npm/v/errx?style=flat-square 55 | [npm-version-href]: https://npmjs.com/package/errx 56 | [npm-downloads-src]: https://img.shields.io/npm/dm/errx?style=flat-square 57 | [npm-downloads-href]: https://npm.chart.dev/errx 58 | [github-actions-src]: https://img.shields.io/github/actions/workflow/status/unjs/errx/ci.yml?branch=main&style=flat-square 59 | [github-actions-href]: https://github.com/unjs/errx/actions?query=workflow%3Aci 60 | [codecov-src]: https://img.shields.io/codecov/c/gh/unjs/errx/main?style=flat-square 61 | [codecov-href]: https://codecov.io/gh/unjs/errx 62 | -------------------------------------------------------------------------------- /build.config.ts: -------------------------------------------------------------------------------- 1 | import { cp, readdir, rm } from 'node:fs/promises' 2 | import { defineBuildConfig } from 'unbuild' 3 | 4 | export default defineBuildConfig({ 5 | hooks: { 6 | 'rollup:done': async function () { 7 | // default to .js and .d.ts extensions 8 | for (const file of await readdir('dist')) { 9 | if (file.endsWith('.mjs') || file.endsWith('.d.mts')) { 10 | await cp(`dist/${file}`, `dist/${file.replace('.mjs', '.js').replace('.d.mts', '.d.ts')}`) 11 | await rm(`dist/${file}`) 12 | } 13 | } 14 | }, 15 | }, 16 | }) 17 | -------------------------------------------------------------------------------- /eslint.config.js: -------------------------------------------------------------------------------- 1 | import antfu from '@antfu/eslint-config' 2 | 3 | export default antfu() 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "errx", 3 | "type": "module", 4 | "version": "0.1.0", 5 | "packageManager": "pnpm@10.11.0", 6 | "description": "Zero dependency library to capture and parse stack traces in Node, Bun and Deno", 7 | "license": "MIT", 8 | "repository": "unjs/errx", 9 | "sideEffects": false, 10 | "exports": { 11 | ".": "./dist/index.js" 12 | }, 13 | "main": "./dist/index.js", 14 | "module": "./dist/index.js", 15 | "types": "./dist/index.d.ts", 16 | "files": [ 17 | "dist" 18 | ], 19 | "scripts": { 20 | "build": "unbuild", 21 | "dev": "vitest dev", 22 | "lint": "eslint . --fix", 23 | "prepare": "simple-git-hooks", 24 | "prepack": "pnpm build", 25 | "prepublishOnly": "pnpm lint && pnpm test", 26 | "release": "bumpp && pnpm publish", 27 | "test": "pnpm test:unit && pnpm test:types", 28 | "test:unit": "vitest", 29 | "test:types": "tsc --noEmit" 30 | }, 31 | "devDependencies": { 32 | "@antfu/eslint-config": "latest", 33 | "@types/node": "latest", 34 | "@vitest/coverage-v8": "latest", 35 | "bumpp": "latest", 36 | "eslint": "latest", 37 | "lint-staged": "latest", 38 | "simple-git-hooks": "latest", 39 | "typescript": "latest", 40 | "unbuild": "latest", 41 | "vite": "latest", 42 | "vitest": "latest" 43 | }, 44 | "resolutions": { 45 | "errx": "link:." 46 | }, 47 | "simple-git-hooks": { 48 | "pre-commit": "npx lint-staged" 49 | }, 50 | "lint-staged": { 51 | "*.{js,ts,mjs,cjs,json,.*rc}": [ 52 | "pnpm eslint --fix" 53 | ] 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /playground/index.js: -------------------------------------------------------------------------------- 1 | import assert from 'node:assert' 2 | import { captureStackTrace } from 'errx' 3 | 4 | // eslint-disable-next-line no-console 5 | console.log(captureStackTrace()) 6 | 7 | const [thisFile] = captureStackTrace() 8 | assert.equal(thisFile.source, import.meta.url) 9 | -------------------------------------------------------------------------------- /playground/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "type": "module", 3 | "private": true, 4 | "scripts": { 5 | "dev": "node index.js" 6 | }, 7 | "dependencies": { 8 | "errx": "latest" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /pnpm-workspace.yaml: -------------------------------------------------------------------------------- 1 | packages: 2 | - playground 3 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "extends": [ 4 | "github>danielroe/renovate" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | const IS_ABSOLUTE_RE = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^[a-z]:[/\\]/i 2 | const LINE_RE = /^\s+at (?:(?[^)]+) \()?(?[^)]+)\)?$/u 3 | const SOURCE_RE = /^(?.+):(?\d+):(?\d+)$/u 4 | 5 | export interface ParsedTrace { 6 | column?: number 7 | function?: string 8 | line?: number 9 | source: string 10 | } 11 | 12 | export function captureRawStackTrace() { 13 | if (!Error.captureStackTrace) { 14 | return 15 | } 16 | 17 | // eslint-disable-next-line unicorn/error-message 18 | const stack = new Error() 19 | Error.captureStackTrace(stack) 20 | return stack.stack 21 | } 22 | 23 | export function captureStackTrace() { 24 | const stack = captureRawStackTrace() 25 | 26 | return stack ? parseRawStackTrace(stack) : [] 27 | } 28 | 29 | export function parseRawStackTrace(stacktrace: string) { 30 | const trace: ParsedTrace[] = [] 31 | for (const line of stacktrace.split('\n')) { 32 | const parsed = LINE_RE.exec(line)?.groups as Partial> | undefined 33 | if (!parsed) { 34 | continue 35 | } 36 | 37 | if (!parsed.source) { 38 | continue 39 | } 40 | 41 | const parsedSource = SOURCE_RE.exec(parsed.source)?.groups 42 | if (parsedSource) { 43 | Object.assign(parsed, parsedSource) 44 | } 45 | 46 | if (IS_ABSOLUTE_RE.test(parsed.source)) { 47 | parsed.source = `file://${parsed.source}` 48 | } 49 | 50 | if (parsed.source === import.meta.url) { 51 | continue 52 | } 53 | 54 | for (const key of ['line', 'column'] as const) { 55 | if (parsed[key]) { 56 | // @ts-expect-error assigning number to string 57 | parsed[key] = Number(parsed[key]) 58 | } 59 | } 60 | 61 | trace.push(parsed as ParsedTrace) 62 | } 63 | 64 | return trace 65 | } 66 | -------------------------------------------------------------------------------- /test/__snapshots__/bun.json5: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "function": "moduleEvaluation", 4 | "source": "native", 5 | }, 6 | { 7 | "function": "moduleEvaluation", 8 | "source": "native", 9 | }, 10 | { 11 | "function": "", 12 | "source": "native", 13 | }, 14 | { 15 | "function": "asyncFunctionResume", 16 | "source": "native", 17 | }, 18 | { 19 | "function": "promiseReactionJobWithoutPromiseUnwrapAsyncContext", 20 | "source": "native", 21 | }, 22 | { 23 | "function": "promiseReactionJob", 24 | "source": "native", 25 | }, 26 | ] -------------------------------------------------------------------------------- /test/__snapshots__/deno.json5: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "column": 9, 4 | "function": "captureStackTrace", 5 | "line": 19, 6 | "source": "file:///some/path", 7 | }, 8 | ] -------------------------------------------------------------------------------- /test/__snapshots__/jiti.json5: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "column": 247313, 4 | "function": "evalModule", 5 | "line": 1, 6 | "source": "file:///Users/daniel/.npm/_npx/d5f9a72d28c5edfe/node_modules/jiti/dist/jiti.js", 7 | }, 8 | { 9 | "column": 245241, 10 | "function": "jiti", 11 | "line": 1, 12 | "source": "file:///Users/daniel/.npm/_npx/d5f9a72d28c5edfe/node_modules/jiti/dist/jiti.js", 13 | }, 14 | { 15 | "column": 1, 16 | "function": "Object.", 17 | "line": 16, 18 | "source": "file:///Users/daniel/.npm/_npx/d5f9a72d28c5edfe/node_modules/jiti/bin/jiti.js", 19 | }, 20 | { 21 | "column": 14, 22 | "function": "Module._compile", 23 | "line": 1376, 24 | "source": "node:internal/modules/cjs/loader", 25 | }, 26 | { 27 | "column": 10, 28 | "function": "Module._extensions..js", 29 | "line": 1435, 30 | "source": "node:internal/modules/cjs/loader", 31 | }, 32 | { 33 | "column": 32, 34 | "function": "Module.load", 35 | "line": 1207, 36 | "source": "node:internal/modules/cjs/loader", 37 | }, 38 | { 39 | "column": 12, 40 | "function": "Module._load", 41 | "line": 1023, 42 | "source": "node:internal/modules/cjs/loader", 43 | }, 44 | { 45 | "column": 12, 46 | "function": "Function.executeUserEntryPoint [as runMain]", 47 | "line": 135, 48 | "source": "node:internal/modules/run_main", 49 | }, 50 | { 51 | "column": 49, 52 | "function": undefined, 53 | "line": 28, 54 | "source": "node:internal/main/run_main_module", 55 | }, 56 | ] -------------------------------------------------------------------------------- /test/__snapshots__/vitest.json5: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "column": 12, 4 | "function": undefined, 5 | "line": 6, 6 | "source": "file:///Users/daniel/code/unjs/errx/test/index.test.ts", 7 | }, 8 | { 9 | "column": 14, 10 | "function": undefined, 11 | "line": 135, 12 | "source": "file:///Users/daniel/code/unjs/errx/node_modules/.pnpm/@vitest+runner@1.6.0/node_modules/@vitest/runner/dist/index.js", 13 | }, 14 | { 15 | "column": 26, 16 | "function": undefined, 17 | "line": 60, 18 | "source": "file:///Users/daniel/code/unjs/errx/node_modules/.pnpm/@vitest+runner@1.6.0/node_modules/@vitest/runner/dist/index.js", 19 | }, 20 | { 21 | "column": 17, 22 | "function": "runTest", 23 | "line": 781, 24 | "source": "file:///Users/daniel/code/unjs/errx/node_modules/.pnpm/@vitest+runner@1.6.0/node_modules/@vitest/runner/dist/index.js", 25 | }, 26 | { 27 | "column": 15, 28 | "function": "runSuite", 29 | "line": 909, 30 | "source": "file:///Users/daniel/code/unjs/errx/node_modules/.pnpm/@vitest+runner@1.6.0/node_modules/@vitest/runner/dist/index.js", 31 | }, 32 | { 33 | "column": 15, 34 | "function": "runSuite", 35 | "line": 909, 36 | "source": "file:///Users/daniel/code/unjs/errx/node_modules/.pnpm/@vitest+runner@1.6.0/node_modules/@vitest/runner/dist/index.js", 37 | }, 38 | { 39 | "column": 5, 40 | "function": "runFiles", 41 | "line": 958, 42 | "source": "file:///Users/daniel/code/unjs/errx/node_modules/.pnpm/@vitest+runner@1.6.0/node_modules/@vitest/runner/dist/index.js", 43 | }, 44 | { 45 | "column": 3, 46 | "function": "startTests", 47 | "line": 967, 48 | "source": "file:///Users/daniel/code/unjs/errx/node_modules/.pnpm/@vitest+runner@1.6.0/node_modules/@vitest/runner/dist/index.js", 49 | }, 50 | { 51 | "column": 7, 52 | "function": undefined, 53 | "line": 116, 54 | "source": "file:///Users/daniel/code/unjs/errx/node_modules/.pnpm/vitest@1.6.0_@types+node@20.14.9/node_modules/vitest/dist/chunks/runtime-runBaseTests.oAvMKtQC.js", 55 | }, 56 | ] 57 | -------------------------------------------------------------------------------- /test/index.test.ts: -------------------------------------------------------------------------------- 1 | import { fileURLToPath } from 'node:url' 2 | import { describe, expect, it } from 'vitest' 3 | import { captureStackTrace, parseRawStackTrace } from '../src' 4 | 5 | describe('errx', () => { 6 | it('works', () => { 7 | const trace = captureStackTrace().map(t => ({ 8 | ...t, 9 | column: typeof t.column === 'number' ? '' : undefined, 10 | line: typeof t.line === 'number' ? '' : undefined, 11 | source: t.source.replace(/^(.*node_modules\/)+/, ''), 12 | })) 13 | expect(trace).toMatchInlineSnapshot(` 14 | [ 15 | { 16 | "column": "", 17 | "function": undefined, 18 | "line": "", 19 | "source": "${import.meta.url}", 20 | }, 21 | { 22 | "column": "", 23 | "function": undefined, 24 | "line": "", 25 | "source": "@vitest/runner/dist/index.js", 26 | }, 27 | { 28 | "column": "", 29 | "function": undefined, 30 | "line": "", 31 | "source": "@vitest/runner/dist/index.js", 32 | }, 33 | { 34 | "column": "", 35 | "function": "runTest", 36 | "line": "", 37 | "source": "@vitest/runner/dist/index.js", 38 | }, 39 | { 40 | "column": "", 41 | "function": "processTicksAndRejections", 42 | "line": "", 43 | "source": "node:internal/process/task_queues", 44 | }, 45 | { 46 | "column": "", 47 | "function": "runSuite", 48 | "line": "", 49 | "source": "@vitest/runner/dist/index.js", 50 | }, 51 | { 52 | "column": "", 53 | "function": "runSuite", 54 | "line": "", 55 | "source": "@vitest/runner/dist/index.js", 56 | }, 57 | { 58 | "column": "", 59 | "function": "runFiles", 60 | "line": "", 61 | "source": "@vitest/runner/dist/index.js", 62 | }, 63 | ] 64 | `) 65 | }) 66 | }) 67 | 68 | const sourcePath = fileURLToPath(new URL('../src/index.ts', import.meta.url)) 69 | 70 | const vitestTrace = ` 71 | Error: 72 | at Module.getTrace (${sourcePath}:10:9) 73 | at /Users/daniel/code/unjs/errx/test/index.test.ts:6:12 74 | at file:///Users/daniel/code/unjs/errx/node_modules/.pnpm/@vitest+runner@1.6.0/node_modules/@vitest/runner/dist/index.js:135:14 75 | at file:///Users/daniel/code/unjs/errx/node_modules/.pnpm/@vitest+runner@1.6.0/node_modules/@vitest/runner/dist/index.js:60:26 76 | at runTest (file:///Users/daniel/code/unjs/errx/node_modules/.pnpm/@vitest+runner@1.6.0/node_modules/@vitest/runner/dist/index.js:781:17) 77 | at runSuite (file:///Users/daniel/code/unjs/errx/node_modules/.pnpm/@vitest+runner@1.6.0/node_modules/@vitest/runner/dist/index.js:909:15) 78 | at runSuite (file:///Users/daniel/code/unjs/errx/node_modules/.pnpm/@vitest+runner@1.6.0/node_modules/@vitest/runner/dist/index.js:909:15) 79 | at runFiles (file:///Users/daniel/code/unjs/errx/node_modules/.pnpm/@vitest+runner@1.6.0/node_modules/@vitest/runner/dist/index.js:958:5) 80 | at startTests (file:///Users/daniel/code/unjs/errx/node_modules/.pnpm/@vitest+runner@1.6.0/node_modules/@vitest/runner/dist/index.js:967:3) 81 | at file:///Users/daniel/code/unjs/errx/node_modules/.pnpm/vitest@1.6.0_@types+node@20.14.9/node_modules/vitest/dist/chunks/runtime-runBaseTests.oAvMKtQC.js:116:7` 82 | 83 | const jitiTrace = ` 84 | Error 85 | at getTrace (${sourcePath}:20:9) 86 | at ${sourcePath}:39:13 87 | at evalModule (/Users/daniel/.npm/_npx/d5f9a72d28c5edfe/node_modules/jiti/dist/jiti.js:1:247313) 88 | at jiti (/Users/daniel/.npm/_npx/d5f9a72d28c5edfe/node_modules/jiti/dist/jiti.js:1:245241) 89 | at Object. (/Users/daniel/.npm/_npx/d5f9a72d28c5edfe/node_modules/jiti/bin/jiti.js:16:1) 90 | at Module._compile (node:internal/modules/cjs/loader:1376:14) 91 | at Module._extensions..js (node:internal/modules/cjs/loader:1435:10) 92 | at Module.load (node:internal/modules/cjs/loader:1207:32) 93 | at Module._load (node:internal/modules/cjs/loader:1023:12) 94 | at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12) 95 | at node:internal/main/run_main_module:28:49` 96 | 97 | const bunTrace = ` 98 | Error: 99 | at getTrace (${sourcePath}:38:8) 100 | at module code (${sourcePath}:19:14) 101 | at moduleEvaluation (native) 102 | at moduleEvaluation (native) 103 | at (native) 104 | at asyncFunctionResume (native) 105 | at promiseReactionJobWithoutPromiseUnwrapAsyncContext (native) 106 | at promiseReactionJob (native)` 107 | 108 | const denoTrace = ` 109 | Error 110 | at captureStackTrace (file:///some/path:19:9) 111 | at file://${sourcePath}:59:13` 112 | 113 | describe('parseStackTrace', () => { 114 | it('parses vitest', () => { 115 | expect(parseRawStackTrace(vitestTrace)).toMatchFileSnapshot('__snapshots__/vitest.json5') 116 | }) 117 | it('parses jiti', () => { 118 | expect(parseRawStackTrace(jitiTrace)).toMatchFileSnapshot('__snapshots__/jiti.json5') 119 | }) 120 | it('parses bun', () => { 121 | expect(parseRawStackTrace(bunTrace)).toMatchFileSnapshot('__snapshots__/bun.json5') 122 | }) 123 | it('parses deno', () => { 124 | expect(parseRawStackTrace(denoTrace)).toMatchFileSnapshot('__snapshots__/deno.json5') 125 | }) 126 | }) 127 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es2022", 4 | "lib": [ 5 | "es2022" 6 | ], 7 | "moduleDetection": "force", 8 | "module": "preserve", 9 | "resolveJsonModule": true, 10 | "allowJs": true, 11 | "strict": true, 12 | "noImplicitOverride": true, 13 | "noUncheckedIndexedAccess": true, 14 | "noEmit": true, 15 | "esModuleInterop": true, 16 | "forceConsistentCasingInFileNames": true, 17 | "isolatedModules": true, 18 | "verbatimModuleSyntax": true, 19 | "skipLibCheck": true 20 | }, 21 | "include": [ 22 | "src", 23 | "test", 24 | "playground" 25 | ] 26 | } 27 | -------------------------------------------------------------------------------- /vitest.config.ts: -------------------------------------------------------------------------------- 1 | import { fileURLToPath } from 'node:url' 2 | import { defineConfig } from 'vitest/config' 3 | 4 | export default defineConfig({ 5 | resolve: { 6 | alias: { 7 | errx: fileURLToPath( 8 | new URL('./src/index.ts', import.meta.url).href, 9 | ), 10 | }, 11 | }, 12 | test: { 13 | coverage: { 14 | include: ['src'], 15 | reporter: ['text', 'json', 'html'], 16 | }, 17 | }, 18 | }) 19 | --------------------------------------------------------------------------------