suite-slimmer-nest
TypeScript icon, indicating that this package has built-in type declarations

1.2.7 • Public • Published

suite-slimmer-nest   GitHub Workflow Status   Node version

Streamlines NestJS testing.

  • Encapsulates test module setup, reducing code duplication and boilerplate
  • Injects tested class and mocked dependencies directly into each test, no need for managing global variables
  • Mocks and spies on dependencies dynamically so you don't have to do it yourself
  • Easily phased into existing projects with no additional configuration required

Installation

npm install suite-slimmer-nest --save-dev

Usage

Installation

Install the npm package.

npm install --save-dev suite-slimmer-nest

Creating a test

Instantiate the framework suite (NestJSTestSuite or e2eNestJSTestSuite), providing the type of the class you are testing as a required argument for non-e2e tests.

new NestJSTestSuite(MyExampleController)

On this object, the following methods available and can be chained:

  • addImports
  • addDeclarations
  • addProviders
  • addMocks
  • addTest
  • beforeEach
  • afterEach
  • run

Frameworks

The following test frameworks are supported:

  • Jasmine
  • Jest
  • Mocha

Examples

Before:

describe('AppController', () => {
    let appController: AppController;
    let service: AppService;

    beforeEach(async () => {
        const app: TestingModule = await Test.createTestingModule({
        controllers: [AppController],
        providers: [
            {
                provide: AppService,
                useValue: {
                    isOvenOn: jest.fn().mockReturnValue(true),
                    putCookiesInOven: jest.fn().mockReturnValue(10),
                },
            },
        ],
        }).compile();

        appController = app.get<AppController>(AppController);
        service = app.get<AppService>(AppService);
    });

    it('should bake cookies', () => {
        appController.bakeCookies().subscribe({
            ...
        });
    });
});

After:

new NestJSTestSuite(AppController)
    .addMocks(AppService)
    .beforeEach((controller, mocks) => {
        mocks.get(AppService).isOvenOn.mockReturnValue(true);
        mocks.get(AppService).putCookiesInOven.mockReturnValue(10);
    })
    .addTest('should bake cookies', (controller) => {
        controller.bakeCookies().subscribe({
            ...
        });
    })
    .run()

Readme

Keywords

none

Package Sidebar

Install

npm i suite-slimmer-nest

Weekly Downloads

7

Version

1.2.7

License

MIT license

Unpacked Size

14.4 kB

Total Files

21

Last publish

Collaborators

  • ahefley