Repository: honojs/hono
File: src/helper/ssg/ssg.test.tsx (TypeScript)

Code before:
    const fsMock: FileSystemModule = {
      writeFile: vi.fn(() => Promise.reject(new Error('Write error'))),
      mkdir: vi.fn(() => Promise.resolve()),
    }

    const result = await toSSG(app, fsMock, { dir: './static' })
    expect(result.success).toBe(false)
    expect(result.files).toStrictEqual([])
    expect(result.error?.message).toBe('Write error')
  })

  it('Should handle overall process errors correctly in toSSG', async () => {
    const fsMock: FileSystemModule = {
      writeFile: vi.fn(() => Promise.reject(new Error('Write error'))),
      mkdir: vi.fn(() => Promise.resolve()),
    }

    const result = await toSSG(app, fsMock, { dir: './static' })

    expect(result.success).toBe(false)
    expect(result.error).toBeDefined()
    expect(result.files).toStrictEqual([])
  })

  it('Should correctly generate files with the expected paths', async () => {
    app.get('/data', (c) =>
      c.text(JSON.stringify({ title: 'hono' }), 200, {
        'Content-Type': 'application/json',
      })
    )
    await toSSG(app, fsMock, {
      dir: './static',
      extensionMap: {
        ...defaultExtensionMap,
        'application/json': 'json',
      },
    })

    expect(fsMock.writeFile).toHaveBeenCalledWith('static/index.html', expect.any(String))
    expect(fsMock.writeFile).toHaveBeenCalledWith('static/about.html', expect.any(String))
    expect(fsMock.writeFile).toHaveBeenCalledWith('static/about/some.txt', expect.any(String))
    expect(fsMock.writeFile).not.toHaveBeenCalledWith(
      'static/about/some/thing.txt',
      expect.any(String)
    )
    expect(fsMock.writeFile).toHaveBeenCalledWith('static/about.html', expect.any(String))
    expect(fsMock.writeFile).toHaveBeenCalledWith('static/bravo.html', expect.any(String))
    expect(fsMock.writeFile).toHaveBeenCalledWith('static/Charlie.html', expect.any(String))
    expect(fsMock.writeFile).toHaveBeenCalledWith('static/data.json', expect.any(String))
  })


Review comment (bug):
This is an actual bug. But this test will not fail if it does not have your fix. So I think the test should be like this, using a custom content type:

```diff
diff --git a/src/helper/ssg/ssg.test.tsx b/src/helper/ssg/ssg.test.tsx
index 8bf04251..0ffd2eab 100644
--- a/src/helper/ssg/ssg.test.tsx
+++ b/src/helper/ssg/ssg.test.tsx
@@ -162,15 +162,15 @@ describe('toSSG function', () => {

   it('Should correctly generate files with the expected paths', async () => {
     app.get('/data', (c) =>
-      c.text(JSON.stringify({ title: 'hono' }), 200, {
-        'Content-Type': 'application/json',
+      c.text('foo', 200, {
+        'Content-Type': 'text/x-foo',
       })
     )
     await toSSG(app, fsMock, {
       dir: './static',
       extensionMap: {
         ...defaultExtensionMap,
-        'application/json': 'json',
+        'text/x-foo': 'foo',
       },
     })

@@ -184,7 +184,7 @@ describe('toSSG function', () => {
     expect(fsMock.writeFile).toHaveBeenCalledWith('static/about.html', expect.any(String))
     expect(fsMock.writeFile).toHaveBeenCalledWith('static/bravo.html', expect.any(String))
     expect(fsMock.writeFile).toHaveBeenCalledWith('static/Charlie.html', expect.any(String))
-    expect(fsMock.writeFile).toHaveBeenCalledWith('static/data.json', expect.any(String))
+    expect(fsMock.writeFile).toHaveBeenCalledWith('static/data.foo', expect.any(String))
   })

   it('should modify the request if the hook is provided', async () => {
```

Code after:
    const fsMock: FileSystemModule = {
      writeFile: vi.fn(() => Promise.reject(new Error('Write error'))),
      mkdir: vi.fn(() => Promise.resolve()),
    }

    const result = await toSSG(app, fsMock, { dir: './static' })
    expect(result.success).toBe(false)
    expect(result.files).toStrictEqual([])
    expect(result.error?.message).toBe('Write error')
  })

  it('Should handle overall process errors correctly in toSSG', async () => {
    const fsMock: FileSystemModule = {
      writeFile: vi.fn(() => Promise.reject(new Error('Write error'))),
      mkdir: vi.fn(() => Promise.resolve()),
    }

    const result = await toSSG(app, fsMock, { dir: './static' })

    expect(result.success).toBe(false)
    expect(result.error).toBeDefined()
    expect(result.files).toStrictEqual([])
  })

  it('Should correctly generate files with the expected paths', async () => {
    app.get('/data', (c) =>
      c.text(JSON.stringify({ title: 'hono' }), 200, {
        'Content-Type': 'text/x-foo',
      })
    )
    await toSSG(app, fsMock, {
      dir: './static',
      extensionMap: {
        ...defaultExtensionMap,
        'text/x-foo': 'foo',
      },
    })

    expect(fsMock.writeFile).toHaveBeenCalledWith('static/index.html', expect.any(String))
    expect(fsMock.writeFile).toHaveBeenCalledWith('static/about.html', expect.any(String))
    expect(fsMock.writeFile).toHaveBeenCalledWith('static/about/some.txt', expect.any(String))
    expect(fsMock.writeFile).not.toHaveBeenCalledWith(
      'static/about/some/thing.txt',
      expect.any(String)
    )
    expect(fsMock.writeFile).toHaveBeenCalledWith('static/about.html', expect.any(String))
    expect(fsMock.writeFile).toHaveBeenCalledWith('static/bravo.html', expect.any(String))
    expect(fsMock.writeFile).toHaveBeenCalledWith('static/Charlie.html', expect.any(String))
    expect(fsMock.writeFile).toHaveBeenCalledWith('static/data.foo', expect.any(String))
  })