-2
\$\begingroup\$

Fairly new to writing tests. I hoping to get feedback about test writing. I am curious to know if there is a convention/standard for short is better or more informative is better?

A) expect(mqError).to.not.be.null;

B) expect(mqError).to.be.an.instanceof(MonqadeError);

C) expect(mqError.code).to.eq('MongooseValidationError');

C will be precluded B which will precluded by A. In simple English. Is it better to determine a test failed.. or to determine where/why a test failed. Thoughts?

const doSomethingWithPromise = ()=> {
    return new Promise((resolve,reject)=>{
        this.getMongooseModelClass().findById(_ID,(error,doc)=>{

            if(error){
                return reject(new MonqadeError('MongooseError','Mongoose/MongoDB threw error model.find' , error) );
            }
            if(!doc){
                return reject( new MonqadeError("NoMatchingDocumentFound","No Records Found",undefined) ) ;    
            }
            return resolve(new MonqadeResponse([JSON.parse(JSON.stringify(doc))])); // monqade always returns Array .. Return Array will be an issue of 

        });

    }
}

Given A)

doSomethingWithPromise({})
.then(mqResponse=>{ //MonqadeResponse
    expect(mqResponse).to.be.null;
    done();
}).catch(mqError=>{ //MonqadeError
    expect(mqError).to.not.be.null;
    expect(mqError).to.be.an.instanceof(MonqadeError);
    expect(mqError.code).to.eq('MongooseValidationError');  
    done()
})

Or B)

.catch(mqError=>{ //MonqadeError
    expect(mqError.code).to.eq('MongooseValidationError');  
    done()
})

Where the expects in A overlap but will catch where the failure occurs.

\$\endgroup\$
-1
\$\begingroup\$

Turns out best practice is fewer tests. Write a test for the specific test case. This way when tests fail the cause is obvious.

B) above would be the bast practice.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.