Unverified Commit 8a370d1f authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update api-support test suite (#9673)

parent 9b6ea9d4
module.exports = {
target: 'server',
}
/* eslint-env jest */ /* eslint-env jest */
/* global jasmine */ /* global jasmine */
import fs from 'fs-extra'
import { join } from 'path' import { join } from 'path'
import { existsSync, readFileSync } from 'fs'
import { import {
killApp, killApp,
findPort, findPort,
...@@ -10,17 +10,17 @@ import { ...@@ -10,17 +10,17 @@ import {
renderViaHTTP, renderViaHTTP,
nextBuild, nextBuild,
nextStart, nextStart,
File,
} from 'next-test-utils' } from 'next-test-utils'
import json from '../big.json' import json from '../big.json'
import { createServer } from 'http'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js')
let appPort let appPort
let server let mode
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2 let app
function runTests(serverless = false) { function runTests(dev = false) {
it('should render page', async () => { it('should render page', async () => {
const html = await renderViaHTTP(appPort, '/') const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/API - support/) expect(html).toMatch(/API - support/)
...@@ -37,33 +37,15 @@ function runTests(serverless = false) { ...@@ -37,33 +37,15 @@ function runTests(serverless = false) {
}) })
it('should not conflict with /api routes', async () => { it('should not conflict with /api routes', async () => {
const port = await findPort() const res = await fetchViaHTTP(appPort, '/api-conflict')
await nextBuild(appDir)
const app = await nextStart(appDir, port)
const res = await fetchViaHTTP(port, '/api-conflict')
expect(res.status).not.toEqual(404) expect(res.status).not.toEqual(404)
killApp(app)
}) })
it('should work with index api', async () => { it('should work with index api', async () => {
if (serverless) { const text = await fetchViaHTTP(appPort, '/api', null, {}).then(
const port = await findPort() res => res.ok && res.text()
const resolver = require(join(appDir, '.next/serverless/pages/api.js')) )
.default expect(text).toEqual('Index should work')
const server = createServer(resolver).listen(port)
const res = await fetchViaHTTP(port, '/api')
const text = await res.text()
server.close()
expect(text).toEqual('Index should work')
} else {
const text = await fetchViaHTTP(appPort, '/api', null, {}).then(
res => res.ok && res.text()
)
expect(text).toEqual('Index should work')
}
}) })
it('should return custom error', async () => { it('should return custom error', async () => {
...@@ -248,24 +230,12 @@ function runTests(serverless = false) { ...@@ -248,24 +230,12 @@ function runTests(serverless = false) {
expect(data).toEqual({ val: '1', post: 'post-1' }) expect(data).toEqual({ val: '1', post: 'post-1' })
}) })
if (serverless) { it('should work with dynamic params and search string like lambda', async () => {
it('should work with dynamic params and search string like lambda', async () => { const res = await fetchViaHTTP(appPort, '/api/post-1?val=1')
await nextBuild(appDir, []) const json = await res.json()
const port = await findPort()
const resolver = require(join(
appDir,
'.next/serverless/pages/api/[post].js'
)).default
const server = createServer(resolver).listen(port)
const res = await fetchViaHTTP(port, '/api/post-1?val=1')
const json = await res.json()
server.close()
expect(json).toEqual({ val: '1', post: 'post-1' }) expect(json).toEqual({ val: '1', post: 'post-1' })
}) })
}
it('should prioritize a non-dynamic page', async () => { it('should prioritize a non-dynamic page', async () => {
const data = await fetchViaHTTP( const data = await fetchViaHTTP(
...@@ -295,98 +265,109 @@ function runTests(serverless = false) { ...@@ -295,98 +265,109 @@ function runTests(serverless = false) {
expect(res.status).toBe(404) expect(res.status).toBe(404)
}) })
it('should build api routes', async () => { it('should return data on dynamic optional nested route', async () => {
await nextBuild(appDir, [], { stdout: true }) const data = await fetchViaHTTP(
if (serverless) { appPort,
'/api/blog/post-1/comment/1',
null,
{}
).then(res => res.ok && res.json())
expect(data).toEqual({ post: 'post-1', id: '1' })
})
if (dev) {
it('should compile only server code in development', async () => {
await fetchViaHTTP(appPort, '/')
await fetchViaHTTP(appPort, '/api/users')
// Normal page
expect(
await fs.exists(
join(appDir, `/.next/static/development/pages/index.js`)
)
).toBeTruthy()
expect(
await fs.exists(
join(appDir, `/.next/server/static/development/pages/index.js`)
)
).toBeTruthy()
// API page
expect(
await fs.exists(
join(appDir, `/.next/static/development/pages/api/users.js`)
)
).toBeFalsy()
expect(
await fs.exists(
join(appDir, `/.next/server/static/development/pages/api/users.js`)
)
).toBeTruthy()
})
} else {
it('should build api routes', async () => {
const pagesManifest = JSON.parse( const pagesManifest = JSON.parse(
readFileSync( await fs.readFile(
join(appDir, '.next/serverless/pages-manifest.json'), join(appDir, `.next/${mode}/pages-manifest.json`),
'utf8' 'utf8'
) )
) )
expect(Object.keys(pagesManifest).includes('/api/[post]')).toBeTruthy() expect(Object.keys(pagesManifest).includes('/api/[post]')).toBeTruthy()
const port = await findPort() const res = await fetchViaHTTP(appPort, '/api/nextjs')
const resolver = require(join(
appDir,
'.next/serverless/pages/api/blog.js'
)).default
const server = createServer(resolver).listen(port)
const res = await fetchViaHTTP(port, '/api/nextjs')
const json = await res.json() const json = await res.json()
server.close()
expect(json).toEqual([{ title: 'Cool Post!' }]) expect(json).toEqual({ post: 'nextjs' })
} else {
expect(
existsSync(join(appDir, '.next/server/pages-manifest.json'), 'utf8')
).toBeTruthy()
const buildManifest = JSON.parse( const buildManifest = JSON.parse(
readFileSync(join(appDir, '.next/build-manifest.json'), 'utf8') await fs.readFile(join(appDir, '.next/build-manifest.json'), 'utf8')
) )
expect( expect(
Object.keys(buildManifest.pages).includes('/api-conflict') Object.keys(buildManifest.pages).includes('/api-conflict')
).toBeTruthy() ).toBeTruthy()
} })
}) }
it('should return data on dynamic optional nested route', async () => {
const data = await fetchViaHTTP(
appPort,
'/api/blog/post-1/comment/1',
null,
{}
).then(res => res.ok && res.json())
expect(data).toEqual({ post: 'post-1', id: '1' })
})
it('should compile only server code in development', async () => {
await fetchViaHTTP(appPort, '/')
await fetchViaHTTP(appPort, '/api/users')
// Normal page
expect(
existsSync(join(appDir, `/.next/static/development/pages/index.js`))
).toBeTruthy()
expect(
existsSync(
join(appDir, `/.next/server/static/development/pages/index.js`)
)
).toBeTruthy()
// API page
expect(
existsSync(join(appDir, `/.next/static/development/pages/api/users.js`))
).toBeFalsy()
expect(
existsSync(
join(appDir, `/.next/server/static/development/pages/api/users.js`)
)
).toBeTruthy()
})
} }
describe('API routes', () => { describe('API routes', () => {
beforeAll(async () => { describe('dev support', () => {
appPort = await findPort() beforeAll(async () => {
server = await launchApp(appDir, appPort) appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))
runTests(true)
}) })
afterAll(() => killApp(server))
describe('Server support', () => { describe('Server support', () => {
beforeAll(async () => {
await nextBuild(appDir)
mode = 'server'
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))
runTests() runTests()
}) })
describe('Serverless support', () => { describe('Serverless support', () => {
const nextConfig = new File(join(appDir, 'next.config.js')) beforeAll(async () => {
beforeEach(() => { await fs.writeFile(
nextConfig.replace('server', 'serverless') nextConfig,
`module.exports = { target: 'serverless' }`
)
await nextBuild(appDir)
mode = 'serverless'
appPort = await findPort()
app = await nextStart(appDir, appPort)
}) })
afterEach(() => { afterAll(async () => {
nextConfig.restore() await killApp(app)
await fs.remove(nextConfig)
}) })
runTests(true)
runTests()
}) })
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment