Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
8a370d1f
Unverified
Commit
8a370d1f
authored
Dec 09, 2019
by
JJ Kasper
Committed by
GitHub
Dec 09, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update api-support test suite (#9673)
parent
9b6ea9d4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
94 additions
and
116 deletions
+94
-116
next.config.js
test/integration/api-support/next.config.js
+0
-3
index.test.js
test/integration/api-support/test/index.test.js
+94
-113
No files found.
test/integration/api-support/next.config.js
deleted
100644 → 0
View file @
9b6ea9d4
module
.
exports
=
{
target
:
'server'
,
}
test/integration/api-support/test/index.test.js
View file @
8a370d1f
/* eslint-env jest */
/* global jasmine */
import
fs
from
'fs-extra'
import
{
join
}
from
'path'
import
{
existsSync
,
readFileSync
}
from
'fs'
import
{
killApp
,
findPort
,
...
...
@@ -10,17 +10,17 @@ import {
renderViaHTTP
,
nextBuild
,
nextStart
,
File
,
}
from
'next-test-utils'
import
json
from
'../big.json'
import
{
createServer
}
from
'http'
jasmine
.
DEFAULT_TIMEOUT_INTERVAL
=
1000
*
60
*
2
const
appDir
=
join
(
__dirname
,
'../'
)
const
nextConfig
=
join
(
appDir
,
'next.config.js'
)
let
appPort
let
server
jasmine
.
DEFAULT_TIMEOUT_INTERVAL
=
1000
*
60
*
2
let
mode
let
app
function
runTests
(
serverless
=
false
)
{
function
runTests
(
dev
=
false
)
{
it
(
'should render page'
,
async
()
=>
{
const
html
=
await
renderViaHTTP
(
appPort
,
'/'
)
expect
(
html
).
toMatch
(
/API - support/
)
...
...
@@ -37,33 +37,15 @@ function runTests(serverless = false) {
})
it
(
'should not conflict with /api routes'
,
async
()
=>
{
const
port
=
await
findPort
()
await
nextBuild
(
appDir
)
const
app
=
await
nextStart
(
appDir
,
port
)
const
res
=
await
fetchViaHTTP
(
port
,
'/api-conflict'
)
const
res
=
await
fetchViaHTTP
(
appPort
,
'/api-conflict'
)
expect
(
res
.
status
).
not
.
toEqual
(
404
)
killApp
(
app
)
})
it
(
'should work with index api'
,
async
()
=>
{
if
(
serverless
)
{
const
port
=
await
findPort
()
const
resolver
=
require
(
join
(
appDir
,
'.next/serverless/pages/api.js'
))
.
default
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'
)
}
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
()
=>
{
...
...
@@ -248,24 +230,12 @@ function runTests(serverless = false) {
expect
(
data
).
toEqual
({
val
:
'1'
,
post
:
'post-1'
})
})
if
(
serverless
)
{
it
(
'should work with dynamic params and search string like lambda'
,
async
()
=>
{
await
nextBuild
(
appDir
,
[])
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
()
it
(
'should work with dynamic params and search string like lambda'
,
async
()
=>
{
const
res
=
await
fetchViaHTTP
(
appPort
,
'/api/post-1?val=1'
)
const
json
=
await
res
.
json
()
expect
(
json
).
toEqual
({
val
:
'1'
,
post
:
'post-1'
})
})
}
expect
(
json
).
toEqual
({
val
:
'1'
,
post
:
'post-1'
})
})
it
(
'should prioritize a non-dynamic page'
,
async
()
=>
{
const
data
=
await
fetchViaHTTP
(
...
...
@@ -295,98 +265,109 @@ function runTests(serverless = false) {
expect
(
res
.
status
).
toBe
(
404
)
})
it
(
'should build api routes'
,
async
()
=>
{
await
nextBuild
(
appDir
,
[],
{
stdout
:
true
})
if
(
serverless
)
{
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'
})
})
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
(
readFileSync
(
join
(
appDir
,
'.next/serverless/pages-manifest.json'
),
await
fs
.
readFile
(
join
(
appDir
,
`.next/
${
mode
}
/pages-manifest.json`
),
'utf8'
)
)
expect
(
Object
.
keys
(
pagesManifest
).
includes
(
'/api/[post]'
)).
toBeTruthy
()
const
port
=
await
findPort
()
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
res
=
await
fetchViaHTTP
(
appPort
,
'/api/nextjs'
)
const
json
=
await
res
.
json
()
server
.
close
()
expect
(
json
).
toEqual
([{
title
:
'Cool Post!'
}])
}
else
{
expect
(
existsSync
(
join
(
appDir
,
'.next/server/pages-manifest.json'
),
'utf8'
)
).
toBeTruthy
()
expect
(
json
).
toEqual
({
post
:
'nextjs'
})
const
buildManifest
=
JSON
.
parse
(
readFileSync
(
join
(
appDir
,
'.next/build-manifest.json'
),
'utf8'
)
await
fs
.
readFile
(
join
(
appDir
,
'.next/build-manifest.json'
),
'utf8'
)
)
expect
(
Object
.
keys
(
buildManifest
.
pages
).
includes
(
'/api-conflict'
)
).
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'
,
()
=>
{
beforeAll
(
async
()
=>
{
appPort
=
await
findPort
()
server
=
await
launchApp
(
appDir
,
appPort
)
describe
(
'dev support'
,
()
=>
{
beforeAll
(
async
()
=>
{
appPort
=
await
findPort
()
app
=
await
launchApp
(
appDir
,
appPort
)
})
afterAll
(()
=>
killApp
(
app
))
runTests
(
true
)
})
afterAll
(()
=>
killApp
(
server
))
describe
(
'Server support'
,
()
=>
{
beforeAll
(
async
()
=>
{
await
nextBuild
(
appDir
)
mode
=
'server'
appPort
=
await
findPort
()
app
=
await
nextStart
(
appDir
,
appPort
)
})
afterAll
(()
=>
killApp
(
app
))
runTests
()
})
describe
(
'Serverless support'
,
()
=>
{
const
nextConfig
=
new
File
(
join
(
appDir
,
'next.config.js'
))
beforeEach
(()
=>
{
nextConfig
.
replace
(
'server'
,
'serverless'
)
beforeAll
(
async
()
=>
{
await
fs
.
writeFile
(
nextConfig
,
`module.exports = { target: 'serverless' }`
)
await
nextBuild
(
appDir
)
mode
=
'serverless'
appPort
=
await
findPort
()
app
=
await
nextStart
(
appDir
,
appPort
)
})
afterEach
(()
=>
{
nextConfig
.
restore
()
afterAll
(
async
()
=>
{
await
killApp
(
app
)
await
fs
.
remove
(
nextConfig
)
})
runTests
(
true
)
runTests
()
})
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment