first commit
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

This commit is contained in:
Arian Tron
2026-03-10 19:37:31 +03:30
commit 61f56f997c
27684 changed files with 2784175 additions and 0 deletions

View File

@@ -0,0 +1 @@
// populated with tests

View File

@@ -0,0 +1,4 @@
// populated with tests
export default () => {}
export const config = { matcher: '/' }

View File

@@ -0,0 +1,5 @@
export default async function handler(request) {
return Response.json({ ok: true })
}
export const config = { runtime: 'edge' }

View File

@@ -0,0 +1,3 @@
export default function Page() {
return <div>ok</div>
}

View File

@@ -0,0 +1,417 @@
/* eslint-env jest */
import { remove } from 'fs-extra'
import { join } from 'path'
import {
check,
fetchViaHTTP,
findPort,
killApp,
launchApp,
nextBuild,
nextStart,
retry,
} from 'next-test-utils'
import {
appOption,
context,
expectModuleNotFoundDevError,
expectModuleNotFoundProdError,
expectNoError,
expectUnsupportedModuleDevError,
expectUnsupportedModuleProdError,
getUnsupportedModuleWarning,
} from './utils'
jest.setTimeout(1000 * 60 * 2)
const routeUrl = '/api/route'
const middlewareUrl = '/'
describe('Edge runtime code with imports', () => {
beforeEach(async () => {
context.appPort = await findPort()
context.logs = { output: '', stdout: '', stderr: '' }
await remove(join(__dirname, '../.next'))
})
afterEach(async () => {
if (context.app) {
await killApp(context.app)
}
context.api.restore()
context.middleware.restore()
context.lib.restore()
context.page.restore()
})
describe.each([
{
title: 'Edge API',
url: routeUrl,
init(importStatement) {
context.api.write(`
import { NextResponse } from 'next/server'
export default async function handler(request) {
const { writeFile } = ${importStatement}
return Response.json({ ok: writeFile() })
}
export const config = { runtime: 'edge' }
`)
},
},
{
title: 'Middleware',
url: middlewareUrl,
init(importStatement) {
context.middleware.write(`
import { NextResponse } from 'next/server'
export async function middleware(request) {
const { writeFile } = ${importStatement}
return NextResponse.next()
}
`)
},
},
])('$title dynamically importing node.js module', ({ init, url }) => {
const moduleName = 'fs'
const importStatement = `await import("${moduleName}")`
beforeEach(() => init(importStatement))
;(process.env.TURBOPACK_BUILD ? describe.skip : describe)(
'development mode',
() => {
it('throws unsupported module error in dev at runtime and highlights the faulty line', async () => {
context.app = await launchApp(
context.appDir,
context.appPort,
appOption
)
await retry(async () => {
const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(500)
expectUnsupportedModuleDevError(
moduleName,
importStatement,
await res.text()
)
})
})
}
)
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
it('throws unsupported module error in production at runtime and prints error on logs', async () => {
const { stderr } = await nextBuild(context.appDir, undefined, {
stderr: true,
})
expect(stderr).toContain(getUnsupportedModuleWarning(moduleName))
// TODO: should this be failing build or not in turbopack
if (!process.env.IS_TURBOPACK_TEST) {
context.app = await nextStart(
context.appDir,
context.appPort,
appOption
)
const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(500)
expectUnsupportedModuleProdError(moduleName)
}
})
}
)
})
describe.each([
{
title: 'Edge API',
url: routeUrl,
init(importStatement) {
context.api.write(`
import throwAsync from '../../lib'
export default async function handler(request) {
return Response.json({ ok: await throwAsync() })
}
export const config = { runtime: 'edge' }
`)
},
},
{
title: 'Middleware',
url: middlewareUrl,
init(importStatement) {
context.middleware.write(`
import { NextResponse } from 'next/server'
import throwAsync from './lib'
export async function middleware(request) {
await throwAsync()
return NextResponse.next()
}
`)
},
},
])(
'$title dynamically importing node.js module in a lib',
({ init, url }) => {
const moduleName = 'os'
const importStatement = `await import("${moduleName}")`
beforeEach(() => {
init(importStatement)
context.lib.write(`
export default async function throwAsync() {
(${importStatement}).cwd()
}
`)
})
;(process.env.TURBOPACK_BUILD ? describe.skip : describe)(
'development mode',
() => {
it('throws unsupported module error in dev at runtime and highlights the faulty line', async () => {
context.app = await launchApp(
context.appDir,
context.appPort,
appOption
)
const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(500)
await check(async () => {
expectUnsupportedModuleDevError(
moduleName,
importStatement,
await res.text()
)
return 'success'
}, 'success')
})
}
)
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
it('throws unsupported module error in production at runtime and prints error on logs', async () => {
const { stderr } = await nextBuild(context.appDir, undefined, {
stderr: true,
})
expect(stderr).toContain(getUnsupportedModuleWarning(moduleName))
context.app = await nextStart(
context.appDir,
context.appPort,
appOption
)
const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(500)
expectUnsupportedModuleProdError(moduleName)
})
}
)
}
)
describe.each([
{
title: 'Edge API',
url: routeUrl,
init(importStatement) {
context.api.write(`
${importStatement}
export default async function handler(request) {
new Unknown()
return Response.json({ ok: true })
}
export const config = { runtime: 'edge' }
`)
},
},
{
title: 'Middleware',
url: middlewareUrl,
init(importStatement) {
context.middleware.write(`
import { NextResponse } from 'next/server'
${importStatement}
export async function middleware(request) {
new Unknown()
return NextResponse.next()
}
`)
},
},
])('$title statically importing 3rd party module', ({ init, url }) => {
const moduleName = 'not-exist'
const importStatement = `import Unknown from "${moduleName}"`
beforeEach(() => init(importStatement))
it('throws not-found module error in dev at runtime and highlights the faulty line', async () => {
context.app = await launchApp(context.appDir, context.appPort, appOption)
const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(500)
const text = await res.text()
await check(async () => {
expectModuleNotFoundDevError(moduleName, importStatement, text)
return 'success'
}, 'success')
})
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
it('does not build and reports', async () => {
const { code, stderr } = await nextBuild(context.appDir, undefined, {
ignoreFail: true,
stdout: true,
stderr: true,
})
expect(code).toEqual(1)
expectModuleNotFoundProdError(moduleName, stderr)
})
}
)
})
describe.each([
{
title: 'Edge API',
url: routeUrl,
init(importStatement) {
context.api.write(`
${importStatement}
export default async function handler(request) {
const response = Response.json({ ok: true })
response.headers.set('x-from-runtime', nanoid())
return response
}
export const config = { runtime: 'edge' }
`)
},
},
{
title: 'Middleware',
url: middlewareUrl,
init(importStatement) {
context.middleware.write(`
import { NextResponse } from 'next/server'
${importStatement}
export async function middleware(request) {
const response = NextResponse.next()
response.headers.set('x-from-runtime', nanoid())
return response
}
`)
},
},
])('$title importing vanilla 3rd party module', ({ init, url }) => {
const moduleName = 'nanoid'
const importStatement = `import { nanoid } from "${moduleName}"`
beforeEach(() => init(importStatement))
it('does not throw in dev at runtime', async () => {
context.app = await launchApp(context.appDir, context.appPort, appOption)
const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(200)
expect(res.headers.get('x-from-runtime')).toBeDefined()
expectNoError(moduleName)
})
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
it('does not throw in production at runtime', async () => {
const { stderr } = await nextBuild(context.appDir, undefined, {
stderr: true,
})
expect(stderr).not.toContain(getUnsupportedModuleWarning(moduleName))
context.app = await nextStart(
context.appDir,
context.appPort,
appOption
)
const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(200)
expect(res.headers.get('x-from-runtime')).toBeDefined()
expectNoError(moduleName)
})
}
)
})
describe.each([
{
title: 'Edge API',
url: routeUrl,
init(importStatement) {
context.api.write(`
${importStatement}
export default async function handler(request) {
const response = Response.json({ ok: true })
response.headers.set('x-from-runtime', Buffer.isBuffer('a string'))
return response
}
export const config = { runtime: 'edge' }
`)
},
},
{
title: 'Middleware',
url: middlewareUrl,
init(importStatement) {
context.middleware.write(`
import { NextResponse } from 'next/server'
${importStatement}
export async function middleware(request) {
const response = NextResponse.next()
response.headers.set('x-from-runtime', Buffer.isBuffer('a string'))
return response
}
`)
},
},
])('$title using Buffer polyfill', ({ init, url }) => {
const moduleName = 'buffer'
const importStatement = `import { Buffer } from "${moduleName}"`
beforeEach(() => init(importStatement))
it('does not throw in dev at runtime', async () => {
context.app = await launchApp(context.appDir, context.appPort, appOption)
const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(200)
expect(res.headers.get('x-from-runtime')).toBe('false')
expectNoError(moduleName)
})
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
it('does not throw in production at runtime', async () => {
await nextBuild(context.appDir, undefined, { stderr: true })
context.app = await nextStart(
context.appDir,
context.appPort,
appOption
)
const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(200)
expect(res.headers.get('x-from-runtime')).toBe('false')
expectNoError(moduleName)
})
}
)
})
})

View File

@@ -0,0 +1,324 @@
/* eslint-env jest */
import { remove } from 'fs-extra'
import { join } from 'path'
import {
fetchViaHTTP,
findPort,
killApp,
launchApp,
nextBuild,
nextStart,
retry,
} from 'next-test-utils'
import {
context,
appOption,
expectModuleNotFoundDevError,
expectModuleNotFoundProdError,
expectNoError,
expectUnsupportedModuleDevError,
expectUnsupportedModuleProdError,
getUnsupportedModuleWarning,
getModuleNotFound,
} from './utils'
jest.setTimeout(1000 * 60 * 2)
const routeUrl = '/api/route'
const middlewareUrl = '/'
describe('Edge runtime code with imports', () => {
beforeEach(async () => {
context.appPort = await findPort()
context.logs = { output: '', stdout: '', stderr: '' }
await remove(join(__dirname, '../.next'))
})
afterEach(async () => {
if (context.app) {
await killApp(context.app)
}
context.api.restore()
context.middleware.restore()
context.lib.restore()
context.page.restore()
})
describe.each([
{
title: 'Edge API',
url: routeUrl,
init(importStatement) {
context.api.write(`
${importStatement}
export default async function handler(request) {
basename()
return Response.json({ ok: basename() })
}
export const config = { runtime: 'edge' }
`)
},
},
{
title: 'Middleware',
url: middlewareUrl,
init(importStatement) {
context.middleware.write(`
import { NextResponse } from 'next/server'
${importStatement}
export async function middleware(request) {
basename()
return NextResponse.next()
}
`)
},
},
])('$title statically importing node.js module', ({ init, url }) => {
const moduleName = 'fs'
const importStatement = `import { basename } from "${moduleName}"`
beforeEach(() => init(importStatement))
it('throws unsupported module error in dev at runtime and highlights the faulty line', async () => {
context.app = await launchApp(context.appDir, context.appPort, appOption)
await retry(async () => {
const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(500)
const text = await res.text()
expectUnsupportedModuleDevError(moduleName, importStatement, text)
})
})
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
it('throws unsupported module error in production at runtime and prints error on logs', async () => {
const { stderr } = await nextBuild(context.appDir, undefined, {
stderr: true,
})
expect(stderr).toContain(getUnsupportedModuleWarning(moduleName))
context.app = await nextStart(
context.appDir,
context.appPort,
appOption
)
const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(500)
expectUnsupportedModuleProdError(moduleName)
})
}
)
})
describe.each([
{
title: 'Edge API',
url: routeUrl,
init(importStatement) {
context.api.write(`
export default async function handler(request) {
new (${importStatement})()
return Response.json({ ok: true })
}
export const config = { runtime: 'edge' }
`)
},
},
{
title: 'Middleware',
url: middlewareUrl,
init(importStatement) {
context.middleware.write(`
import { NextResponse } from 'next/server'
export async function middleware(request) {
new (${importStatement})()
return NextResponse.next()
}
`)
},
},
])('$title dynamically importing 3rd party module', ({ init, url }) => {
const moduleName = 'not-exist'
const importStatement = `await import("${moduleName}")`
beforeEach(() => init(importStatement))
it('throws not-found module error in dev at runtime and highlights the faulty line', async () => {
context.app = await launchApp(context.appDir, context.appPort, appOption)
await retry(async () => {
const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(500)
const text = await res.text()
expectModuleNotFoundDevError(moduleName, importStatement, text)
})
})
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
it('does not build and reports module not found error', async () => {
const { code, stderr } = await nextBuild(context.appDir, undefined, {
ignoreFail: true,
stdout: true,
stderr: true,
})
expect(code).toEqual(1)
expectModuleNotFoundProdError(moduleName, stderr)
})
}
)
})
describe.each([
{
title: 'Edge API',
url: routeUrl,
init(importStatement) {
context.api.write(`
export default async function handler(request) {
if (process.env === 'production') {
new (${importStatement})()
}
return Response.json({ ok: true })
}
export const config = { runtime: 'edge' }
`)
},
},
{
title: 'Middleware',
url: middlewareUrl,
init(importStatement) {
context.middleware.write(`
import { NextResponse } from 'next/server'
export async function middleware(request) {
if (process.env === 'production') {
new (${importStatement})()
}
return NextResponse.next()
}
`)
},
},
])('$title importing unused 3rd party module', ({ init, url }) => {
const moduleName = 'not-exist'
const importStatement = `await import("${moduleName}")`
beforeEach(() => init(importStatement))
it('throws not-found module error in dev at runtime and highlights the faulty line', async () => {
context.app = await launchApp(context.appDir, context.appPort, appOption)
await retry(async () => {
const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(500)
const text = await res.text()
expectModuleNotFoundDevError(moduleName, importStatement, text)
})
})
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
it('does not build and reports module not found error', async () => {
const { code, stderr } = await nextBuild(context.appDir, undefined, {
ignoreFail: true,
stdout: true,
stderr: true,
})
expect(code).toEqual(1)
expectModuleNotFoundProdError(moduleName, stderr)
})
}
)
})
describe.each([
{
title: 'Edge API',
url: routeUrl,
init(importStatement) {
context.api.write(`
export default async function handler(request) {
if (process.env === 'production') {
(${importStatement}).spawn('ls', ['-lh', '/usr'])
}
return Response.json({ ok: true })
}
export const config = { runtime: 'edge' }
`)
},
},
{
title: 'Middleware',
url: middlewareUrl,
init(importStatement) {
context.middleware.write(`
import { NextResponse } from 'next/server'
export async function middleware(request) {
if (process.env === 'production') {
(${importStatement}).spawn('ls', ['-lh', '/usr'])
}
return NextResponse.next()
}
`)
},
},
])('$title importing unused node.js module', ({ init, url }) => {
const moduleName = 'child_process'
const importStatement = `await import("${moduleName}")`
beforeEach(() => init(importStatement))
it('does not throw in dev at runtime', async () => {
context.app = await launchApp(context.appDir, context.appPort, appOption)
await retry(async () => {
const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(200)
expectNoError(moduleName)
})
})
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
it('does not throw in production at runtime', async () => {
const { stderr } = await nextBuild(context.appDir, undefined, {
stderr: true,
})
expect(stderr).toContain(getUnsupportedModuleWarning(moduleName))
let logs = { stdout: '', stderr: '', output: '' }
const port = await findPort()
const options = {
onStdout(msg) {
logs.output += msg
logs.stdout += msg
},
onStderr(msg) {
logs.output += msg
logs.stderr += msg
},
}
await nextStart(context.appDir, port, options)
const res = await fetchViaHTTP(port, url)
expect(res.status).toBe(200)
expect(logs.output).not.toContain(
getUnsupportedModuleWarning(moduleName)
)
expect(logs.output).not.toContain(getModuleNotFound(moduleName))
})
}
)
})
})

View File

@@ -0,0 +1,101 @@
import { join } from 'path'
import stripAnsi from 'strip-ansi'
import { File } from 'next-test-utils'
export const context = {
appDir: join(__dirname, '../'),
logs: { output: '', stdout: '', stderr: '' },
api: new File(join(__dirname, '../pages/api/route.js')),
lib: new File(join(__dirname, '../lib.js')),
middleware: new File(join(__dirname, '../middleware.js')),
page: new File(join(__dirname, '../pages/index.js')),
}
export const appOption = {
onStdout(msg) {
context.logs.output += msg
context.logs.stdout += msg
},
onStderr(msg) {
context.logs.output += msg
context.logs.stderr += msg
},
}
export function getModuleNotFound(name) {
return `Module not found: Can't resolve '${name}'`
}
export function getUnsupportedModule(name) {
return `The edge runtime does not support Node.js '${name}' module`
}
export function getUnsupportedModuleWarning(name) {
return `A Node.js module is loaded ('${name}'`
}
export function escapeLF(s) {
return s.replace(/\n/g, '\\n')
}
export function expectUnsupportedModuleProdError(
moduleName,
output = context.logs.output
) {
const moduleNotSupportedMessage = getUnsupportedModule(moduleName)
expect(output).toContain(moduleNotSupportedMessage)
const moduleNotFoundMessage = getModuleNotFound(moduleName)
expect(output).not.toContain(moduleNotFoundMessage)
}
export function expectUnsupportedModuleDevError(
moduleName,
importStatement,
responseText,
output = context.logs.output
) {
expectUnsupportedModuleProdError(moduleName, output)
// Codeframe points to internal frame because this app is not isolated.
// TODO: Once this test runs in an isolated app, make sure the codeframe includes the import statement
// expect(stripAnsi(output)).toContain(importStatement)
const moduleNotSupportedMessage = getUnsupportedModule(moduleName)
expect(responseText).toContain(escapeLF(moduleNotSupportedMessage))
const moduleNotFoundMessage = getModuleNotFound(moduleName)
expect(responseText).not.toContain(escapeLF(moduleNotFoundMessage))
}
export function expectModuleNotFoundProdError(
moduleName,
output = context.logs.output
) {
const moduleNotSupportedMessage = getUnsupportedModule(moduleName)
expect(stripAnsi(output)).not.toContain(moduleNotSupportedMessage)
const moduleNotFoundMessages = [
expect.stringContaining(`Error: Cannot find module '${moduleName}'`),
expect.stringContaining(getModuleNotFound(moduleName)),
]
expect(moduleNotFoundMessages).toContainEqual(stripAnsi(output))
}
export function expectModuleNotFoundDevError(
moduleName,
importStatement,
responseText,
output = context.logs.output
) {
expectModuleNotFoundProdError(moduleName, output)
expect(stripAnsi(output)).toContain(importStatement)
const moduleNotSupportedMessage = getUnsupportedModule(moduleName)
expect(responseText).not.toContain(escapeLF(moduleNotSupportedMessage))
const moduleNotFoundMessage = getModuleNotFound(moduleName)
expect(responseText).toContain(escapeLF(moduleNotFoundMessage))
}
export function expectNoError(moduleName) {
expect(context.logs.output).not.toContain(getUnsupportedModule(moduleName))
expect(context.logs.output).not.toContain(getModuleNotFound(moduleName))
}