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 <gordon.sun@pipe17.com>
This commit is contained in:
Gordon Sun
2020-04-21 10:37:43 -07:00
committed by GitHub
parent f2f772084f
commit 9d8955cf90

View File

@@ -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);
}
});
});