Testing

The framework provides a number of helpers for testing.

Backend

In your backend applications you can reduce a lot of complexity by using the scaffolding and helpers provided.

Scaffolding

The scaffolding is provided via the @juicyllama/core package and provides everything you need to perform tests quickly and effectively.

Pass into the scaffolding the NestJs Module and Service you wish to test. This will build the test suite with all the imports required and a bunch of pre-build services to leverage including the one you pass in.
//example.test.spec.ts

describe(`Example Test Suite`, () => {
    const scaffolding = new Scaffold<T>()
    let scaffold: ScaffoldDto<T>

    beforeAll(async () => {
        scaffold = await scaffolding.up(MODULE, SERVICE)
    })

    // tests here

    afterAll(async () => {
        await scaffolding.down(E)
    })
})

You can then use the scaffold object to access the following (use the ScaffoldDto type to get intellisense):

PropertyDescription
serverThe NestJs HttpServer server instance
moduleThe TestingModule instance
queryThe typeorm query helper
repositoryThe typeorm repository for the entity
servicesA number of pre-imported services and the service you passed in
valuesA number of pre-imported values you can use in your tests

Controllers

A TestEndpoint helper is provided to make testing CRUD endpoints easier.

await TestEndpoint<T>({
    type: METHOD.GET,
    scaffold: scaffold,
    url: `${url}/${primaryKey}`,
    PRIMARY_KEY: PRIMARY_KEY,
})

The following properties are available:

PropertyDescription
typeThe HTTP METHOD to use
scaffoldThe scaffold object
urlThe URL to call
dataThe data to pass to the endpoint in the body
queryAn object with any query params you wish to pass
PRIMARY_KEYThe name of the primary key for the entity
primaryKeyThe value of the primary key for the entity
access_tokenIf you wish to use a different user to the owner of the testing account
accountIf you wish to pass make the request for different account from the account setup in the scaffolding
emitCheckResultKeysAn array of keys to skip validating in the response

Services

A TestService helper is provided to make testing your services easier.

await TestService<T>({
    type: METHOD.GET,
    scaffold: scaffold,
    PRIMARY_KEY: PRIMARY_KEY,
})

The following properties are available:

PropertyDescription
typeThe METHOD to use
scaffoldThe scaffold object
mockThe data to pass to the endpoint in the body
PRIMARY_KEYThe name of the primary key for the entity
recordAn existing record to compare the resulting action to

Frontend

We need to build a similar scaffolding for the frontend.

Docs v.0.14.0 Copyright © 2024