Skip to content

Commit

Permalink
💚 Wait for server start before running e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
jellydn committed Sep 23, 2023
1 parent 9bc65e1 commit 9bc1059
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ jobs:
- id: test
name: E2E Test
run: |
bun run test:e2e
bun run ci
Binary file modified bun.lockb
Binary file not shown.
98 changes: 55 additions & 43 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
// Import the framework and instantiate it
import Fastify, { FastifyInstance, RouteShorthandOptions } from 'fastify'
import cors from '@fastify/cors'
import swagger from '@fastify/swagger';
import Fastify, {
FastifyInstance,
FastifyReply,
FastifyRequest,
RouteShorthandOptions,
} from "fastify";
import cors from "@fastify/cors";
import swagger from "@fastify/swagger";
import swaggerUI from "@fastify/swagger-ui";

const server: FastifyInstance = Fastify({
logger: true
})
logger: process.env.NODE_DEVELOPMENT === "test" ? false : true,
});

// Register plugins
await server.register(cors)
await server.register(cors);

await server.register(swagger, {
swagger: {
info: {
title: 'Test swagger',
description: 'Testing the Fastify swagger API',
version: '0.1.0'
title: "Test swagger",
description: "Testing the Fastify swagger API",
version: "0.1.0",
},
externalDocs: {
url: 'https://swagger.io',
description: 'Find more info here'
url: "https://swagger.io",
description: "Find more info here",
},
host: 'localhost',
schemes: ['http', 'https'],
consumes: ['application/json'],
produces: ['application/json'],
}
})
host: "localhost",
schemes: ["http", "https"],
consumes: ["application/json"],
produces: ["application/json"],
},
});

await server.register(swaggerUI, {
routePrefix: "/documentation",
Expand All @@ -40,46 +45,53 @@ await server.register(swaggerUI, {

// Declare a route
server.get<{
Querystring: { name: string },
}>('/hello', async function handler(request, _reply) {
const name = request.query.name
return { hello: name ?? 'world' }
})
Querystring: { name: string };
}>("/hello", async function handler(request) {
const name = request.query.name;
return { hello: name ?? "world" };
});

const opts: RouteShorthandOptions = {
schema: {
response: {
200: {
type: 'object',
type: "object",
properties: {
pong: {
type: 'string'
}
}
}
}
type: "string",
},
},
},
},
},
};

server.get(
"/ping",
opts,
async (_request: FastifyRequest, _reply: FastifyReply) => {
return { pong: "it worked!" };
}
}
);

server.get('/ping', opts, async (_request, _reply) => {
return { pong: 'it worked!' }
})
server.get("/", async (_request, _reply) => {
return { ok: true };
});

const start = async () => {
try {
await server.listen({ port: 3000 })

const address = server.server.address()
const port = typeof address === 'string' ? address : address?.port
server.log.info(`server listening on ${port}`)
await server.listen({ port: 3000 });

await server.ready()
server.swagger()
const address = server.server.address();
const port = typeof address === "string" ? address : address?.port;
server.log.info(`server listening on ${port}`);

await server.ready();
server.swagger();
} catch (err) {
server.log.error(err)
process.exit(1)
server.log.error(err);
process.exit(1);
}
}
};

start()
start();
23 changes: 13 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
{
"name": "stepci-demo",
"module": "index.ts",
"type": "module",
"module": "index.ts",
"scripts": {
"dev": "tsx watch index.ts",
"test:e2e": "stepci run workflow.yml",
"ci": "run-p --race test:*",
"test:server": "NODE_DEVELOPMENT=test tsx index.ts",
"test:e2e": "wait-on -l tcp:3000 && stepci run workflow.yml",
"start": "tsx index.ts"
},
"dependencies": {
"@fastify/cors": "^8.4.0",
"@fastify/swagger": "^8.10.1",
"@fastify/swagger-ui": "^1.9.3",
"fastify": "^4.23.2"
},
"devDependencies": {
"bun-types": "latest",
"npm-run-all": "^4.1.5",
"stepci": "^2.6.8",
"tsup": "^7.2.0",
"tsx": "^3.12.10"
"tsx": "^3.12.10",
"wait-on": "^7.0.1"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"@fastify/cors": "^8.4.0",
"@fastify/swagger": "^8.10.1",
"@fastify/swagger-ui": "^1.9.3",
"fastify": "^4.23.2"
}
}

0 comments on commit 9bc1059

Please sign in to comment.