rsnext/test/e2e/app-dir/next-after-pages/index.test.ts
Sebastian Silbermann fe8d953e2d
Replace hasRedbox() (#67025)
...with `assertHasRedbox` and `assertNoRedbox`.

`hasRedbox()` has a hardcoded timeout of 5s that is only required for
the negative assertion.
Instead, we now have dedicated assertions for the positive
(`assertHasRedbox`) and negative case (`assertNoRedbox`).
The negative assertion still has the hardcoded timeout.
But the positive assertion just retries until we find the Redbox.

This speeds up tests using the positive assertion.
Removing `hasRedbox` also uncovered some unused expressions e.g. `await
hasRedbox(browser)`.
These expressions probably wanted to use `expect(await
hasRedbox(browser)).toBe(true)
2024-06-20 10:37:32 +02:00

80 lines
2.3 KiB
TypeScript

/* eslint-env jest */
import { nextTestSetup, isNextDev } from 'e2e-utils'
import { assertHasRedbox, getRedboxSource, retry } from 'next-test-utils'
import * as fs from 'fs'
import * as path from 'path'
import * as os from 'os'
import * as Log from './utils/log'
// using unstable_after is a compile-time error in build mode.
const _describe = isNextDev ? describe : describe.skip
_describe('unstable_after() - pages', () => {
const logFileDir = fs.mkdtempSync(path.join(os.tmpdir(), 'logs-'))
const logFile = path.join(logFileDir, 'logs.jsonl')
const { next } = nextTestSetup({
files: __dirname,
env: {
PERSISTENT_LOG_FILE: logFile,
},
})
let currentCliOutputIndex = 0
beforeEach(() => {
currentCliOutputIndex = next.cliOutput.length
})
const getLogs = () => {
return Log.readCliLogs(next.cliOutput.slice(currentCliOutputIndex))
}
it('runs in middleware', async () => {
const requestId = `${Date.now()}`
const res = await next.fetch(
`/middleware/redirect-source?requestId=${requestId}`,
{
redirect: 'follow',
headers: {
cookie: 'testCookie=testValue',
},
}
)
expect(res.status).toBe(200)
await retry(() => {
expect(getLogs()).toContainEqual({
source: '[middleware] /middleware/redirect-source',
requestId,
cookies: { testCookie: 'testValue' },
})
})
})
describe('invalid usages', () => {
describe('errors at compile time when used in pages dir', () => {
it.each([
{
title: 'errors when used in getServerSideProps',
path: '/pages-dir/invalid-in-gssp',
},
{
title: 'errors when used in getStaticProps',
path: '/pages-dir/123/invalid-in-gsp',
},
{
title: 'errors when used in within a page component',
path: '/pages-dir/invalid-in-page',
},
])('$title', async ({ path }) => {
const browser = await next.browser(path)
await assertHasRedbox(browser)
expect(await getRedboxSource(browser)).toMatch(
/You're importing a component that needs "?unstable_after"?\. That only works in a Server Component which is not supported in the pages\/ directory\./
)
expect(getLogs()).toHaveLength(0)
})
})
})
})