Skip to content

Commit 1488bce

Browse files
authored
docs: Update Jest examples (#1797)
Somehow the Jest examples in this repo still worked, but if you try to use `next-intl` outside of this monorepo there was an issue with Jest. The problem is that Jest doesn't support ESM (see e.g. vercel/next.js#40183). Ref #1796
1 parent d6c0e91 commit 1488bce

File tree

6 files changed

+86
-18
lines changed

6 files changed

+86
-18
lines changed

docs/src/pages/blog/next-intl-4-0.mdx

+2
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ The build output of `next-intl` has been modernized and now leverages the follow
188188
2. **Modern JSX transform:** The peer dependency for React has been bumped to v17 in order to use the more efficient, modern JSX transform.
189189
3. **Modern syntax:** Syntax is now compiled down to the Browserslist `defaults` query, which is a shortcut for ">0.5%, last 2 versions, Firefox ESR, not dead"—a baseline that is considered a reasonable target for modern apps.
190190

191+
If you're using `next-intl` with Jest or Vitest, please also refer to the new [testing docs](/docs/environments/testing).
192+
191193
With these changes, the bundle size of `next-intl` has been reduced by ~7% ([PR #1470](https://github.com/amannn/next-intl/pull/1470)).
192194

193195
## Improved inheritance of `NextIntlClientProvider` [#nextintlclientprovider-inheritance]

docs/src/pages/docs/environments/_meta.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ export default {
55
'error-files': 'Error files (e.g. not-found)',
66
mdx: 'Markdown (MDX)',
77
'core-library': 'Core library',
8-
'runtime-requirements': 'Runtime requirements'
8+
'runtime-requirements': 'Runtime requirements',
9+
testing: 'Testing'
910
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import Callout from '@/components/Callout';
2+
import Details from '@/components/Details';
3+
4+
# Testing
5+
6+
Components that use `next-intl` can be used in unit tests:
7+
8+
```tsx
9+
import {render} from '@testing-library/react';
10+
import {NextIntlClientProvider} from 'next-intl';
11+
import {expect, it} from 'vitest';
12+
import messages from '../../messages/en.json';
13+
import UserProfile from './UserProfile';
14+
15+
it('renders', () => {
16+
render(
17+
<NextIntlClientProvider locale="en" messages={messages}>
18+
<UserProfile />
19+
</NextIntlClientProvider>
20+
);
21+
});
22+
```
23+
24+
To avoid having to mock server components, it can be beneficial to define components as [non-async functions](/docs/environments/server-client-components#async-or-non-async), allowing you to flexibly render them in any environment.
25+
26+
## Vitest
27+
28+
`next-intl` is bundled as ESM-only, which requires the usage of [explicit file extensions](https://nodejs.org/api/esm.html#mandatory-file-extensions) internally. However, in order to avoid a [deoptimization in Next.js](https://github.com/vercel/next.js/issues/77200), `next-intl` currently has to import from `next/navigation` instead of `next/navigation.js`.
29+
30+
Vitest correctly assumes a file extension though, therefore for the time being, if you're using [`createNavigation`](/docs/routing/navigation), you need to ask Vitest to process imports within `next-intl`:
31+
32+
```tsx
33+
import {defineConfig} from 'vitest/config';
34+
35+
export default defineConfig({
36+
test: {
37+
server: {
38+
deps: {
39+
// https://github.com/vercel/next.js/issues/77200
40+
inline: ['next-intl']
41+
}
42+
}
43+
}
44+
});
45+
```
46+
47+
## Jest
48+
49+
Since Jest doesn't have built-in ESM support, you need to instruct Jest to transform imports from `next-intl`:
50+
51+
```tsx
52+
const nextJest = require('next/jest');
53+
54+
const createJestConfig = nextJest({dir: './'});
55+
56+
module.exports = async () => ({
57+
...(await createJestConfig({
58+
testEnvironment: 'jsdom',
59+
rootDir: 'src'
60+
})()),
61+
// https://github.com/vercel/next.js/issues/40183
62+
transformIgnorePatterns: ['node_modules/(?!next-intl)/']
63+
});
64+
```

examples/example-app-router-playground/jest.config.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ const nextJest = require('next/jest');
33

44
const createJestConfig = nextJest({dir: './'});
55

6-
module.exports = createJestConfig({
7-
moduleNameMapper: {
8-
'^next$': require.resolve('next'),
9-
'^next/navigation$': require.resolve('next/navigation')
10-
},
11-
testEnvironment: 'jsdom',
12-
rootDir: 'src'
6+
module.exports = async () => ({
7+
...(await createJestConfig({
8+
testEnvironment: 'jsdom',
9+
rootDir: 'src'
10+
})()),
11+
transformIgnorePatterns: ['node_modules/(?!next-intl)/']
1312
});

examples/example-app-router/jest.config.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ const nextJest = require('next/jest');
33

44
const createJestConfig = nextJest({dir: './'});
55

6-
module.exports = createJestConfig({
7-
moduleNameMapper: {
8-
'^next$': require.resolve('next'),
9-
'^next/navigation$': require.resolve('next/navigation')
10-
},
11-
testEnvironment: 'jsdom',
12-
rootDir: 'src'
6+
module.exports = async () => ({
7+
...(await createJestConfig({
8+
testEnvironment: 'jsdom',
9+
rootDir: 'src'
10+
})()),
11+
transformIgnorePatterns: ['node_modules/(?!next-intl)/']
1312
});

examples/example-pages-router-advanced/jest.config.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ const nextJest = require('next/jest');
33

44
const createJestConfig = nextJest({dir: './'});
55

6-
module.exports = createJestConfig({
7-
testEnvironment: 'jsdom',
8-
rootDir: 'src'
6+
module.exports = async () => ({
7+
...(await createJestConfig({
8+
testEnvironment: 'jsdom',
9+
rootDir: 'src'
10+
})()),
11+
transformIgnorePatterns: ['node_modules/(?!next-intl)/']
912
});

0 commit comments

Comments
 (0)