firebase-functions-test icon indicating copy to clipboard operation
firebase-functions-test copied to clipboard

wrapped function not correctly catching error?

Open jonathan-chin opened this issue 1 year ago • 0 comments
trafficstars

Version info

firebase-functions-test: 3.1.1

firebase-functions: 3.18.0

firebase-admin: 10.0.2

Test case

// create.ts
// userSchema is a Zod object

const {
  HttpsError,
  onCall
} = require("firebase-functions/v2/https")
import {userSchema} from '@sma-v4/schema';

export default onCall({}, async (request: any, response: any) => {
  const schema = userSchema.pick({
    email: true,
    name: true,
    password: true,
  });
  try{
    schema.parse(request);
  }catch(error: any){
    throw new HttpsError(
      'invalid-argument',
      'z123fffff' // todo: build better error
    );
  }

  console.log(request);
  console.log(response);
});
// create.test.ts
import firebaseFunctionsTest from 'firebase-functions-test';
import create from './create';

const {wrap} = firebaseFunctionsTest();

describe('create', () => {
  const wrappedCreate = wrap(create);
  it('should throw an error if the zod schema is not followed', () => {
    expect(() => {
      wrappedCreate({
	email: 'this is not an email address'
      });
    }).toThrow();
});

Steps to reproduce

setup files as created above. run jest. the program throws the actual error and doesn't finish executing.

I've tried putting it in a try/catch block, etc. but I can't get it to work. other jest tests that don't rely on throwing an error behave as expected.

Expected behavior

test should either pass or fail and jest should complete

Actual behavior

 RUNS  src/user/create.test.ts
/Users/streaming/projects/sm/v4/apps/server/functions/src/user/create.ts:15
        throw new HttpsError('invalid-argument', 'z123fffff' // todo: build better error
              ^

HttpsError: z123fffff
    at Function.run (/Users/streaming/projects/sm/v4/apps/server/functions/src/user/create.ts:15:15)
    at wrapped (/Users/streaming/projects/sm/v4/node_modules/firebase-functions-test/lib/v1.js:71:30)
    at /Users/streaming/projects/sm/v4/apps/server/functions/src/user/create.test.ts:10:7
    at Object.<anonymous> (/Users/streaming/projects/sm/v4/node_modules/expect/build/toThrowMatchers.js:74:11)
    at Object.throwingMatcher [as toThrow] (/Users/streaming/projects/sm/v4/node_modules/expect/build/index.js:320:21)
    at Object.<anonymous> (/Users/streaming/projects/sm/v4/apps/server/functions/src/user/create.test.ts:13:8)
    at Promise.then.completed (/Users/streaming/projects/sm/v4/node_modules/jest-circus/build/utils.js:298:28)
    at new Promise (<anonymous>)
    at callAsyncCircusFn (/Users/streaming/projects/sm/v4/node_modules/jest-circus/build/utils.js:231:10)
    at _callCircusTest (/Users/streaming/projects/sm/v4/node_modules/jest-circus/build/run.js:316:40)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at _runTest (/Users/streaming/projects/sm/v4/node_modules/jest-circus/build/run.js:252:3)
    at _runTestsForDescribeBlock (/Users/streaming/projects/sm/v4/node_modules/jest-circus/build/run.js:126:9)
    at _runTestsForDescribeBlock (/Users/streaming/projects/sm/v4/node_modules/jest-circus/build/run.js:121:9)
    at run (/Users/streaming/projects/sm/v4/node_modules/jest-circus/build/run.js:71:3)
    at runAndTransformResultsToJestFormat (/Users/streaming/projects/sm/v4/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21)
    at jestAdapter (/Users/streaming/projects/sm/v4/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19)
    at runTestInternal (/Users/streaming/projects/sm/v4/node_modules/jest-runner/build/runTest.js:367:16)
    at runTest (/Users/streaming/projects/sm/v4/node_modules/jest-runner/build/runTest.js:444:34) {
  code: 'invalid-argument',
  details: undefined,
  httpErrorCode: { canonicalName: 'INVALID_ARGUMENT', status: 400 }
}

jonathan-chin avatar Feb 04 '24 21:02 jonathan-chin