From 61ed028e6c993218c9f2789ed3513f6b21db89cf Mon Sep 17 00:00:00 2001 From: Rauno Viskus Date: Tue, 17 May 2022 14:53:08 +0300 Subject: [PATCH] style: remove unnecessary promise creation in the fastify tests (#1005) --- .../test/instrumentation.test.ts | 56 +++++++------------ 1 file changed, 21 insertions(+), 35 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts b/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts index 503606d2967..399dad31978 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/test/instrumentation.test.ts @@ -85,14 +85,10 @@ describe('fastify', () => { let PORT: number; let app: FastifyInstance; - function startServer(): Promise { - return new Promise(resolve => - app.listen(0, (err, address) => { - const url = new URL(address); - PORT = parseInt(url.port, 10); - resolve(); - }) - ); + async function startServer(): Promise { + const address = await app.listen(0); + const url = new URL(address); + PORT = parseInt(url.port, 10); } beforeEach(async () => { @@ -102,11 +98,9 @@ describe('fastify', () => { }); afterEach(async () => { - await new Promise(resolve => - app.close(() => { - resolve(); - }) - ); + if (app.server.address()) { + await app.close(); + } contextManager.disable(); contextManager.enable(); @@ -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(); @@ -136,7 +130,6 @@ describe('fastify', () => { }); await startServer(); - await httpRequest.get(`http://localhost:${PORT}/test`); const spans = memoryExporter.getFinishedSpans(); @@ -159,7 +152,6 @@ describe('fastify', () => { }); await startServer(); - await httpRequest.get(`http://localhost:${PORT}/test`); const spans = memoryExporter.getFinishedSpans(); @@ -207,8 +199,8 @@ describe('fastify', () => { app.register(subsystem); await startServer(); - await httpRequest.get(`http://localhost:${PORT}/test/1`); + assert.strictEqual(getSpans().length, 4); }); @@ -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); }); @@ -384,12 +376,10 @@ 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); }); @@ -397,29 +387,25 @@ describe('fastify', () => { 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(); }); }); });