You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was following the TypeScript and Testing guides but I had to work a lot in order to get the tests run with ts-jest.
Setup
This is my setup:
index.ts defines the handler
import{Request,Response,http}from"@google-cloud/functions-framework";// Register an HTTP function with the Functions FrameworkexportasyncfunctionhelloHTTPFunction(req: Request,res: Response){// Your code here// Send an HTTP responseres.send("OK");}http("helloHTTPFunction",helloHTTPFunction);
index.test.ts defines the handler's test
import{HttpFunction,Request,Response,}from"@google-cloud/functions-framework";// @ts-ignoreimport{getFunction}from"@google-cloud/functions-framework/testing";describe("HelloTests",()=>{beforeAll(async()=>{// load the module that defines HelloTestsawaitimport("./index");});it("is testable",()=>{// get the function using the name it was registered withconsthelloHTTPFunction=getFunction("helloHTTPFunction")asHttpFunction;// a Request stub with a simple JSON payloadconstreq={body: {foo: "bar"},}asRequest;// a Response stub that captures the sent responseconstres={send: (result)=>{// assert the response matches the expected valueexpect(result).toBe("OK");},}asResponse;// invoke the functionhelloHTTPFunction(req,res);});});
Node 18.12.1
TypeScript 4.9.5
jest 29.4.3
ts-jest 29.0.5
@google-cloud/functions-framework 3.1.3
OS: macOS Ventura on M1 Pro processor
Error
I get this error when I try to import the testing submodule without the // @ts-ignore in the test file
I think it's related to this TypeScript issue and the way the package.json is declared in @google-cloud/functions-framework module.
The test will also fail if not using the --experimental-vm-modules whith jest, but that's because of the await import("./index") statement in the hook.
So you need to add this to the package.json scripts
Adding the // @ts-ignore does the trick. However, if I am not messing up with the TS settings too much, wouldn't it be nice to have a more TS-friendly testing module package declaration?
And if you think (and I might agree with you) that this issue is more a TS problem than your library's, then could you please extend the documentation by adding a "testing with typescript" section in the docs (feel free to add my workaround if deemed good enough)?
The text was updated successfully, but these errors were encountered:
Goes without saying this is weird because the method exist if you import this way import functionsFramework from '@google-cloud/functions-framework/build/src/testing';
But does not function properly after being ran actively, needs to be reverted and ignored to be useful. I'm sure I'm missing a setting somewhere, but this is ridiculous in the year 2023.
Something is wrong in the way the types are exported.
As a workaround you should be able to use this instead: const {getFunction} = require('@google-cloud/functions-framework/testing');
Hi there,
I was following the TypeScript and Testing guides but I had to work a lot in order to get the tests run with ts-jest.
Setup
This is my setup:
index.ts
defines the handlerindex.test.ts
defines the handler's testjest.config.ts
ts-jest config filetsconfig.json
defines TS settingsEnvironment
Node 18.12.1
TypeScript 4.9.5
jest 29.4.3
ts-jest 29.0.5
@google-cloud/functions-framework 3.1.3
OS: macOS Ventura on M1 Pro processor
Error
I get this error when I try to import the
testing
submodule without the// @ts-ignore
in the test fileI think it's related to this TypeScript issue and the way the package.json is declared in
@google-cloud/functions-framework
module.The test will also fail if not using the --experimental-vm-modules whith jest, but that's because of the
await import("./index")
statement in the hook.So you need to add this to the
package.json
scripts"test": "NODE_OPTIONS=--experimental-vm-modules jest"
and then launch
npm test
.Workaround
Adding the
// @ts-ignore
does the trick. However, if I am not messing up with the TS settings too much, wouldn't it be nice to have a more TS-friendly testing module package declaration?And if you think (and I might agree with you) that this issue is more a TS problem than your library's, then could you please extend the documentation by adding a "testing with typescript" section in the docs (feel free to add my workaround if deemed good enough)?
The text was updated successfully, but these errors were encountered: