Skip to content

Commit

Permalink
style: remove unnecessary promise creation in the fastify tests (open…
Browse files Browse the repository at this point in the history
  • Loading branch information
rauno56 authored May 17, 2022
1 parent 5f0d1e4 commit 61ed028
Showing 1 changed file with 21 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,10 @@ describe('fastify', () => {
let PORT: number;
let app: FastifyInstance;

function startServer(): Promise<void> {
return new Promise<void>(resolve =>
app.listen(0, (err, address) => {
const url = new URL(address);
PORT = parseInt(url.port, 10);
resolve();
})
);
async function startServer(): Promise<void> {
const address = await app.listen(0);
const url = new URL(address);
PORT = parseInt(url.port, 10);
}

beforeEach(async () => {
Expand All @@ -102,11 +98,9 @@ describe('fastify', () => {
});

afterEach(async () => {
await new Promise<void>(resolve =>
app.close(() => {
resolve();
})
);
if (app.server.address()) {
await app.close();
}

contextManager.disable();
contextManager.enable();
Expand All @@ -120,8 +114,8 @@ describe('fastify', () => {
app.get('/test', (req, res) => {
res.send('OK');
});
await startServer();

await startServer();
await httpRequest.get(`http://localhost:${PORT}/test`);

const spans = getSpans();
Expand All @@ -136,7 +130,6 @@ describe('fastify', () => {
});

await startServer();

await httpRequest.get(`http://localhost:${PORT}/test`);

const spans = memoryExporter.getFinishedSpans();
Expand All @@ -159,7 +152,6 @@ describe('fastify', () => {
});

await startServer();

await httpRequest.get(`http://localhost:${PORT}/test`);

const spans = memoryExporter.getFinishedSpans();
Expand Down Expand Up @@ -207,8 +199,8 @@ describe('fastify', () => {
app.register(subsystem);

await startServer();

await httpRequest.get(`http://localhost:${PORT}/test/1`);

assert.strictEqual(getSpans().length, 4);
});

Expand Down Expand Up @@ -375,7 +367,7 @@ describe('fastify', () => {
});

describe('application hooks', () => {
it('onRoute not instrumented', done => {
it('onRoute not instrumented', async () => {
app.addHook('onRoute', () => {
assert.strictEqual(context.active(), ROOT_CONTEXT);
});
Expand All @@ -384,42 +376,36 @@ describe('fastify', () => {
reply.send('OK');
});

startServer()
.then(() => done())
.catch(err => done(err));
await startServer();
});

it('onRegister is not instrumented', done => {
it('onRegister is not instrumented', async () => {
app.addHook('onRegister', () => {
assert.strictEqual(context.active(), ROOT_CONTEXT);
});
// register a plugin to trigger 'onRegister' hook
app.register((fastify, options, done) => {
done();
});
startServer()
.then(() => done())
.catch(err => done(err));

await startServer();
});

it('onReady is not instrumented', done => {
it('onReady is not instrumented', async () => {
app.addHook('onReady', () => {
assert.strictEqual(context.active(), ROOT_CONTEXT);
});
startServer()
.then(() => done())
.catch(err => done(err));

await startServer();
});

it('onClose is not instrumented', done => {
it('onClose is not instrumented', async () => {
app.addHook('onClose', () => {
assert.strictEqual(context.active(), ROOT_CONTEXT);
});
startServer()
.then(() => {
app.close().then(() => done());
})
.catch(err => done(err));

await startServer();
await app.close();
});
});
});
Expand Down

0 comments on commit 61ed028

Please sign in to comment.