# Testing

# Unit test

# Installation

Ts.ED support officially two unit test frameworks: Jest, Mocha and Vitest. It's also possible to use your preferred frameworks. Your feedback are welcome.

    # Usage

    Ts.ED provides PlatformTest to create a new context to inject your Services, Controllers, Middlewares, etc... registered with annotations like Injectable .

    The process to test any components is the same thing:

    • Create a new context for your unit test with PlatformTest.create,
    • Inject or invoke your component with PlatformTest.inject or PlatformTest.invoke,
    • Reset the context with PlatformTest.reset.

    Here is an example to test the ParseService:

      # Async / Await

      Testing asynchronous method is also possible using Promises (async/await):

        # Mock dependencies

        PlatformTest API provides an invoke method to create a new instance of your component with mocked dependencies.

          TIP

          PlatformTest.invoke() executes automatically the $onInit hook!

          # Test your Rest API

          # Installation

          To test your API, I recommend you to use the supertest (opens new window) module.

          To install supertest just run these commands:

            # Example

              WARNING

              If you use the PlatformTest, you'll probably get an error when you'll run the unit test:

              Platform type is not specified. Have you added at least `import @tsed/platform-express` (or equivalent) on your Server.ts ?
              
              1

              To solve it, just add the import @tsed/platform-express on your Server.ts. PlatformTest need this import to know on which Platform your server must be executed for integration test.

              # Pros / Cons

              WARNING

              Use PlatformTest.boostrap() is not recommended in Jest environment.
              This method is practical for carrying out some integration tests but consumes a lot of resources which can lead to a significant slowdown in your tests or even cause timeouts.

              It's better to write your tests using Cucumber and test your Rest applications in a container.

              Note

              There is no performance issue as long as you use PlatformTest.create() to perform your tests, But it's not possible with this method to do an integration test with the server (Express or Koa). You can only test your controller and the services injected into it.

              # Stub a service method

              When you're testing your API, you have sometimes to stub a method of a service.

              Here is an example to do that:

                # Stub a middleware method 6.114.3+

                When you're testing your API, you have sometimes to stub middleware to disable authentication for example.

                Here is an example to do that:

                  # Testing session

                  To install session with Ts.ED see our tutorial.

                    # Testing with mocked service v7.4.0

                    One inconvenient with PlatformTest.bootstrap() and PlatformTest.create() is that they will always call the hooks of your service like for example $onInit().

                    Note

                    PlatformTest.create() call only the $onInit() hook while PlatformTest.bootstrap() call all hooks.

                    This is going to be a problem when you want to test your application, and it uses $onInit to initialize your database or something else.

                    Since v7.4.0, You can now mock one or more services as soon as the PlatformTest context is created (like is possible with PlatformTest.invoke).

                    Here is an example:

                    import {MyCtrl} from "../controllers/MyCtrl";
                    import {DbService} from "../services/DbService";
                    
                    describe("MyCtrl", () => {
                      // bootstrap your Server to load all endpoints before run your test
                      beforeEach(() =>
                        PlatformTest.create({
                          imports: [
                            {
                              token: DbService,
                              use: {
                                getData: () => {
                                  return "test";
                                }
                              }
                            }
                          ]
                        })
                      );
                      afterEach(() => PlatformTest.reset());
                    });
                    
                    1
                    2
                    3
                    4
                    5
                    6
                    7
                    8
                    9
                    10
                    11
                    12
                    13
                    14
                    15
                    16
                    17
                    18
                    19
                    20
                    21

                    It's also possible to do that with PlatformTest.bootstrap():

                    import {PlatformTest} from "@tsed/common";
                    import SuperTest from "supertest";
                    import {Server} from "../../Server";
                    
                    describe("SomeIntegrationTestWithDB", () => {
                      beforeAll(
                        PlatformTest.bootstrap(Server, {
                          imports: [
                            {
                              token: DbService,
                              use: {
                                getData: () => {
                                  return "test";
                                }
                              }
                            }
                          ]
                        })
                      );
                      afterAll(PlatformTest.reset);
                    });
                    
                    1
                    2
                    3
                    4
                    5
                    6
                    7
                    8
                    9
                    10
                    11
                    12
                    13
                    14
                    15
                    16
                    17
                    18
                    19
                    20
                    21

                    Last Updated: 10/24/2024, 6:33:40 AM

                    Other topics