rsnext/test/e2e/ssr-react-context/index.test.ts
Jiachi Liu e9d23d709c
fix: react dev bundle is picked in prod mode (#39221)
When we detect if `reactRoot` rendering should be enabled we `require` the require to check the version. But at that time the `NODE_ENV` isn't set yet. Then the react dev build stays in the `require.cache` that any future require of react will get the wrong build. In that case, React dev bundle is picked in production mode.

Fun fact: if you're using hooks, that seem not to effect you, but context consumer works different then you couldn't get the proper context from provider.

Fixes #38176
Fixes #38765
Fixes #38332

## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
2022-08-01 14:21:42 +00:00

46 lines
1.4 KiB
TypeScript

import { join } from 'path'
import { renderViaHTTP, check } from 'next-test-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { createNext, FileRef } from 'e2e-utils'
describe('React Context', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
pages: new FileRef(join(__dirname, 'app/pages')),
'context.js': new FileRef(join(__dirname, 'app/context.js')),
},
})
})
afterAll(() => next.destroy())
it('should render a page with context', async () => {
const html = await renderViaHTTP(next.url, '/')
expect(html).toMatch(/Value: .*?hello world/)
})
it('should render correctly with context consumer', async () => {
const html = await renderViaHTTP(next.url, '/consumer')
expect(html).toMatch(/Value: .*?12345/)
})
if ((globalThis as any).isNextDev) {
it('should render with context after change', async () => {
const aboutAppPagePath = 'pages/_app.js'
const originalContent = await next.readFile(aboutAppPagePath)
await next.patchFile(
aboutAppPagePath,
originalContent.replace('hello world', 'new value')
)
try {
await check(() => renderViaHTTP(next.url, '/'), /Value: .*?new value/)
} finally {
await next.patchFile(aboutAppPagePath, originalContent)
}
await check(() => renderViaHTTP(next.url, '/'), /Value: .*?hello world/)
})
}
})