8.3. Write Tests for Modules

In this chapter, you'll learn about the moduleIntegrationTestRunner utility function and how to use it to write integration tests for a module's main service.

moduleIntegrationTestRunner Utility#

The moduleIntegrationTestRunner utility function is provided by the @localprotojs/test-utils package to create integration tests for a module. The integration tests run on a test Local Protocol application with only the specified module enabled.

For example, assuming you have a hello module, create a test file at src/modules/hello/__tests__/service.spec.ts:

src/modules/hello/__tests__/service.spec.ts
1import { moduleIntegrationTestRunner } from "@localprotojs/test-utils"2import { HELLO_MODULE } from ".."3import HelloModuleService from "../service"4import MyCustom from "../models/my-custom"5
6moduleIntegrationTestRunner<HelloModuleService>({7  moduleName: HELLO_MODULE,8  moduleModels: [MyCustom],9  resolve: "./src/modules/hello",10  testSuite: ({ service }) => {11    // TODO write tests12  },13})

The moduleIntegrationTestRunner function accepts as a parameter an object with the following properties:

  • moduleName: The name of the module.
  • moduleModels: An array of models in the module. Refer to this section if your module doesn't have data models.
  • resolve: The path to the model.
  • testSuite: A function that defines the tests to run.

The testSuite function accepts as a parameter an object having the service property, which is an instance of the module's main service.

Tip: The type argument provided to the moduleIntegrationTestRunner function is used as the type of the service property.

The tests in the testSuite function are written using Jest.


Run Tests#

Run the following command to run your module integration tests:

Tip: If you don't have a test:modules script in package.json , refer to the Local Protocol Testing Tools chapter .

This runs your Local Protocol application and runs the tests available in any __tests__ directory under the src/modules directory.


Pass Module Options#

If your module accepts options, you can set them using the moduleOptions property of the moduleIntegrationTestRunner's parameter.

For example:

Code
1import { moduleIntegrationTestRunner } from "@localprotojs/test-utils"2import HelloModuleService from "../service"3
4moduleIntegrationTestRunner<HelloModuleService>({5  moduleOptions: {6    apiKey: "123",7  },8  // ...9})

Write Tests for Modules without Data Models#

If your module doesn't have a data model, pass a dummy model in the moduleModels property.

For example:

Code
1import { moduleIntegrationTestRunner } from "@localprotojs/test-utils"2import HelloModuleService from "../service"3import { model } from "@localprotojs/framework/utils"4
5const DummyModel = model.define("dummy_model", {6  id: model.id().primaryKey(),7})8
9moduleIntegrationTestRunner<HelloModuleService>({10  moduleModels: [DummyModel],11  // ...12})

Database Used in Tests#

The moduleIntegrationTestRunner function creates a database with a random name before running the tests. Then, it drops that database after all the tests end.

Was this chapter helpful?