Below is the solution for the unit test:
concat.ts
:
export const concat = (str1: string, str2: string) => {
if (str1.length === 0 || str2.length === 0) {
throw new Error('either of the strings are empty');
}
let result = str1 + ' ' + str2;
return result;
};
concat.test.ts
:
import { concat } from './concat';
import { expect } from 'chai';
describe('61353628', () => {
it('should return the concatenated result', () => {
const str1 = 'hello';
const str2 = 'world';
const actual = concat(str1, str2);
expect(actual).to.be.equal('hello world');
});
it('should throw an error when str1 is empty', () => {
const str1 = '';
const str2 = 'world';
expect(() => concat(str1, str2)).to.throw('either of the strings are empty');
});
it('should throw an error when str2 is empty', () => {
const str1 = 'hello';
const str2 = '';
expect(() => concat(str1, str2)).to.throw('either of the strings are empty');
});
});
Unit test results showing 100% coverage:
61353628
✓ should return the concatenated result
✓ should throw an error when str1 is empty
✓ should throw an error when str2 is empty
3 passing (10ms)
-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
concat.ts | 100 | 100 | 100 | 100 |
-----------|---------|----------|---------|---------|-------------------
Source code can be found here: https://github.com/mrdulin/expressjs-research/tree/master/src/stackoverflow/61353628