test method

  1. @override
String test(
  1. String path,
  2. String baseName,
  3. String className
)
override

Define test code. The path path is passed relative to lib, baseName is the filename, and className is the filename converted to Pascal case.

テストコードを定義します。pathlibからの相対パス、baseNameにファイル名が渡され、classNameにファイル名をパスカルケースに変換した値が渡されます。

Implementation

@override
String test(String path, String baseName, String className) {
  return """
/**
 * Test for ${className.toPascalCase()} functions.
 */
import * as admin from 'firebase-admin';

/**
 * File path of Functions for testing (omit extensions).
 */
import { ${className.toPascalCase()}Schedule } from '../src/functions/$baseName';

/**
 * Firebase project ID for testing.
 */
const testProjectId = "\${testProjectId}";

/**
 * Regions to deploy Functions.
 */
const regions = ["asia-northeast1"];

// Create tests for Functions.
const tester = require("firebase-functions-test")({
projectId: testProjectId,
});
describe(`Test: \${functionsFile}`, () => {
let functions: any;

// Performs initial setup for testing.
beforeAll(() => {
  admin.initializeApp();
  process.env.FIRESTORE_EMULATOR_HOST = "127.0.0.1:8080";
  functions = new ${className.toPascalCase()}Schedule().build(regions);
});
afterEach(async () => {
  await fetch(
    `http://\${process.env.FIRESTORE_EMULATOR_HOST}/emulator/v1/projects/\${testProjectId}/databases/(default)/documents`,
    { method: "DELETE" },
  );
});
afterAll(() => {
  tester.cleanup()
})

// The actual test is written from here.
it("Test Item 1", async () => {
  const firestoreInstance = admin.firestore();
  const parameters: { [key: string]: any } = {
  };

  const functionsWrapped = tester.wrap(functions);
  await functionsWrapped({
    params: parameters,
  });
});
});
""";
}