UnitTest GraphQL query - fail on undefined fields

Discussão em 'Angular' iniciado por ChrisK, Fevereiro 21, 2025.

  1. ChrisK

    ChrisK Guest

    One of my angular components loads a Person from the backend graphql endpoint. This can be tested by using the apollo testing controller and module.

    it('queries for a person', () => {
    component['loadPerson']();
    const op = apolloController.expectOne(PERSON_QUERY);
    expect(op.operation.variables.id).toEqual('1');
    op.flushData({
    data: {
    person: {
    id: 0,
    firstName: 'any'
    },
    },
    });
    apolloController.verify();
    });


    Now I had a few occassions where the frontend fails but the test won't. Let's say the firstName property that prior existed was changed in the backend to name. If the PERSON_QUERY is not updated the backend will raise a GraphQLError ("Field 'firstName' doesn't exist on type 'Person' (Did you mean name?)") once the component is opened. The test on the other hand will be valid.

    I know that the error handling is handeled by the backend and not tested in the frontend and I know that testing a live API is a bad idea.

    I think the best way would be to at least check if the flushed data in the test would be equal to the requested data in the query (PERSON_QUERY) or is there another way to test this behaviour? Maybe dump the backend schema in anyway and test it against the query?

    Continue reading...

Compartilhe esta Página