Skip to content

Commit

Permalink
fix(tests): rework server teardown in afterEach
Browse files Browse the repository at this point in the history
  • Loading branch information
Mairu committed Dec 26, 2023
1 parent c5d35a6 commit 1211cfe
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 36 deletions.
6 changes: 3 additions & 3 deletions test/custom-methods-v4.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('feathers 4 custom http methods', () => {
return; // only valid with feathers 4
}

let server;
let appTeardown;

const startFeathersWithService = (service) => {
const app = express(feathers())
Expand All @@ -21,10 +21,10 @@ describe('feathers 4 custom http methods', () => {
.configure(express.rest())
.use('/service', service);

return startFeathersApp(app, 6776).then((res) => { server = res; });
return startFeathersApp(app, 6776).then((res) => { appTeardown = res; });
};

afterEach(done => server.close(done));
afterEach(done => appTeardown(() => done()));

it('handle simple post method', async () => {
const customService = {
Expand Down
22 changes: 11 additions & 11 deletions test/custom-methods-v5.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ describe('feathers 5 custom methods', () => {
describe(`using customMethodsHandler with ${type}`, () => {
const { initApp } = options;

let server;
let appTeardown;

afterEach(done => server.close(done));
afterEach(done => appTeardown(() => done()));

it('should handle simple post method', async () => {
const customService = {
Expand All @@ -74,7 +74,7 @@ describe('feathers 5 custom methods', () => {
})
};

server = await startFeathersWithService(initApp, customService, ['find', 'getVersion']);
appTeardown = await startFeathersWithService(initApp, customService, ['find', 'getVersion']);

const { data: responseContent } = await axios.post(
'http://localhost:6776/service/getVersion?param=abc',
Expand All @@ -101,7 +101,7 @@ describe('feathers 5 custom methods', () => {
})
};

server = await startFeathersWithService(initApp, customService, ['find', 'getVersion']);
appTeardown = await startFeathersWithService(initApp, customService, ['find', 'getVersion']);

const { data: responseContent } = await axios.get('http://localhost:6776/service/5/version?param=abc');

Expand All @@ -124,7 +124,7 @@ describe('feathers 5 custom methods', () => {
})
};

server = await startFeathersWithService(initApp, customService, ['find', 'getVersion']);
appTeardown = await startFeathersWithService(initApp, customService, ['find', 'getVersion']);

const { data: responseContent } = await axios.get('http://localhost:6776/service/5/version?param=abc');

Expand All @@ -146,7 +146,7 @@ describe('feathers 5 custom methods', () => {
})
};

server = await startFeathersWithService(initApp, customService, ['find', 'setVersion']);
appTeardown = await startFeathersWithService(initApp, customService, ['find', 'setVersion']);

const { data: responseContent } = await axios.put(
'http://localhost:6776/service/5/version?param=abc',
Expand All @@ -171,7 +171,7 @@ describe('feathers 5 custom methods', () => {
})
};

server = await startFeathersWithService(initApp, customService, ['find', 'setVersion']);
appTeardown = await startFeathersWithService(initApp, customService, ['find', 'setVersion']);

const { data: responseContent } = await axios.patch(
'http://localhost:6776/service/5/version?param=abc',
Expand All @@ -196,7 +196,7 @@ describe('feathers 5 custom methods', () => {
})
};

server = await startFeathersWithService(initApp, customService, ['find', 'removeVersion']);
appTeardown = await startFeathersWithService(initApp, customService, ['find', 'removeVersion']);

const { data: responseContent } = await axios.delete(
'http://localhost:6776/service/5/version?param=abc'
Expand Down Expand Up @@ -226,7 +226,7 @@ describe('feathers 5 custom methods', () => {
});

describe('without customMethodsHandler', () => {
let server;
let appTeardown;
let app;

const initApp = () => {
Expand All @@ -236,7 +236,7 @@ describe('feathers 5 custom methods', () => {
});
};

afterEach(done => { server.close(done); app = undefined; });
afterEach(done => { appTeardown(() => { done(); app = undefined; }); });

it('should not fail and don\'t add custom methods to open api specs', async () => {
const customService = {
Expand All @@ -248,7 +248,7 @@ describe('feathers 5 custom methods', () => {
})
};

server = await startFeathersWithService(initApp, customService, ['find', 'getVersion']);
appTeardown = await startFeathersWithService(initApp, customService, ['find', 'getVersion']);

try {
await axios.post(
Expand Down
25 changes: 11 additions & 14 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ if (feathers.version && feathers.version[0] >= '5') {
* Start feathers app (compatibility layer to support feathers 4 and 5)
* @param {object} app
* @param {number} port
* @param {Function} [done]
* @return {Promise<object>} server
* @return {Promise<function>} appShutdown
*/
exports.startFeathersApp = async function (app, port, done) {
const server = await app.listen(port);
if (done) { done(); }
return server;
exports.startFeathersApp = async function (app, port) {
await app.listen(port);
return (done) => {
app.teardown().then(done);
};
};

const { SERVICE } = feathers;
Expand Down Expand Up @@ -59,16 +59,13 @@ if (feathers.version && feathers.version[0] >= '5') {
* Start feathers app (compatibility layer to support feathers 4 and 5)
* @param {object} app
* @param {number} port
* @param {Function} [done]
* @return {Promise<object>} server
* @return {Promise<Function>} server
*/
exports.startFeathersApp = function (app, port, done) {
let server;
exports.startFeathersApp = async function (app, port) {
return new Promise(resolve => {
server = app.listen(port, () => resolve());
}).then(() => {
if (done) { done(); }
return server;
const server = app.listen(port, () => resolve((done) => {
server.close(() => { done(); });
}));
});
};

Expand Down
6 changes: 3 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('feathers-swagger', () => {
const { initApp } = options;

describe(`should serve openapi json file with ${type}`, () => {
let server;
let appTeardown;
let app;

const startApp = (swaggerConfig) => {
Expand All @@ -36,10 +36,10 @@ describe('feathers-swagger', () => {
...swaggerConfig
}));

return startFeathersApp(app, 6776).then((res) => { server = res; });
return startFeathersApp(app, 6776).then((res) => { appTeardown = res; });
};

afterEach(done => server.close(done));
afterEach(done => appTeardown(() => { done(); appTeardown = undefined; }));

it('default as /swagger.json', async () => {
await startApp({});
Expand Down
9 changes: 4 additions & 5 deletions test/ui.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('feathers-swagger.swaggerUI', () => {
const { initApp } = options;
describe(`when using ${type}`, () => {
let app;
let appTeardown;
let messageService;

const startServiceWithUi = (ui) => {
Expand All @@ -45,7 +46,7 @@ describe('feathers-swagger.swaggerUI', () => {
)
.use('/messages', messageService);

return startFeathersApp(app, 6776);
return startFeathersApp(app, 6776).then(teardown => { appTeardown = teardown; });
};

before(done => {
Expand Down Expand Up @@ -74,10 +75,8 @@ describe('feathers-swagger.swaggerUI', () => {
});

afterEach(function (done) {
this.timeout(50000);
app.teardown().then(() => {
done();
});
this.timeout(10000);
appTeardown(() => done());
});

['/docs', '/docs/'].forEach((requestPath) => {
Expand Down

0 comments on commit 1211cfe

Please sign in to comment.