rsnext/test/unit/parse-page-static-info.test.ts
Jiachi Liu 084ad964a0
Fix static info parsing when export data fetching method as variable (#40317)
Fix the ssr/ssg detection when you export a nextjs data fetching method
as a variable instead of an async function.

- [x] Add case support in `checkExports`
- [x] Add unit tests for `getPageStaticInfo`
2022-09-07 09:28:15 -07:00

79 lines
2.6 KiB
TypeScript

import { getPageStaticInfo } from 'next/dist/build/analysis/get-page-static-info'
import { join } from 'path'
const fixtureDir = join(__dirname, 'fixtures')
function createNextConfig(runtime?: 'experimental-edge' | 'nodejs') {
return {
experimental: { runtime },
}
}
describe('parse page static info', () => {
it('should parse nodejs runtime correctly', async () => {
const { runtime, ssr, ssg } = await getPageStaticInfo({
pageFilePath: join(fixtureDir, 'page-runtime/nodejs-ssr.js'),
nextConfig: createNextConfig(),
})
expect(runtime).toBe('nodejs')
expect(ssr).toBe(true)
expect(ssg).toBe(false)
})
it('should parse static runtime correctly', async () => {
const { runtime, ssr, ssg } = await getPageStaticInfo({
pageFilePath: join(fixtureDir, 'page-runtime/nodejs.js'),
nextConfig: createNextConfig(),
})
expect(runtime).toBe(undefined)
expect(ssr).toBe(false)
expect(ssg).toBe(false)
})
it('should parse edge runtime correctly', async () => {
const { runtime } = await getPageStaticInfo({
pageFilePath: join(fixtureDir, 'page-runtime/edge.js'),
nextConfig: createNextConfig(),
})
expect(runtime).toBe('experimental-edge')
})
it('should return undefined if no runtime is specified', async () => {
const { runtime } = await getPageStaticInfo({
pageFilePath: join(fixtureDir, 'page-runtime/static.js'),
nextConfig: createNextConfig(),
})
expect(runtime).toBe(undefined)
})
it('should parse ssr info with variable exported gSSP correctly', async () => {
const { ssr, ssg } = await getPageStaticInfo({
pageFilePath: join(fixtureDir, 'page-runtime/ssr-variable-gssp.js'),
nextConfig: createNextConfig(),
})
expect(ssr).toBe(true)
expect(ssg).toBe(false)
})
})
describe('fallback to the global runtime configuration', () => {
it('should fallback when gSP is defined and exported', async () => {
const { runtime, ssr, ssg } = await getPageStaticInfo({
pageFilePath: join(fixtureDir, 'page-runtime/fallback-with-gsp.js'),
nextConfig: createNextConfig('experimental-edge'),
})
expect(runtime).toBe('experimental-edge')
expect(ssr).toBe(false)
expect(ssg).toBe(true)
})
it('should fallback when gSP is re-exported from other module', async () => {
const { runtime, ssr, ssg } = await getPageStaticInfo({
pageFilePath: join(fixtureDir, 'page-runtime/fallback-re-export-gsp.js'),
nextConfig: createNextConfig('experimental-edge'),
})
expect(runtime).toBe('experimental-edge')
expect(ssr).toBe(false)
expect(ssg).toBe(true)
})
})