Back to Repositories

Testing Stacktrace Parser Implementation in uni-app

This test suite validates the stacktracey functionality in uni-app, focusing on error stack trace parsing and formatting for both UNI and UTS environments. The tests ensure proper error message transformation and source mapping capabilities.

Test Coverage Overview

The test suite provides comprehensive coverage of stacktracey parsing functionality.

Key areas tested include:
  • UNI stacktrace preset with local configurations
  • UTS stacktrace preset with source mapping
  • Error message transformation and formatting
  • Path resolution and source map integration

Implementation Analysis

The testing approach utilizes Jest’s asynchronous testing capabilities to validate stacktrace transformations. The implementation leverages preset configurations for both UNI and UTS environments, with specific focus on path resolution and source mapping integration.

Technical patterns include:
  • Promise-based error handling
  • Custom preset configurations
  • Source map path resolution
  • Error message parsing and formatting

Technical Details

Testing infrastructure includes:
  • Jest test framework
  • TypeScript for type safety
  • Path module for filesystem operations
  • Custom stacktracey presets (uniStracktraceyPreset, utsStracktraceyPreset)
  • Mock error messages for both UNI and UTS environments

Best Practices Demonstrated

The test suite exhibits several testing best practices for error handling and stack trace processing.

Notable practices include:
  • Isolated test cases for different preset configurations
  • Comprehensive error message validation
  • Clear separation of concerns between UNI and UTS testing
  • Proper async/await pattern usage
  • Explicit expected output validation

dcloudio/uni-app

packages/uni-stacktracey/__tests__/uni-stacktracey.spec.ts

            
const path = require('path')
const {
  stacktracey,
  uniStracktraceyPreset,
  utsStracktraceyPreset,
} = require('../dist/uni-stacktracey.cjs.js')

const utsProjectDir = path.resolve(__dirname, '../test/uts')

const utsErrorMsg = `Error: 
${path.resolve(
  utsProjectDir,
  'unpackage/dist/dev/app-plus/uni_modules/test-uts/app-android/index.kt'
)}:59:67: error: unresolved reference: UTSJSONObject
open suspend fun testClassAsync(opts: AsyncOptions): Deferred<UTSJSONObject> = CoroutineScope(Dispatchers.Default).async {
                                                              ^
`

const uniErrorMsg = `Error: Sentry Error
at a.throwError(/static/js/pages-index-index.3ab0d0e5.js:1:567)
at click(/static/js/pages-index-index.3ab0d0e5.js:1:2745)
at ee(/static/js/chunk-vendors.75525bd5.js:34:11927)
at n(/static/js/chunk-vendors.75525bd5.js:34:13747)
at ee(/static/js/chunk-vendors.75525bd5.js:34:11927)
at HTMLElement.n(/static/js/chunk-vendors.75525bd5.js:34:13824)
at HTMLElement.o._wrapper(/static/js/chunk-vendors.75525bd5.js:34:53966)
at HTMLElement.i(/static/js/chunk-vendors.75525bd5.js:7:609894)`

describe('uni-stacktracey', () => {
  test('uniStracktraceyPreset local', () => {
    stacktracey(uniErrorMsg, {
      preset: uniStracktraceyPreset({
        base: path.resolve(__dirname, '../test/__UNI_APPID__/h5/1.0.0/'),
        sourceRoot: '',
      }),
    }).then((res: string) => {
      expect(res).toEqual(`Error: Sentry Error
at   src/pages/index/index.vue:44:0
at   src/pages/index/index.vue?be58:12:17
at   node_modules/@sentry/browser/esm/helpers.js:74:22
at   node_modules/@dcloudio/vue-cli-plugin-uni/packages/h5-vue/dist/vue.runtime.esm.js:1864:25
at   node_modules/@dcloudio/vue-cli-plugin-uni/packages/h5-vue/dist/vue.runtime.esm.js:2189:13
at   node_modules/@dcloudio/vue-cli-plugin-uni/packages/h5-vue/dist/vue.runtime.esm.js:1864:25
at   node_modules/@dcloudio/vue-cli-plugin-uni/packages/h5-vue/dist/vue.runtime.esm.js:2185:8
at   node_modules/@dcloudio/vue-cli-plugin-uni/packages/h5-vue/dist/vue.runtime.esm.js:7076:24`)
    })
  })

  test('utsStracktraceyPreset local', () => {
    stacktracey(utsErrorMsg, {
      preset: utsStracktraceyPreset({
        inputRoot: '/Users/fxy/Projects/Demo/my-vue3-project-uts/src',
        outputRoot: path.resolve(utsProjectDir, 'unpackage/dist/dev/app-plus'),
        sourceMapRoot: path.resolve(
          utsProjectDir,
          'unpackage/dist/dev/.sourcemap/app-plus'
        ),
      }),
    }).then((res: string) => {
      expect(res).toEqual(`Error:
at uni_modules/test-uts/app-android/index.uts:82:52
error: unresolved reference: UTSJSONObject
open suspend fun testClassAsync(opts: AsyncOptions): Deferred<UTSJSONObject> = CoroutineScope(Dispatchers.Default).async {
^
`)
    })
  })
})