From 9d8955cf90d3ad1e953a1ef70077b99fc68eabc1 Mon Sep 17 00:00:00 2001 From: Gordon Sun Date: Tue, 21 Apr 2020 10:37:43 -0700 Subject: [PATCH] improve a test case (#6629) 1. do not need to create a new server, use the reconfigureServer tool 3. use async await Co-authored-by: Gordon Sun --- spec/EnableExpressErrorHandler.spec.js | 69 +++++++++----------------- 1 file changed, 24 insertions(+), 45 deletions(-) diff --git a/spec/EnableExpressErrorHandler.spec.js b/spec/EnableExpressErrorHandler.spec.js index b81a0229..52cb985c 100644 --- a/spec/EnableExpressErrorHandler.spec.js +++ b/spec/EnableExpressErrorHandler.spec.js @@ -1,54 +1,33 @@ -const ParseServer = require('../lib/index'); -const express = require('express'); const request = require('../lib/request'); describe('Enable express error handler', () => { - it('should call the default handler in case of error, like updating a non existing object', done => { - const serverUrl = 'http://localhost:12667/parse'; - const appId = 'anOtherTestApp'; - const masterKey = 'anOtherTestMasterKey'; - let server; - - const parseServer = ParseServer.ParseServer( + it('should call the default handler in case of error, like updating a non existing object', async (done) => { + const parseServer = await reconfigureServer( Object.assign({}, defaultConfiguration, { - appId: appId, - masterKey: masterKey, - serverURL: serverUrl, enableExpressErrorHandler: true, - serverStartComplete: () => { - expect(Parse.applicationId).toEqual('anOtherTestApp'); - const app = express(); - app.use('/parse', parseServer); - - server = app.listen(12667); - - app.use(function(err, req, res, next) { - expect(err.message).toBe('Object not found.'); - next(err); - }); - - request({ - method: 'PUT', - url: serverUrl + '/classes/AnyClass/nonExistingId', - headers: { - 'X-Parse-Application-Id': appId, - 'X-Parse-Master-Key': masterKey, - 'Content-Type': 'application/json', - }, - body: { someField: 'blablabla' }, - }) - .then(() => { - fail('Should throw error'); - }) - .catch(response => { - expect(response).toBeDefined(); - expect(response.status).toEqual(500); - }) - .then(() => { - server.close(done); - }); - }, }) ); + parseServer.app.use(function (err, req, res, next) { + expect(err.message).toBe('Object not found.'); + next(err); + }); + + try { + await request({ + method: 'PUT', + url: defaultConfiguration.serverURL + '/classes/AnyClass/nonExistingId', + headers: { + 'X-Parse-Application-Id': defaultConfiguration.appId, + 'X-Parse-Master-Key': defaultConfiguration.masterKey, + 'Content-Type': 'application/json', + }, + body: { someField: 'blablabla' }, + }); + fail('Should throw error'); + } catch (response) { + expect(response).toBeDefined(); + expect(response.status).toEqual(500); + parseServer.server.close(done); + } }); });