Skip to content

feat(test): migrate Jest to Vitest#303

Open
koralle wants to merge 4 commits intohonojs:mainfrom
koralle:feat/migrate-jest-to-vitest
Open

feat(test): migrate Jest to Vitest#303
koralle wants to merge 4 commits intohonojs:mainfrom
koralle:feat/migrate-jest-to-vitest

Conversation

@koralle
Copy link

@koralle koralle commented Feb 11, 2026

Summary

I migrated our test framework from Jest to Vitest, considering aspects such as performance and development experience.

Consideration

In the test case 'should return undefined if stream is closed', the test for the function readWithoutBlocking() is failing.
However, the following ReadableStream should be rejected when reader.read() is executed, and I believe the verification method for readWithoutBlocking() is incorrect.

However, when I ran the tests with Jest, this test case passed, and I don't understand why.

const body = new ReadableStream({
  async start() {
    throw new Error('test')
  },
})

const response = new Response(body)
const reader = response.body!.getReader()
const readPromise = reader.read()

For now, I'm verifying that an exception is thrown by ReadableStream... I'd like you to think with me about what this test case should ideally be.

@usualoma
Copy link
Member

Thank you for your assistance!
I believe the existing test was written incorrectly. Please add and modify it with the following content.

diff --git i/test/utils.test.ts w/test/utils.test.ts
index 9837a7d..b5940c2 100644
--- i/test/utils.test.ts
+++ w/test/utils.test.ts
@@ -177,7 +177,21 @@ describe('readWithoutBlocking', () => {
     expect(result3).toEqual({ done: true, value: undefined })
   })
 
-  it('should return undefined if stream is closed', async () => {
+  it('should return undefined value if stream is closed in start', async () => {
+    const body = new ReadableStream({
+      async start(controller) {
+        controller.close()
+      },
+    })
+    const response = new Response(body)
+    const reader = response.body!.getReader()
+    const readPromise = reader.read()
+
+    const result = await readWithoutBlocking(readPromise)
+    expect(result).toEqual({ done: true, value: undefined })
+  })
+
+  it('should return undefined if stream is errored in start', async () => {
     const body = new ReadableStream({
       async start() {
         throw new Error('test')
@@ -187,7 +201,7 @@ describe('readWithoutBlocking', () => {
     const reader = response.body!.getReader()
     const readPromise = reader.read()
 
-    await expect(readPromise).rejects.toThrow('test')
+    expect(readWithoutBlocking(readPromise)).rejects.toThrow('test')
   })
 
   it('should return undefined if stream is errored', async () => {
@@ -200,7 +214,6 @@ describe('readWithoutBlocking', () => {
     const reader = response.body!.getReader()
     const readPromise = reader.read()
 
-    const result = await readWithoutBlocking(readPromise).catch(() => undefined)
-    expect(result).toBeUndefined()
+    expect(readWithoutBlocking(readPromise)).rejects.toThrow('test')
   })
 })

…cases

Ensure closed streams resolve with `{ done: true, value: undefined }`
and errored streams properly reject.
@koralle koralle force-pushed the feat/migrate-jest-to-vitest branch from 97f9448 to 167b3fd Compare February 11, 2026 08:11
@koralle
Copy link
Author

koralle commented Feb 11, 2026

@usualoma
Thank you for your advice!

As you pointed out, I have modified the test code for some existing test cases related to readWithoutBlocking().
Additionally, I have added new test cases.

@yusukebe
Copy link
Member

@koralle

If this PR is ready, please ping me!

@usualoma
Copy link
Member

Hi @koralle
Thank you for the update.

Since --expose-gc isn't being passed to the execution environment, I think it would be best to do it as follows.

diff --git i/package.json w/package.json
index e1221b7..5b22f6f 100644
--- i/package.json
+++ w/package.json
@@ -54,7 +54,7 @@
     }
   },
   "scripts": {
-    "test": "node --expose-gc node_modules/.bin/vitest",
+    "test": "vitest",
     "build": "tsup --external hono",
     "watch": "tsup --watch",
     "postbuild": "publint",
diff --git i/vitest.config.ts w/vitest.config.ts
index 5031913..2590435 100644
--- i/vitest.config.ts
+++ w/vitest.config.ts
@@ -6,6 +6,7 @@ export default defineConfig({
     include: ['test/**/*.test.ts'],
     environment: 'node',
     globals: true,
+    execArgv: ['--expose-gc'],
     setupFiles: ["./test/setup.ts"]
   }
 })

@yusukebe yusukebe added the v2 label Feb 15, 2026
@koralle koralle marked this pull request as ready for review February 17, 2026 10:58
@koralle
Copy link
Author

koralle commented Feb 17, 2026

@usualoma

Thank you.

I have modified vitest.config.ts and package.json to pass the --expose-gc option to the node process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments