Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix flaky tests in CI #2933

Merged
merged 14 commits into from
Jul 14, 2021
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
/* eslint-disable require-await */
const { join } = require('path')
const process = require('process')

// eslint-disable-next-line ava/use-test
const avaTest = require('ava')
const test = require('ava')
const pWaitFor = require('p-wait-for')

const { withDevServer } = require('./utils/dev-server')
const got = require('./utils/got')
const { pause } = require('./utils/pause')
const { withSiteBuilder } = require('./utils/site-builder')

const test = process.env.CI === 'true' ? avaTest.serial.bind(avaTest) : avaTest
const testMatrix = [{ args: [] }, { args: ['esbuild'] }]
const testName = (title, args) => (args.length <= 0 ? title : `${title} - ${args.join(' ')}`)

const WAIT_INTERVAL = 1800
const WAIT_TIMEOUT = 30000
const WAIT_WRITE = 1000

const gotCatch404 = async (url, options) => {
try {
Expand Down Expand Up @@ -53,6 +52,8 @@ testMatrix.forEach(({ args }) => {
await withDevServer({ cwd: builder.directory, args }, async ({ port }) => {
t.is(await got(`http://localhost:${port}/.netlify/functions/hello`).text(), 'Hello')

await pause(WAIT_WRITE)

await builder
.withFunction({
path: 'hello.js',
Expand Down Expand Up @@ -118,6 +119,8 @@ testMatrix.forEach(({ args }) => {
'Modern Web Development on the JAMStack',
)

await pause(WAIT_WRITE)

await builder
.withContentFile({
path: 'functions/hello.ts',
Expand Down Expand Up @@ -179,6 +182,8 @@ testMatrix.forEach(({ args }) => {
await withDevServer({ cwd: builder.directory, args }, async ({ port }) => {
t.is(await got(`http://localhost:${port}/.netlify/functions/hello`).text(), 'WOOF!')

await pause(WAIT_WRITE)

await builder
.withContentFile({ path: 'functions/lib/util.js', content: `exports.bark = () => 'WOOF WOOF!'` })
.buildAsync()
Expand Down Expand Up @@ -249,6 +254,8 @@ testMatrix.forEach(({ args }) => {
'Modern Web Development on the JAMStack',
)

await pause(WAIT_WRITE)

await builder
.withContentFile({
path: 'functions/lib/util.ts',
Expand Down Expand Up @@ -291,6 +298,8 @@ testMatrix.forEach(({ args }) => {

t.is(unauthenticatedResponse.statusCode, 404)

await pause(WAIT_WRITE)

await builder
.withFunction({
path: 'hello.js',
Expand Down Expand Up @@ -349,6 +358,8 @@ export { handler }

t.is(unauthenticatedResponse.statusCode, 404)

await pause(WAIT_WRITE)

await builder
.withContentFile({
path: 'functions/hello.ts',
Expand Down Expand Up @@ -415,6 +426,8 @@ export { handler }
await withDevServer({ cwd: builder.directory, args }, async ({ port }) => {
t.is(await got(`http://localhost:${port}/.netlify/functions/hello`).text(), 'Hello')

await pause(WAIT_WRITE)

await builder
.withoutFile({
path: 'functions/hello.js',
Expand Down
6 changes: 6 additions & 0 deletions tests/utils/pause.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const pause = (interval) =>
new Promise((resolve) => {
setTimeout(resolve, interval)
})

module.exports = { pause }