0

I am currently getting into TDD and I wanted to write a test for a simple 'String.split()'.

The function should return an array of words that are contained in a String.

export function splitSentence(inputSentence) {
    const result = inputSentence.split(' ');
    return result;
}

I am using mocha as testing framework and chai for the assertion.

Here is the test.

describe('String Split', () => {
  it('should split a string into an array of words', () => {
    const inputString = 'This is some text';
    const expectedResult = ['This', 'is', 'some', 'text'];
    expect(splitSentence(inputString)).to.equal(expectedResult);
  });
});

While testing everything in the browser console it worked fine so I was confident that it would pass the test. However it failed with the following error message.

  AssertionError: expected [ 'This', 'is', 'some', 'text' ] to equal [ 'This', 'is', 'some', 'text' ]`

After looking at the error message it seems like they are both equal so I am currently wondering what went wrong here.

Thank your for your help.

Ironlors
  • 173
  • 3
  • 19
  • 1
    According to the Chai docs, `.equal` uses `===` under the covers. What you want is `.eql`, which uses a deep comparison. Docs: https://www.chaijs.com/api/bdd/ – Welbog Jul 29 '19 at 15:17
  • Oh wow XD. The docs I got only mentioned `.equal` Thank you. – Ironlors Jul 29 '19 at 15:19

0 Answers0