GoogleCloudPlatform / GoogleCloudPlatform/functions-framework-nodejs
Testing with TypeScript and TS-Jest
まだ誰も着手していません。
- 主要言語
- TypeScript
- スター
- 1.4k
- フォーク
- 181
- PR マージ指標
- 30日以内にマージされた PR はありません
説明
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 handler
import { Request, Response, http } from "@google-cloud/functions-framework";
// Register an HTTP function with the Functions Framework
export async function helloHTTPFunction(req: Request, res: Response) {
// Your code here
// Send an HTTP response
res.send("OK");
}
http("helloHTTPFunction", helloHTTPFunction);
index.test.ts defines the handler's test
import {
HttpFunction,
Request,
Response,
} from "@google-cloud/functions-framework";
// @ts-ignore
import { getFunction } from "@google-cloud/functions-framework/testing";
describe("HelloTests", () => {
beforeAll(async () => {
// load the module that defines HelloTests
await import("./index");
});
it("is testable", () => {
// get the function using the name it was registered with
const helloHTTPFunction = getFunction(
"helloHTTPFunction"
) as HttpFunction;
// a Request stub with a simple JSON payload
const req = {
body: { foo: "bar" },
} as Request;
// a Response stub that captures the sent response
const res = {
send: (result) => {
// assert the response matches the expected value
expect(result).toBe("OK");
},
} as Response;
// invoke the function
helloHTTPFunction(req, res);
});
});
jest.config.ts ts-jest config file
import type { JestConfigWithTsJest } from "ts-jest";
const config: JestConfigWithTsJest = {
preset: "ts-jest",
slowTestThreshold: 5000,
testEnvironment: "node",
silent: true,
modulePathIgnorePatterns: ["<rootDir>/dist/", "/node_modules/"],
testPathIgnorePatterns: [
"<rootDir>/src/fixtures/",
"<rootDir>/dist/",
"/node_modules/",
],
coveragePathIgnorePatterns: [
"<rootDir>/src/fixtures/",
"<rootDir>/dist/",
"/node_modules/",
],
};
export default config;
tsconfig.json defines TS settings
{
"compilerOptions": {
"target": "ES2020",
"module": "CommonJS",
"moduleResolution": "nodenext",
"outDir": "dist",
"esModuleInterop": true,
"strict": true,
"skipLibCheck": true
}
}
Environment
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
"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)?
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
docs/typescript.md と docs/testing-functions.md を読み、次に testing サブモジュールの package export または宣言、およびリポジトリの Jest/TypeScript セットアップを調査します。報告されている ts-jest の import 問題を再現し、NODE_OPTIONS の回避策または確認済みのパッケージ宣言の制限を含む、サポートされている TypeScript テスト設定を文書化します。完了とは、ガイドが実行可能で、例が実行できる状態を意味します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- node.js, typescript
- 領域
- documentation, testing
- issue の種類
- ドキュメント
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 42/100