1414
1515import * as assert from 'assert' ;
1616import * as functions from '../../src/functions' ;
17+ import * as sinon from 'sinon' ;
1718import { getServer } from '../../src/server' ;
1819import * as supertest from 'supertest' ;
1920
@@ -31,6 +32,15 @@ const TEST_CLOUD_EVENT = {
3132} ;
3233
3334describe ( 'Event Function' , ( ) => {
35+ beforeEach ( ( ) => {
36+ // Prevent log spew from the PubSub emulator request.
37+ sinon . stub ( console , 'error' ) ;
38+ } ) ;
39+
40+ afterEach ( ( ) => {
41+ ( console . error as sinon . SinonSpy ) . restore ( ) ;
42+ } ) ;
43+
3444 const testData = [
3545 {
3646 name : 'GCF event' ,
@@ -188,4 +198,25 @@ describe('Event Function', () => {
188198 assert . deepStrictEqual ( receivedContext , test . expectedContext ) ;
189199 } ) ;
190200 } ) ;
201+
202+ it ( 'returns a 500 if the function throws an exception' , async ( ) => {
203+ const server = getServer ( ( ) => {
204+ throw 'I crashed' ;
205+ } , 'event' ) ;
206+ await supertest ( server )
207+ . post ( '/' )
208+ . send ( {
209+ eventId : 'testEventId' ,
210+ timestamp : 'testTimestamp' ,
211+ eventType : 'testEventType' ,
212+ resource : 'testResource' ,
213+ data : { some : 'payload' } ,
214+ } )
215+ . set ( { 'Content-Type' : 'application/json' } )
216+ . expect ( res => {
217+ assert . deepStrictEqual ( res . headers [ 'x-google-status' ] , 'error' ) ;
218+ assert . deepStrictEqual ( res . body , { } ) ;
219+ } )
220+ . expect ( 500 ) ;
221+ } ) ;
191222} ) ;
0 commit comments