mardi 8 septembre 2020

How to test agains null or undefined values?

I am creating an API using typescript and as usual, i am getting values from request.body. here is the thing, those values can be undefined and i need to pass those values to my factories.

Inside a factory i have a function that validates whether the value is null or undefined. (and some other validations) In this way i make sure not passing undefined or null values to my instances. But i cannot test it because of type definitions.

The value generated from request can be or not undefined, but the constructor can get that value. When i try to test it i cannot, because the interpreter do not let me pass a null or udnefined value (because of type), example:

...

const { value } = request.body;
    
const result = myFactory.create(value);
// This works fine because body can be anything, literaly, even undefined. 
//Althought the function is waiting for a number.
    

...

const nullValue = null;

const result = myFactory.create(nullValue);
// this does not work, because the function want a number and is getting a null value.
// but i need to do this in order to test that case.

...

Here is the thing: how can I test that? I cannot generate a null value for the situation where the function receibe a null or undefined and don't create the instance because of that.

should i get out of the function the code section that validate againstNullOrUndefined?

Aucun commentaire:

Enregistrer un commentaire