Files
next.js/test/e2e/app-dir/node-extensions/node-extensions.random.test.ts
Arian Tron 61f56f997c
Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
first commit
2026-03-10 19:37:31 +03:30

271 lines
8.7 KiB
TypeScript

import { nextTestSetup } from 'e2e-utils'
describe('Node Extensions', () => {
describe('Random', () => {
describe('Cache Components', () => {
const { next, skipped } = nextTestSetup({
files: __dirname + '/fixtures/random/cache-components',
skipDeployment: true,
})
if (skipped) {
return
}
it('should not error when accessing middlware that use Math.random()', async () => {
let res: Awaited<ReturnType<typeof next.fetch>>,
$: Awaited<ReturnType<typeof next.render$>>
res = await next.fetch('/rewrite')
expect(res.status).toBe(200)
$ = await next.render$('/rewrite')
expect($('[data-testid="content"]').text()).toBe('rewritten')
})
it('should not error when accessing pages that use Math.random() in App Router', async () => {
let res, $
res = await next.fetch('/app/prerendered/unstable-cache')
expect(res.status).toBe(200)
$ = await next.render$('/app/prerendered/unstable-cache')
expect($('li').length).toBe(2)
res = await next.fetch('/app/prerendered/use-cache')
expect(res.status).toBe(200)
$ = await next.render$('/app/prerendered/use-cache')
expect($('li').length).toBe(2)
res = await next.fetch('/app/rendered/uncached')
expect(res.status).toBe(200)
$ = await next.render$('/app/rendered/uncached')
expect($('li').length).toBe(2)
res = await next.fetch('/app/rendered/unstable-cache')
expect(res.status).toBe(200)
$ = await next.render$('/app/rendered/unstable-cache')
expect($('li').length).toBe(2)
res = await next.fetch('/app/rendered/use-cache')
expect(res.status).toBe(200)
$ = await next.render$('/app/rendered/use-cache')
expect($('li').length).toBe(2)
})
it('should not error when accessing routes that use Math.random() in App Router', async () => {
let res, body
res = await next.fetch('/app/prerendered/uncached/api')
expect(res.status).toBe(200)
body = await res.json()
expect(body).toEqual({
rand1: expect.any(Number),
rand2: expect.any(Number),
})
res = await next.fetch('/app/prerendered/unstable-cache/api')
expect(res.status).toBe(200)
body = await res.json()
expect(body).toEqual({
rand1: expect.any(Number),
rand2: expect.any(Number),
})
res = await next.fetch('/app/prerendered/use-cache/api')
expect(res.status).toBe(200)
body = await res.json()
expect(body).toEqual({
rand1: expect.any(Number),
rand2: expect.any(Number),
})
res = await next.fetch('/app/rendered/uncached/api')
expect(res.status).toBe(200)
body = await res.json()
expect(body).toEqual({
rand1: expect.any(Number),
rand2: expect.any(Number),
})
res = await next.fetch('/app/rendered/unstable-cache/api')
expect(res.status).toBe(200)
body = await res.json()
expect(body).toEqual({
rand1: expect.any(Number),
rand2: expect.any(Number),
})
res = await next.fetch('/app/rendered/use-cache/api')
expect(res.status).toBe(200)
body = await res.json()
expect(body).toEqual({
rand1: expect.any(Number),
rand2: expect.any(Number),
})
})
it('should not error when accessing pages that use Math.random() in Pages Router', async () => {
let res, $
res = await next.fetch('/pages/gip/random')
expect(res.status).toBe(200)
$ = await next.render$('/pages/gip/random')
expect($('li').length).toBe(2)
res = await next.fetch('/pages/gssp/random')
expect(res.status).toBe(200)
$ = await next.render$('/pages/gssp/random')
expect($('li').length).toBe(2)
res = await next.fetch('/pages/gsp/random')
expect(res.status).toBe(200)
$ = await next.render$('/pages/gsp/random')
expect($('li').length).toBe(2)
})
it('should not error when accessing routes that use Math.random() in Pages Router', async () => {
let res, body
res = await next.fetch('/api/random')
expect(res.status).toBe(200)
body = await res.json()
expect(body).toEqual({
rand1: expect.any(Number),
rand2: expect.any(Number),
})
expect(body.rand1).not.toBe(body.rand2)
const first1 = body.rand1
const first2 = body.rand2
res = await next.fetch('/api/random')
body = await res.json()
expect(body.rand1).not.toBe(body.rand2)
expect(body.rand1).not.toBe(first1)
expect(body.rand2).not.toBe(first2)
})
})
describe('Legacy', () => {
const { next, skipped } = nextTestSetup({
files: __dirname + '/fixtures/random/legacy',
})
if (skipped) {
return
}
it('should not error when accessing middlware that use Math.random()', async () => {
let res, $
res = await next.fetch('/rewrite')
expect(res.status).toBe(200)
$ = await next.render$('/rewrite')
expect($('[data-testid="content"]').text()).toBe('rewritten')
})
it('should not error when accessing pages that use Math.random() in App Router', async () => {
let res, $
res = await next.fetch('/app/prerendered/uncached')
expect(res.status).toBe(200)
$ = await next.render$('/app/prerendered/uncached')
expect($('li').length).toBe(2)
res = await next.fetch('/app/prerendered/unstable-cache')
expect(res.status).toBe(200)
$ = await next.render$('/app/prerendered/unstable-cache')
expect($('li').length).toBe(2)
res = await next.fetch('/app/rendered/uncached')
expect(res.status).toBe(200)
$ = await next.render$('/app/rendered/uncached')
expect($('li').length).toBe(2)
res = await next.fetch('/app/rendered/unstable-cache')
expect(res.status).toBe(200)
$ = await next.render$('/app/rendered/unstable-cache')
expect($('li').length).toBe(2)
})
it('should not error when accessing routes that use Math.random() in App Router', async () => {
let res, body
res = await next.fetch('/app/prerendered/uncached/api')
expect(res.status).toBe(200)
body = await res.json()
expect(body).toEqual({
rand1: expect.any(Number),
rand2: expect.any(Number),
})
res = await next.fetch('/app/prerendered/unstable-cache/api')
expect(res.status).toBe(200)
body = await res.json()
expect(body).toEqual({
rand1: expect.any(Number),
rand2: expect.any(Number),
})
res = await next.fetch('/app/rendered/uncached/api')
expect(res.status).toBe(200)
body = await res.json()
expect(body).toEqual({
rand1: expect.any(Number),
rand2: expect.any(Number),
})
res = await next.fetch('/app/rendered/unstable-cache/api')
expect(res.status).toBe(200)
body = await res.json()
expect(body).toEqual({
rand1: expect.any(Number),
rand2: expect.any(Number),
})
})
it('should not error when accessing pages that use Math.random() in Pages Router', async () => {
let res, $
res = await next.fetch('/pages/gip/random')
expect(res.status).toBe(200)
$ = await next.render$('/pages/gip/random')
expect($('li').length).toBe(2)
res = await next.fetch('/pages/gssp/random')
expect(res.status).toBe(200)
$ = await next.render$('/pages/gssp/random')
expect($('li').length).toBe(2)
res = await next.fetch('/pages/gsp/random')
expect(res.status).toBe(200)
$ = await next.render$('/pages/gsp/random')
expect($('li').length).toBe(2)
})
it('should not error when accessing routes that use Math.random() in Pages Router', async () => {
let res, body
res = await next.fetch('/api/random')
expect(res.status).toBe(200)
body = await res.json()
expect(body).toEqual({
rand1: expect.any(Number),
rand2: expect.any(Number),
})
expect(body.rand1).not.toBe(body.rand2)
const first1 = body.rand1
const first2 = body.rand2
res = await next.fetch('/api/random')
body = await res.json()
expect(body.rand1).not.toBe(body.rand2)
expect(body.rand1).not.toBe(first1)
expect(body.rand2).not.toBe(first2)
})
})
})
})