1
\$\begingroup\$

Can anyone here comment on the quality of this test case? I am here testing for the exception should be thrown scenario. I mean it works but is it the correct way to unit test for the scenarios where the expectation is that exception should be thrown?

it('should throw exception if config.env.json is malformed', async () => {
  // Arrange: beforeEach
  const fakeFsStub = sinon.stub(File, 'readFileAsyc');
  fakeFsStub.withArgs('./src/configuration/config.json').returns(mockConfig);
  fakeFsStub.withArgs('./src/configuration/config.test.json').returns(FakePromise.resolve(`{"key"}`));

  try {
    // Act
    await Configuration.getConfiguration('test');
    chai.assert.fail('test case failed: [should throw exception if config.env.json is malformed]');
  } catch (e) {
    // Assert
    chai.assert.equal('SyntaxError: Unexpected token } in JSON at position 6', e + '');
  }
});
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

You should be using the Chai assert.throws operator instead (http://www.chaijs.com/api/assert/#method_throwsO so your code might look like:

it('should throw exception if config.env.json is malformed', async () => {
  // Arrange: beforeEach
  const fakeFsStub = sinon.stub(File, 'readFileAsyc');
  fakeFsStub.withArgs('./src/configuration/config.json').returns(mockConfig);
  fakeFsStub.withArgs('./src/configuration/config.test.json').returns(FakePromise.resolve(`{"key"}`));

  chai.assert.throws( () => {
      // Act
      await Configuration.getConfiguration('test');
    }, 
    SyntaxError,
    'test case failed: [should throw exception if config.env.json is malformed]'
  );

});

Also it is not generally a good idea to test the string returned by an exception. There is no guarantee that it will be the same between different browsers or different browser versions. If you need to check the position you should use the lineNumber and columnNumber properties of SyntaxError (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError)

Lastly concating with '' to perform a string conversion is not the best option either use e.messaage attribute of an exception or the toString() method available on pretty much all objects.

\$\endgroup\$
0
\$\begingroup\$

If it helps anybody in the future, I modified my test as pointed out by @MarcRohloff

it('should throw exception if config.env.json is malformed', async (done) => {
  // Arrange: beforeEach
  const fakeFsStub = sandbox.stub(File, 'readFileAsyc');
  fakeFsStub.withArgs('./src/configuration/config.json').returns(mockConfig);
  fakeFsStub.withArgs('./src/configuration/config.test.json').returns(FakePromise.resolve(`{"key"}`));

  chai.assert.throws(() => {
    // Act
    Configuration.getConfiguration('test').catch((e) => {
      chai.assert.instanceOf(e, SyntaxError);
      chai.assert.isTrue(e.toString().startsWith('SyntaxError: Unexpected token } in JSON'));
      done();
    });
  });
});
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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