Outline

Terminal
npx nestia e2e
npx nestia e2e --config nestia.config.ts --project tsconfig.json

Configure nestia.config.ts file and run npx nestia e2e command.

Then, @nestia/sdk will analyze your NestJS backend server code, and generate both SDK (Software Development Kit) library for client developers. Also, E2E test functions, utilizing the SDK library, will be automatically generated for correspnding to every API functions.

Here is an example of generated E2E test functions:

test/api/features/automated/test_api_body_store.ts
import typia, { Primitive } from "typia";
 
import api from "../../../../src/api";
import type { IBbsArticle } from "../../../../src/api/structures/IBbsArticle";
 
export const test_api_body_store = async (
  connection: api.IConnection,
): Promise<void> => {
  const output = await api.functional.body.store(
    connection,
    typia.random<Primitive<IBbsArticle.IStore>>(),
  );
  typia.assert(output);
};

SDK

Left is server code, and right is e2e test code utilizing SDK library

Configuration

nestia.config.ts
import { INestiaConfig } from "@nestia/sdk";
import { NestFactory } from "@nestjs/core";
// import { FastifyAdapter } from "@nestjs/platform-fastify";
 
import { YourModule } from "./src/YourModule";
 
const NESTIA_CONFIG: INestiaConfig = {
  input: async () => {
    const app = await NestFactory.create(YourModule);
    // const app = await NestFactory.create(YourModule, new FastifyAdapter());
    // app.setGlobalPrefix("api");
    // app.enableVersioning({
    //     type: VersioningType.URI,
    //     prefix: "v",
    // })
    return app;
  },
  output: "src/api",
  distribute: "packages/api",
  e2e: "test",
};
export default NESTIA_CONFIG;

Make nestia.config.ts file and run npx nestia e2e command.

At first, create nestia.config.ts file through npx nestia init command. It would be placed on the top level directory of your NestJS backend project. For reference, tsconfig.json file also must be placed in the top level directory, too. After creation, configure the nestia.config.ts file referencing above example code and type definition.

At least, you’ve to configure those three properties:

  • input: Accessor of controller classes
  • output: Path of output directory for SDK library
  • e2e: Path of output directory for E2E test functions

When you’ve completed above configuration, just run npx nestia e2e command. Then, SDK library would be generated into the $config.output directory, and E2E test functions would be generated into the $config.e2e directory, following your nestia.config.ts option.

By the way, nestia.config.ts supports alternative options specifying the target controller classes instead of using the Module instance. If your backend application server does not have special configuration like setGlobalPrefix, enableVersioning and RouterModule, it is okay to specifying the target controller classes just by writing their file path like below.

nestia.config.ts
import { INestiaConfig } from "@nestia/sdk";
 
const NESTIA_CONFIG: INestiaConfig = {
  input: "src/**/*.controller.ts",
  output: "src/api",
  distribute: "packages/api",
  e2e: "test",
};
export default NESTIA_CONFIG;

Customization

test/index.ts
import core from "@nestia/core";
import { DynamicExecutor } from "@nestia/e2e";
 
import { INestApplication } from "@nestjs/common";
import { NestFactory } from "@nestjs/core";
 
async function main(): Promise<void> {
  const server: INestApplication = await NestFactory.create(
    await core.DynamicModule.mount({
      // follows your nestia.config.ts setting
      controllers: {
        input: ["src/controllers"],
        exclude: ["src/**/*.fake.ts"],
      },
    }),
  );
  await server.listen(37_000);
 
  const report: DynamicExecutor.IReport = await DynamicExecutor.validate({
    prefix: "test",
    parameters: () => [
      {
        host: "http://127.0.0.1:37000",
      },
    ],
  })(`${__dirname}/features`);
  await server.close();
 
  const exceptions: Error[] = report.executions
    .filter((exec) => exec.error !== null)
    .map((exec) => exec.error!);
  if (exceptions.length === 0) {
    console.log("Success");
    console.log("Elapsed time", report.time.toLocaleString(), `ms`);
  } else {
    for (const exp of exceptions) console.log(exp);
    console.log("Failed");
    console.log("Elapsed time", report.time.toLocaleString(), `ms`);
    process.exit(-1);
  }
}
main().catch((exp) => {
  console.log(exp);
  process.exit(-1);
});

Nothing be specified, customize by yourself.

When you generate e2e test functions through npx nestia e2e command, such index.ts file would be placed into the top level directory of test program. As you can see, the initial e2e test program only opens your NestJS backend server only with path of controllers with port number 37,000.

However, it may not fully meet your requirements. For example, you may connect to a database server, and also need to configure DI (Dependency Injection) classes, too. You’ve to configure those things by yourself. @nestia/sdk can analyzes your backend server in the compilation level, but unable to reproduce such customizations.

test/features/api/automated/test_api_bbs_articles_update.ts
import type { IConnection, Primitive } from "@nestia/fetcher";
import { PlainFetcher } from "@nestia/fetcher/lib/PlainFetcher";
import type { Format } from "typia/lib/tags/Format";
 
import api from "../../../../src/api";
import type { IBbsArticle } from "../../../../src/api/structures/IBbsArticle";
 
export const test_api_bbs_articles_update = async (
  connection: api.IConnection,
): Promise<void> => {
  const output = await api.functional.bbs.articles.update(
    connection,
    typia.random<string>(),
    typia.random<string & Format<"uuid">>(),
    typia.random<Primitive<IBbsArticle.IStore>>(),
  );
  typia.assert(output);
};

You also need to customize each e2e test functions.

When you run npx nestia e2e command, every e2e functions would be placed into $config.e2e/features/api/automated directory. Also, automatically generated e2e test functions are composing parameters through typia.random<T>() function.

If your NestJS backend server development has not been completed, and your API functions are in the mock-up level, such random parameter composition would not be problem. Otherwise your API functions are enoughly completed, such random parameter composition may occur logic error.

Therefore, you also need to customize automatically generated e2e test functions. Move each e2e test files from the $config.e2e/features/api/automated directory to somewhere else, and customize those e2e test functions to be suitable for your domain logics.