简体   繁体   中英

How to Kill Surviving mutation JavaScript

I have this code

const writeToDB = async (data) => {
    console.log("Inside db put")
    try {
        const resp = await dynamoDB.put(data).promise();
        console.log("Data added db: ", resp);
        return "successfully inserted"
    } catch (err){
       throw new Error(`Failed to write in database`, err)
  }
}

I have 2 tests for this functionality one to check when its sucessful and one where it throws an error. When I run stryker I get a surviving mutation


    - } catch (err){
    -   throw new Error(`Failed to write in database`, err)
    - }
     + } catch (err){}

I believe this is trying to find a test "if it catches the error but does not throw the error". How do I write a test to kill this particular Blockstatement mutation. The code is always going to throw the error that I have specified.

The mutation test is entirely correct. The approach

return mm.putMetadataItem(metadata).catch(err => { assert.throws(() => { throw error }, err) })

to check for the expected behaviour of the putMetadataItem function is wrong. In particular, if the returned promise is not rejected, the .catch() callback with your assertion doesn't run at all, and the fulfilled promise is returned, which causes the unit test to pass. Also assert.throws is rather pointless here, you know for sure that throw error will throw, so all this does is to check equality between error and err .

You would need to write

return mm.putMetadataItem(metadata).then(() => {
  throw new AssertionError('expected putMetadataItem() to reject');
}, err => {
  assert.equal(error, new Error('Failed to write in database'));
});

however you actually should use assert.rejects :

return assert.rejects(() => {
  return mm.putMetadataItem(metadata);
}, new Error('Failed to write in database'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM