0

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    Please post your existing test cases, especially the one for the error case. – Bergi Dec 15 '20 at 16:17
  • @Bergi there is no error in the test. So I have 1st test that test the return of "successfully inserted" message and another test that verifies that the error is thrown. Mutation run is showing that there is a surviving mutation so what additional test is needed to kill it. – Shailesh123 Dec 15 '20 at 20:56
  • "*test that verifies that the error is thrown*" - that one. Please post the code of both test cases. – Bergi Dec 15 '20 at 21:10
  • This is one of the test where I am verifying the error ``` it("test failed to write in database", async () => { const error = new Error("Failed to write in database") const metadata = await mm.createObject(event.detail) stubAWSCall( sinonSandbox, AWS.DynamoDB.DocumentClient.prototype, "put", metadata.Item, true, error ) return mm.putMetadataItem(metadata).catch(err => { assert.throws(() => { throw error }, err) }) }) ``` – Shailesh123 Dec 15 '20 at 21:13
  • Please [edit] the question to include it, so you can use proper formatting – Bergi Dec 15 '20 at 21:16

1 Answers1

1

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'));
Bergi
  • 630,263
  • 148
  • 957
  • 1,375