1.2.7 • Published 2 years ago

suite-slimmer-nest v1.2.7

Weekly downloads
-
License
MIT license
Repository
github
Last release
2 years ago

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()
1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago