From 0369d710cca411dda33c5567f9764f6be606f085 Mon Sep 17 00:00:00 2001 From: Bernardo Guerreiro Date: Wed, 13 Mar 2024 11:44:55 +0000 Subject: [PATCH] test(unit): improve before/after teardown in plugin test --- .../plugins/plugins-processor.test.js | 48 +++++++++++-------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/packages/artillery/test/testcases/plugins/plugins-processor.test.js b/packages/artillery/test/testcases/plugins/plugins-processor.test.js index 4f47bf7b53..8798cce9a7 100644 --- a/packages/artillery/test/testcases/plugins/plugins-processor.test.js +++ b/packages/artillery/test/testcases/plugins/plugins-processor.test.js @@ -1,4 +1,4 @@ -const { test } = require('tap'); +const { test, beforeEach, before, afterEach } = require('tap'); const http = require('http'); const { $ } = require('zx'); const path = require('path'); @@ -18,10 +18,16 @@ function createServer() { }); } -(async function () { - const server = createServer().listen(0); +let server; +let overrides; - const overrides = JSON.stringify({ +before(async () => { + await $`${A9} -V`; +}); + +beforeEach(async () => { + server = createServer().listen(0); + overrides = JSON.stringify({ config: { phases: [{ duration: 2, arrivalRate: 2 }], target: `http://localhost:${server.address().port}`, @@ -31,20 +37,20 @@ function createServer() { } } }); - - await $`${A9} -V`; - - test('plugins can attach functions to processor object', async (t) => { - const output = await $`ARTILLERY_PLUGIN_PATH=${path.join( - __dirname, - '../../plugins' - )} ${A9} run --quiet --overrides ${overrides} ${path.join( - __dirname, - '/script.json' - )}`; - - t.match(output, /afterResponse hook/, 'plugin should have been called'); - - server.close(t.end); - }); -})(); +}); + +afterEach(async () => { + server.close(); +}); + +test('plugins can attach functions to processor object', async (t) => { + const output = await $`ARTILLERY_PLUGIN_PATH=${path.join( + __dirname, + '../../plugins' + )} ${A9} run --quiet --overrides ${overrides} ${path.join( + __dirname, + '/script.json' + )}`; + + t.match(output, /afterResponse hook/, 'plugin should have been called'); +});