I am building an API with NestJS and typeorm. I need to test the structure of the data that is sent back to the client who perform requests to it.

For example, I could expect the route /players to send some data with a structure that looks like this (TypeScript example):

        id: number,
        name: string,
        hasCats: boolean, // optional
        birthday: date-time,
        team: {
            id: number,
            name: string

I found no tool that enables me to check that the data sent back to the client follow this pattern. Hence I have to write my own function. For the pattern, I use the documentation data provided as a JSON by Swagger on the route /api-json.

Is there a tool for testing the structure of some data?

1 Answers

Kim Kern On

Alternatively to using another library, you can just use jest's any matcher to assert an object's structure:

results.forEach(result => expect(result).toEqual({
      id: expect.any(Number),
      name: expect.any(String),
      date: expect.any(Date)