Skip to content

Testing Error Scenarios with Eden Treaty is Broken #1055

@antoniel

Description

@antoniel

What version of Elysia is running?

1.2.12

What platform is your computer?

Darwin 24.0.0 arm64 arm

What steps can reproduce the bug?

When testing error scenarios using Eden Treaty, the error object is not properly formatted, making it difficult to write assertions in tests.

import { describe, expect, it } from "bun:test"
import { Elysia } from "elysia"
import { treaty } from "@elysiajs/eden"

const app = new Elysia().get("/hello", (ctx) => {
  return ctx.error(418, "I like tea")
})
const api = treaty(app)

describe("Elysia", () => {
  // This one Passes
  it("Testing using Request", async () => {
    const response = await app.handle(
      new Request("http://localhost/hello", {
        method: "GET",
      })
    )

    expect(response.status).toBe(418)
    expect(await response.text()).toEqual("I like tea")
  })
  // This one Fails
  it("Testing using treaty", async () => {
    const { data, error } = await api.hello.get()

    expect(error).toBe({ status: 418, value: "I like tea" })
  })
})

What is the expected behavior?

The error object should be properly structured and readable, as shown in the documentation.

What do you see instead?

Expected: {
  status: 418,
  value: "I like tea",
}
Received: 1 | var s=class extends Error{constructor(e,n){super(n+"");this.status=e;this.value=n}};var i=/(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/,o=/(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat)\s(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s\d{2}\s\d{4}\s\d{2}:\d{2}:\d{2}\sGMT(?:\+|-)\d{4}\s\([^)]+\)/,c=/^(?:(?:(?:(?:0?[1-9]|[12][0-9]|3[01])[/\s-](?:0?[1-9]|1[0-2])[/\s-](?:19|20)\d{2})|(?:(?:19|20)\d{2}[/\s-](?:0?[1-9]|1[0-2])[/\s-](?:0?[1-9]|[12][0-9]|3[01]))))(?:\s(?:1[012]|0?[1-9]):[0-5][0-9](?::[0-5][0-9])?(?:\s[AP]M)?)?$/,u=t=>t.trim().length!==0&&!Number.isNaN(Number(t)),d=t=>{if(typeof t!="string")return null;let r=t.replace(/"/g,"");if(i.test(r)||o.test(r)||c.test(r)){let e=new Date(r);if(!Number.isNaN(e.getTime()))return e}return null},a=t=>{let r=t.charCodeAt(0),e=t.charCodeAt(t.length-1);return r===123&&e===125||r===91&&e===93},p=t=>JSON.parse(t,(r,e)=>{

error: I like tea
      at new s (/Users/antonielmagalhaes/d/antoniel/carteira/node_modules/@elysiajs/eden/dist/chunk-XYW4OUFN.mjs:1:44)
      at /Users/antonielmagalhaes/d/antoniel/carteira/node_modules/@elysiajs/eden/dist/chunk-7WO4HTSU.mjs:1:4302


      at /Users/antonielmagalhaes/d/antoniel/carteira/src/server/routers/auth-router.test.ts:27:19
✗ Elysia > Testing using treaty [1.13ms]

 1 pass
 1 fail

Additional information

There is a PR (#118) that follows in the correct path but doesn't fix the entire problem, after apply this PR as a patch I was able to see the error, but in a stack trace format, which isn't expecte, see bellow:

error: expect(received).toBe(expected)

Expected: {
  status: 418,
  value: "I like tea",
}
Received:  6 |                 message = JSON.stringify(passedValue)
 7 |             } else {
 8 |                 message = String(passedValue || '')
 9 |             }
10 |         }
11 |         super(message)
             ^
error: I like tea
      at new s (/Users/antonielmagalhaes/d/antoniel/carteira/node_modules/@elysiajs/eden/dist/chunk-XYW4OUFN.mjs:11:9)
      at /Users/antonielmagalhaes/d/antoniel/carteira/node_modules/@elysiajs/eden/dist/chunk-7WO4HTSU.mjs:1:4302


      at /Users/antonielmagalhaes/d/antoniel/carteira/src/server/routers/auth-router.test.ts:27:19
✗ Elysia > Testing using treaty [1.21ms]

 1 pass
 1 fail
 3 expect() calls
Ran 2 tests across 1 files. [51.00ms]

Have you try removing the node_modules and bun.lockb and try again yet?

yes

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingreviewWaiting for issue reporter approval

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions