#E2E testing of versioned endpoints

1 messages · Page 1 of 1 (latest)

compact musk
#

Hi!

I have versioned endpoints like this using the @Version(‘<version>’) decorator:

/api/v1/auth/<endpoint>
/api/v2/auth/<endpoint>

However, when writing E2E tests using the docs, adding /api/v1 to the URL causes everything to return 404s. Removing it and testing only auth/<endpoint> makes the tests pass. However, when it comes to our versioned endpoints, it always points to the first instance of that endpoint in the controller file.

I’ve tried searching online and haven’t found anything regarding how to access the different versioned endpoints in the E2E tests. Below is my current test bootstrapping

beforeAll(async () => {
        const moduleRef = await Test.createTestingModule({
            imports: [AppModule],
        }).compile()

        app = moduleRef.createNestApplication()
        await app.init()
        httpServer = app.getHttpServer()
    })

Example endpoint to test

@Version('2')
@Get('')
async getDisplayNamesV2(): Promise<DisplayNamesResponseDtoV2> {
    return ...
}

@Get('')
async getDisplayNames(
    @Res({ passthrough: true }) res: Response,
): Promise<DisplayNamesResponseDto> {
    return ....
}
compact musk
#

Solved it myself by adding the code below to beforeAll

// Enable v1/v2 endpoints for these tests
app.enableVersioning({
    type: VersioningType.URI,
})

My v1 endpoints were then accessible with regular baseUrl of /endpoint (without /api/v1/ like in regular running app), and v2 endpoints worked with /v2/<endpoint> (without /app)

noble wedge
#

That's why I always recommend moving global setup like this to a separate file and function (like setup(app: NestApplication)), that you call both from your main entrypoint and also in any e2e test. This keeps the setup in sync and you don't need to duplicate it in your tests