1

I'm a bit of a hacker (i.e. write some code and manually test the functionality) but now I want to add a bit more structure to my coding by adding some unit tests and follow a TDD approach. But I'm struggling to build a unit test that validates a method. So any insight would be appreciated.

I want to test my readDirectories method, but as it's asynchronous I'm going to have to use setTimeout - but my test keeps returning an error:

test.js

 test('The readDirectories method', (t) => {
  const xyz = new abc('/foo/bar/', '/foo/baz/');
  const expected = ['/foo/bar/Download', '/foo/bar/HardDrive' ];

  xyz.readDirectories(xyz.source)

  setTimeout(() => {
    let actual = xyz.directories;
    t.equal(actual, expected, 'should produce an array of subdirectories');
  }, 10);

});

console

operator: equal
expected: |-
  [ '/foo/bar/Download', '/foo/bar/HardDrive' ]
actual: |-
  [ '/foo/bar/Download', '/foo/bar/HardDrive' ]
at: Timeout.setTimeout (/home/user/Documents/app/tests/testModel.js:33:7)

Having looked at the example on Tape I believe I'm doing everything correctly...but then again I could just be doing something stupid! How do I get my test to pass???

hloughrey
  • 958
  • 3
  • 11
  • 22

1 Answers1

1

Turns out the test was failing due to the .equal test. .equal tests the my results are both arrays whereas .deepEqual tests that the two arrays have the same structure and nested values.

From the Tape website:

t.equal()

Assert that actual === expected with an optional description msg.

t.deepEqual()

Assert that actual and expected have the same structure and nested values using node's deepEqual() algorithm with strict comparisons (===) on leaf nodes and an optional description msg.

hlovdal
  • 26,565
  • 10
  • 94
  • 165
hloughrey
  • 958
  • 3
  • 11
  • 22