GoogleCloudPlatform / functions-framework-nodejs

FaaS (Function as a service) framework for writing portable Node.js functions
Apache License 2.0
1.28k stars 158 forks source link

Supertest: getTestServer Not Found #594

Open adamkpurdy opened 3 months ago

adamkpurdy commented 3 months ago

Hello and to whom it may concern.

I am trying to work with the implementation of getTestServer in Supertest documentation, and when I try to instantiate my test server I get this error:

Screenshot 2024-03-25 at 9 58 27 AM

@google-cloud/functions-framework is installed as a dependency of this project so my assumption would have been that this should be available in the import.

I've Googled a few things and I've tried updating my tsconfig, but nothing seems to work.

Thanks ahead.

kenneth-rosario commented 3 months ago

Hey,

Can you share your tsconfig.json?

adamkpurdy commented 3 months ago

Sure thing:

tsconfig.json

{
  "extends": "./node_modules/gts/tsconfig-google.json",
  "include": ["src/**/*.ts"],
  "compilerOptions": {
    "esModuleInterop": true,
    "rootDir": ".",
    "outDir": "build",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "noImplicitAny": false,
    "resolveJsonModule": true
  }
}

I tried adding: "target": "ES2020", // or "ESNext" "moduleResolution": "node",

kenneth-rosario commented 3 months ago

Thanks,

can you try setting moduleResolution to nodenext?

adamkpurdy commented 3 months ago

When I did that the terminal threw an error saying: "error TS5110: Option 'module' must be set to 'NodeNext' when option 'moduleResolution' is set to 'NodeNext'." I did that and got it to compile correctly, but then my test threw a jest configuration error"

FAIL tests/endpoints.spec.ts ● Test suite failed to run

Unable to process '/Users/adampurdy/Sites/KPA_PRODUCTIONS/USSSA/ms-platform/event-api-ms/__tests__/endpoints.spec.ts', please make sure that `outDir` in your tsconfig is neither `''` or `'.'`. You can also configure Jest config option `transformIgnorePatterns` to inform `ts-jest` to transform /Users/blah/Sites/blah/blah/ms-platform/event-api-ms/__tests__/endpoints.spec.ts

  at TsCompiler.getCompiledOutput (node_modules/ts-jest/dist/legacy/compiler/ts-compiler.js:173:27)
  at TsJestCompiler.getCompiledOutput (node_modules/ts-jest/dist/legacy/compiler/ts-jest-compiler.js:14:39)
  at TsJestTransformer.exports.TsJestTransformer.TsJestTransformer.processWithTs (node_modules/ts-jest/dist/legacy/ts-jest-transformer.js:265:37)
  at TsJestTransformer.exports.TsJestTransformer.TsJestTransformer.process (node_modules/ts-jest/dist/legacy/ts-jest-transformer.js:188:24)
  at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:545:31)
  at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:674:40)
  at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:726:19)

I tried adjusting jest.config.js and that didn't work either.

Can you help me understand why I need to adjust the tsconfig, when I'm importing other @google-cloud/functions-framework functions like:

import { cloudEvent } from '@google-cloud/functions-framework'. That works fine without adjusting my tsconfig from what it was.

Thanks

adamkpurdy commented 3 months ago

@kenneth-rosario I was able to access the getTestServer function via this assignment: const getTestServer = require('@google-cloud/functions-framework')

I then did a little inspection of what was being returned via these three statements:

console.log(typeof getTestServer, 'getTestServer typeof') console.log(getTestServer, 'getTestServer') console.log(getTestServer(), 'getTestServer invocation')

And the output:

console.log object getTestServer typeof

  at Object.<anonymous> (src/api/eventHandler/event.controller.spec.ts:8:9)

console.log { http: [Getter], cloudEvent: [Getter], typed: [Getter], JsonInvocationFormat: [Getter] } getTestServer

  at Object.<anonymous> (src/api/eventHandler/event.controller.spec.ts:9:9)

FAIL src/api/eventHandler/event.controller.spec.ts ● Test suite failed to run

TypeError: getTestServer is not a function

Not what I was expecting from the instructions in the documentation.

Our API application is a microservice with a small express app wrapped in a cloud function. Our approach is working great, I am just having trouble calling GCP services when trying to run integration tests using supertest test runner.

Any insight would be helpful. Thank you.

kenneth-rosario commented 3 months ago

Hey @adamkpurdy can you provide a very minimal reproduction repo I can use to poke around?

Not sure if https://github.com/GoogleCloudPlatform/functions-framework-nodejs/blob/main/docs/typescript.md might be of help as well.

adamkpurdy commented 3 months ago

@kenneth-rosario Thanks for your help on this.

Looks like its a TypeScript issue. When I goto test this in a basic javascript file, it works as expected, but when I changed it into a ts file I get the issue. I know we tried adjusting the tscofig, but that doesn't seem to work. Here is my basic file.

functions-framwork.zip