Skip to content

Commit

Permalink
Isolate test case
Browse files Browse the repository at this point in the history
because it's interferring with the other routes
  • Loading branch information
Janpot committed Feb 16, 2020
1 parent aa6e63d commit ad7059e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 33 deletions.
85 changes: 85 additions & 0 deletions test/integration/api-catch-all/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* eslint-env jest */
/* global jasmine */
import fs from 'fs-extra'
import { join } from 'path'
import {
killApp,
findPort,
launchApp,
fetchViaHTTP,
nextBuild,
nextStart,
} from 'next-test-utils'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const appDir = join(__dirname, '../')
const nextConfig = join(appDir, 'next.config.js')
let appPort
let app

function runTests() {
it('should return data when catch-all', async () => {
const data = await fetchViaHTTP(appPort, '/api/users/1', null, {}).then(
res => res.ok && res.json()
)

expect(data).toEqual({ slug: ['1'] })
})

it('should 404 when catch-all with index and trailing slash', async () => {
const data = await fetchViaHTTP(appPort, '/api/users/', null, {}).then(
res => res.status
)

expect(data).toEqual(404)
})

it('should return data when catch-all with index and no trailing slash', async () => {
const data = await fetchViaHTTP(appPort, '/api/users', null, {}).then(
res => res.ok && res.json()
)

expect(data).toEqual({})
})
}

describe('API routes', () => {
describe('dev support', () => {
beforeAll(async () => {
appPort = await findPort()
app = await launchApp(appDir, appPort)
})
afterAll(() => killApp(app))

runTests()
})

describe('Server support', () => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))

runTests()
})

describe('Serverless support', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfig,
`module.exports = { target: 'serverless' }`
)
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
await fs.remove(nextConfig)
})

runTests()
})
})
33 changes: 0 additions & 33 deletions test/integration/api-support/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,39 +322,6 @@ function runTests(dev = false) {
expect(data).toEqual({ post: 'post-1', id: '1' })
})

it('should return data when catch-all', async () => {
const data = await fetchViaHTTP(
appPort,
'/api/catch-all/users/1',
null,
{}
).then(res => res.ok && res.json())

expect(data).toEqual({ slug: ['1'] })
})

it('should 404 when catch-all with index and trailing slash', async () => {
const data = await fetchViaHTTP(
appPort,
'/api/catch-all/users/',
null,
{}
).then(res => res.status)

expect(data).toEqual(404)
})

it('should return data when catch-all with index and no trailing slash', async () => {
const data = await fetchViaHTTP(
appPort,
'/api/catch-all/users',
null,
{}
).then(res => res.ok && res.json())

expect(data).toEqual({})
})

if (dev) {
it('should compile only server code in development', async () => {
await fetchViaHTTP(appPort, '/')
Expand Down

0 comments on commit ad7059e

Please sign in to comment.