mercredi 6 février 2019

Avoid warnings when using jest to test that data corresponds to TypeScript types

In the example below I want to verify that the data actually gives med a project with a property id.

test('getActiveProject => Project', async () => {
    let previouslyOpenProject = await client.getActiveProject()
    if (!previouslyOpenProject) {
        previouslyOpenProject = await client.createProject();
    }
    let project = await client.getActiveProject();
    expect(project.id).toBeGreaterThanOrEqual(1);
});

I get the following ts error message

[ts] Object is possibly 'null' (referring to project and the use of project.id)

What is a good course of action from here:

  • Should I write my tests in js instead of ts?
  • Should I find a testing framework with a TypeScript pre-processor that will allow me to use TypeScript types in test expectations? (I don't think this exists)
  • Should these kinds of checks be made by some kind of json validation scheme instead of jest tests?

Aucun commentaire:

Enregistrer un commentaire