Skip to content

Commit

Permalink
Fix issues in test with conflicting ports
Browse files Browse the repository at this point in the history
  • Loading branch information
bas080 committed May 7, 2023
1 parent 154f086 commit fb451da
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
14 changes: 10 additions & 4 deletions cli.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import { spawn } from 'child_process'

const bin = './cli.mjs'

const PORT = process.env.PORT || 3000

test('run server with default port', (t) => {
const child = spawn(bin)

child.on('error', (err) => t.fail(err))
child.stderr.on('data', (data) => {
t.ok(data.toString().includes('Listening on port 3000'))
t.end()
if (data.toString().includes(`Listening on port ${PORT}`)) {
t.pass()
t.end()
}
})

setTimeout(() => {
Expand All @@ -22,8 +26,10 @@ test('run server with custom port', (t) => {

child.on('error', (err) => t.fail(err))
child.stderr.on('data', (data) => {
t.ok(data.toString().includes('Listening on port 4000'))
t.end()
if (data.toString().includes('Listening on port 4000')) {
t.pass()
t.end()
}
})

setTimeout(() => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"scripts": {
"start": "./cli.mjs",
"dev": "npx nodemon ./cli.mjs",
"test": "PORT=4010 DEBUG=furver tap *.test.mjs --no-cov",
"test": "tap *.test.mjs --no-cov",
"version": "markatzea CONTRIBUTING.mz > CONTRIBUTING.md && git add *.md",
"docs": "markatzea CONTRIBUTING.mz | tee CONTRIBUTING.md"
},
Expand Down
5 changes: 3 additions & 2 deletions server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ export default async function serve (api) {
request.on('end', async () => {
try {
debug('Parse')
const data = Buffer.concat(chunks)
const data = Buffer.concat(chunks).toString()
const parsedData = tryCatch(
() => JSON.parse(data.toString()),
() => JSON.parse(data),
error => {
debug(error)
debug(data)
error.status = 400
throw error
})
Expand Down
12 changes: 6 additions & 6 deletions server.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ const { test } = tap

let serverProcess

const port = Number(process.env.PORT || 3000) + 1
const apiUri = `http://localhost:${port}`

tap.before(async () => {
// Start the server in a child process
serverProcess = spawn('node', ['./cli.mjs', './example/api.mjs'])
serverProcess = spawn('node', ['./cli.mjs', './example/api.mjs', '--port', port])

// Wait for the server to start listening on the port
await new Promise((resolve) => serverProcess.stderr.once('data', resolve))
})

tap.teardown(() => serverProcess.kill())

const port = process.env.PORT
const apiUri = `http://localhost:${port}`

test('Returns 400 when receiving malformed JSON data', async (t) => {
// Send a POST request with malformed JSON data
const res = await fetch(apiUri, {
Expand All @@ -28,8 +28,7 @@ test('Returns 400 when receiving malformed JSON data', async (t) => {

// Verify the response status code
t.equal(res.status, 400)

// Stop the server
t.end()
})

test('Returns 500 status', async (t) => {
Expand Down Expand Up @@ -63,4 +62,5 @@ test('Test 200 status', async (t) => {
// Verify the response body
const responseBody = await res.json()
t.same(responseBody, expectedResponse)
t.end()
})

0 comments on commit fb451da

Please sign in to comment.