Skip to content

Commit

Permalink
test(serving-functions): add CLI output on failure (#3015)
Browse files Browse the repository at this point in the history
  • Loading branch information
erezrokah authored Jul 27, 2021
1 parent 3b49f19 commit f3d433d
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 101 deletions.
20 changes: 12 additions & 8 deletions tests/serving-functions-go.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const test = require('ava')
const pWaitFor = require('p-wait-for')

const { withDevServer } = require('./utils/dev-server')
const { withDevServer, tryAndLogOutput } = require('./utils/dev-server')
const got = require('./utils/got')
const { pause } = require('./utils/pause')
const { withSiteBuilder } = require('./utils/site-builder')
Expand Down Expand Up @@ -85,7 +85,7 @@ require github.com/aws/aws-lambda-go v1.20.0`,

await withDevServer(
{ cwd: builder.directory, env: { NETLIFY_EXPERIMENTAL_BUILD_GO_SOURCE: 'true' } },
async ({ port }) => {
async ({ port, outputBuffer }) => {
t.is(await got(`http://localhost:${port}/.netlify/functions/go-func`).text(), 'Hello, world!')

await pause(WAIT_WRITE)
Expand All @@ -94,13 +94,17 @@ require github.com/aws/aws-lambda-go v1.20.0`,
.withContentFile({ path: 'functions/go-func/main.go', content: goSource.replace('world', 'Netlify') })
.buildAsync()

await pWaitFor(
async () => {
const response = await got(`http://localhost:${port}/.netlify/functions/go-func`).text()
await tryAndLogOutput(
() =>
pWaitFor(
async () => {
const response = await got(`http://localhost:${port}/.netlify/functions/go-func`).text()

return response === 'Hello, Netlify!'
},
{ interval: WAIT_INTERVAL, timeout: WAIT_TIMEOUT },
return response === 'Hello, Netlify!'
},
{ interval: WAIT_INTERVAL, timeout: WAIT_TIMEOUT },
),
outputBuffer,
)
},
)
Expand Down
214 changes: 125 additions & 89 deletions tests/serving-functions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { join } = require('path')
const test = require('ava')
const pWaitFor = require('p-wait-for')

const { withDevServer } = require('./utils/dev-server')
const { withDevServer, tryAndLogOutput } = require('./utils/dev-server')
const got = require('./utils/got')
const { pause } = require('./utils/pause')
const { withSiteBuilder } = require('./utils/site-builder')
Expand Down Expand Up @@ -49,7 +49,7 @@ testMatrix.forEach(({ args }) => {
})
.buildAsync()

await withDevServer({ cwd: builder.directory, args }, async ({ port }) => {
await withDevServer({ cwd: builder.directory, args }, async ({ port, outputBuffer }) => {
t.is(await got(`http://localhost:${port}/.netlify/functions/hello`).text(), 'Hello')

await pause(WAIT_WRITE)
Expand All @@ -64,13 +64,17 @@ testMatrix.forEach(({ args }) => {
})
.buildAsync()

await pWaitFor(
async () => {
const response = await got(`http://localhost:${port}/.netlify/functions/hello`).text()

return response === 'Goodbye'
},
{ interval: WAIT_INTERVAL, timeout: WAIT_TIMEOUT },
await tryAndLogOutput(
() =>
pWaitFor(
async () => {
const response = await got(`http://localhost:${port}/.netlify/functions/hello`).text()

return response === 'Goodbye'
},
{ interval: WAIT_INTERVAL, timeout: WAIT_TIMEOUT },
),
outputBuffer,
)
})
})
Expand Down Expand Up @@ -113,7 +117,7 @@ testMatrix.forEach(({ args }) => {
})
.buildAsync()

await withDevServer({ cwd: builder.directory, args }, async ({ port }) => {
await withDevServer({ cwd: builder.directory, args }, async ({ port, outputBuffer }) => {
t.is(
await got(`http://localhost:${port}/.netlify/functions/hello`).text(),
'Modern Web Development on the JAMStack',
Expand Down Expand Up @@ -147,13 +151,17 @@ testMatrix.forEach(({ args }) => {
})
.buildAsync()

await pWaitFor(
async () => {
const response = await got(`http://localhost:${port}/.netlify/functions/hello`).text()

return response === 'Modern Web Development on the Jamstack'
},
{ interval: WAIT_INTERVAL, timeout: WAIT_TIMEOUT },
await tryAndLogOutput(
() =>
pWaitFor(
async () => {
const response = await got(`http://localhost:${port}/.netlify/functions/hello`).text()

return response === 'Modern Web Development on the Jamstack'
},
{ interval: WAIT_INTERVAL, timeout: WAIT_TIMEOUT },
),
outputBuffer,
)
})
})
Expand All @@ -179,7 +187,7 @@ testMatrix.forEach(({ args }) => {
])
.buildAsync()

await withDevServer({ cwd: builder.directory, args }, async ({ port }) => {
await withDevServer({ cwd: builder.directory, args }, async ({ port, outputBuffer }) => {
t.is(await got(`http://localhost:${port}/.netlify/functions/hello`).text(), 'WOOF!')

await pause(WAIT_WRITE)
Expand All @@ -188,13 +196,17 @@ testMatrix.forEach(({ args }) => {
.withContentFile({ path: 'functions/lib/util.js', content: `exports.bark = () => 'WOOF WOOF!'` })
.buildAsync()

await pWaitFor(
async () => {
const response = await got(`http://localhost:${port}/.netlify/functions/hello`).text()

return response === 'WOOF WOOF!'
},
{ interval: WAIT_INTERVAL, timeout: WAIT_TIMEOUT },
await tryAndLogOutput(
() =>
pWaitFor(
async () => {
const response = await got(`http://localhost:${port}/.netlify/functions/hello`).text()

return response === 'WOOF WOOF!'
},
{ interval: WAIT_INTERVAL, timeout: WAIT_TIMEOUT },
),
outputBuffer,
)
})
})
Expand Down Expand Up @@ -248,7 +260,7 @@ testMatrix.forEach(({ args }) => {
])
.buildAsync()

await withDevServer({ cwd: builder.directory, args }, async ({ port }) => {
await withDevServer({ cwd: builder.directory, args }, async ({ port, outputBuffer }) => {
t.is(
await got(`http://localhost:${port}/.netlify/functions/hello`).text(),
'Modern Web Development on the JAMStack',
Expand All @@ -267,13 +279,17 @@ testMatrix.forEach(({ args }) => {
})
.buildAsync()

await pWaitFor(
async () => {
const response = await got(`http://localhost:${port}/.netlify/functions/hello`).text()

return response === 'Modern Web Development on the Jamstack'
},
{ interval: WAIT_INTERVAL, timeout: WAIT_TIMEOUT },
await tryAndLogOutput(
() =>
pWaitFor(
async () => {
const response = await got(`http://localhost:${port}/.netlify/functions/hello`).text()

return response === 'Modern Web Development on the Jamstack'
},
{ interval: WAIT_INTERVAL, timeout: WAIT_TIMEOUT },
),
outputBuffer,
)
})
})
Expand All @@ -293,7 +309,7 @@ testMatrix.forEach(({ args }) => {
})
.buildAsync()

await withDevServer({ cwd: builder.directory, args }, async ({ port }) => {
await withDevServer({ cwd: builder.directory, args }, async ({ port, outputBuffer }) => {
const unauthenticatedResponse = await gotCatch404(`http://localhost:${port}/.netlify/functions/hello`)

t.is(unauthenticatedResponse.statusCode, 404)
Expand All @@ -310,17 +326,21 @@ testMatrix.forEach(({ args }) => {
})
.buildAsync()

await pWaitFor(
async () => {
try {
const response = await got(`http://localhost:${port}/.netlify/functions/hello`).text()

return response === 'Hello'
} catch (_) {
return false
}
},
{ interval: WAIT_INTERVAL, timeout: WAIT_TIMEOUT },
await tryAndLogOutput(
() =>
pWaitFor(
async () => {
try {
const response = await got(`http://localhost:${port}/.netlify/functions/hello`).text()

return response === 'Hello'
} catch (_) {
return false
}
},
{ interval: WAIT_INTERVAL, timeout: WAIT_TIMEOUT },
),
outputBuffer,
)
})
})
Expand Down Expand Up @@ -353,7 +373,7 @@ export { handler }
})
.buildAsync()

await withDevServer({ cwd: builder.directory, args }, async ({ port }) => {
await withDevServer({ cwd: builder.directory, args }, async ({ port, outputBuffer }) => {
const unauthenticatedResponse = await gotCatch404(`http://localhost:${port}/.netlify/functions/hello`)

t.is(unauthenticatedResponse.statusCode, 404)
Expand Down Expand Up @@ -386,17 +406,21 @@ export { handler }
})
.buildAsync()

await pWaitFor(
async () => {
try {
const response = await got(`http://localhost:${port}/.netlify/functions/hello`).text()

return response === 'Modern Web Development on the Jamstack'
} catch (_) {
return false
}
},
{ interval: WAIT_INTERVAL, timeout: WAIT_TIMEOUT },
await tryAndLogOutput(
() =>
pWaitFor(
async () => {
try {
const response = await got(`http://localhost:${port}/.netlify/functions/hello`).text()

return response === 'Modern Web Development on the Jamstack'
} catch (_) {
return false
}
},
{ interval: WAIT_INTERVAL, timeout: WAIT_TIMEOUT },
),
outputBuffer,
)
})
})
Expand All @@ -423,7 +447,7 @@ export { handler }
})
.buildAsync()

await withDevServer({ cwd: builder.directory, args }, async ({ port }) => {
await withDevServer({ cwd: builder.directory, args }, async ({ port, outputBuffer }) => {
t.is(await got(`http://localhost:${port}/.netlify/functions/hello`).text(), 'Hello')

await pause(WAIT_WRITE)
Expand All @@ -434,13 +458,17 @@ export { handler }
})
.buildAsync()

await pWaitFor(
async () => {
const { statusCode } = await gotCatch404(`http://localhost:${port}/.netlify/functions/hello`)

return statusCode === 404
},
{ interval: WAIT_INTERVAL, timeout: WAIT_TIMEOUT },
await tryAndLogOutput(
() =>
pWaitFor(
async () => {
const { statusCode } = await gotCatch404(`http://localhost:${port}/.netlify/functions/hello`)

return statusCode === 404
},
{ interval: WAIT_INTERVAL, timeout: WAIT_TIMEOUT },
),
outputBuffer,
)
})
})
Expand Down Expand Up @@ -468,7 +496,7 @@ export { handler }
})
.buildAsync()

await withDevServer({ cwd: builder.directory, args }, async ({ port }) => {
await withDevServer({ cwd: builder.directory, args }, async ({ port, outputBuffer }) => {
t.is(await got(`http://localhost:${port}/.netlify/functions/hello`).text(), 'Internal')

await pause(WAIT_WRITE)
Expand All @@ -484,17 +512,21 @@ export { handler }
})
.buildAsync()

await pWaitFor(
async () => {
try {
const response = await got(`http://localhost:${port}/.netlify/functions/hello`).text()

return response === 'Internal updated'
} catch (_) {
return false
}
},
{ interval: WAIT_INTERVAL, timeout: WAIT_TIMEOUT },
await tryAndLogOutput(
() =>
pWaitFor(
async () => {
try {
const response = await got(`http://localhost:${port}/.netlify/functions/hello`).text()

return response === 'Internal updated'
} catch (_) {
return false
}
},
{ interval: WAIT_INTERVAL, timeout: WAIT_TIMEOUT },
),
outputBuffer,
)
})
})
Expand Down Expand Up @@ -529,7 +561,7 @@ export { handler }
})
.buildAsync()

await withDevServer({ cwd: builder.directory, args }, async ({ port }) => {
await withDevServer({ cwd: builder.directory, args }, async ({ port, outputBuffer }) => {
t.is(await got(`http://localhost:${port}/.netlify/functions/hello`).text(), 'User')

await pause(WAIT_WRITE)
Expand All @@ -552,17 +584,21 @@ export { handler }
})
.buildAsync()

await pWaitFor(
async () => {
try {
const response = await got(`http://localhost:${port}/.netlify/functions/hello`).text()

return response === 'User updated'
} catch (_) {
return false
}
},
{ interval: WAIT_INTERVAL, timeout: WAIT_TIMEOUT },
await tryAndLogOutput(
() =>
pWaitFor(
async () => {
try {
const response = await got(`http://localhost:${port}/.netlify/functions/hello`).text()

return response === 'User updated'
} catch (_) {
return false
}
},
{ interval: WAIT_INTERVAL, timeout: WAIT_TIMEOUT },
),
outputBuffer,
)
})
})
Expand Down
Loading

1 comment on commit f3d433d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📊 Benchmark results

Package size: 331 MB

Please sign in to comment.