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 + '');
 }
});
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Sep 11, 2018 at 2:50
\$\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.

answered Sep 11, 2018 at 3:44
\$\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();
 });
 });
});
answered Sep 11, 2018 at 16:09
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.