next-international icon indicating copy to clipboard operation
next-international copied to clipboard

Unit testing with createI18nServer in server-side components throws server-only error

Open Deer404 opened this issue 9 months ago • 1 comments

Describe the bug I encountered a server-only error when using createI18nServer in server-side components for unit testing.

To Reproduce Steps to reproduce the behavior:

  1. Use createI18nServer in a server-side component.
  2. Run the unit tests.

Expected behavior

Screenshots

// demo.test.tsx
import { describe, it, vi } from 'vitest'
import { render, screen, waitFor } from './customRender' 
import { createI18nServer } from 'next-international/server'



describe('Example test', () => {
  vi.mock("@server-only", () => ({}));
  it('just an example', async () => {
    const { getI18n } = createI18nServer({
      en: () => import('../locales/messages/enUS'),
    })
 
    async function App() {
      const t = await getI18n()
 
      return <p>{t("login.welcome")}</p>
    }
 
    render(
      <App/>
    )
 
 
    await waitFor(() => {
      expect(screen.getByText('Welcome to the platform')).toBeInTheDocument()
    })
  })
})

About (please complete the following information):

  • next-international version [ 1.2.4]
  • Next.js version [14.2.1]
  • Vitest version [1.6.0]

Deer404 avatar May 17 '24 08:05 Deer404