Skip to content

Commit a54b0d0

Browse files
committed
test: add integration and e2e test for requiring CJS .js module in serverless runtime
1 parent a118607 commit a54b0d0

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

tests/e2e/simple-app.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,14 @@ test('Compressed rewrites are readable', async ({ simple }) => {
252252
expect(resp.headers.get('content-encoding')).toEqual('br')
253253
expect(await resp.text()).toContain('<title>Example Domain</title>')
254254
})
255+
256+
test('can require CJS module that is not bundled', async ({ simple }) => {
257+
const resp = await fetch(`${simple.url}/api/cjs-file-with-js-extension`)
258+
259+
expect(resp.status).toBe(200)
260+
261+
const parsedBody = await resp.json()
262+
263+
expect(parsedBody.notBundledCJSModule.isBundled).toEqual(false)
264+
expect(parsedBody.bundledCJSModule.isBundled).toEqual(true)
265+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const { parse: pathParse } = require('node:path')
2+
3+
const fileBase = pathParse(__filename).base
4+
5+
module.exports = {
6+
fileBase,
7+
// if fileBase is not the same as this module name, it was bundled
8+
isBundled: fileBase !== 'bundled.cjs',
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { NextResponse } from 'next/server'
2+
import { resolve } from 'node:path'
3+
4+
export async function GET() {
5+
return NextResponse.json({
6+
notBundledCJSModule: __non_webpack_require__(resolve('./cjs-file-with-js-extension.js')),
7+
bundledCJSModule: require('./bundled.cjs'),
8+
})
9+
}
10+
11+
export const dynamic = 'force-dynamic'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const { parse: pathParse } = require('node:path')
2+
3+
const fileBase = pathParse(__filename).base
4+
5+
module.exports = {
6+
fileBase,
7+
// if fileBase is not the same as this module name, it was bundled
8+
isBundled: fileBase !== 'cjs-file-with-js-extension.js',
9+
}

tests/integration/simple-app.test.ts

+14
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,20 @@ test.skipIf(process.env.NEXT_VERSION !== 'canary')<FixtureTestContext>(
245245
},
246246
)
247247

248+
test<FixtureTestContext>('can require CJS module that is not bundled', async (ctx) => {
249+
await createFixture('simple', ctx)
250+
await runPlugin(ctx)
251+
252+
const response = await invokeFunction(ctx, { url: '/api/cjs-file-with-js-extension' })
253+
254+
expect(response.statusCode).toBe(200)
255+
256+
const parsedBody = JSON.parse(response.body)
257+
258+
expect(parsedBody.notBundledCJSModule.isBundled).toEqual(false)
259+
expect(parsedBody.bundledCJSModule.isBundled).toEqual(true)
260+
})
261+
248262
describe('next patching', async () => {
249263
const { cp: originalCp, appendFile } = (await vi.importActual(
250264
'node:fs/promises',

0 commit comments

Comments
 (0)