简体   繁体   中英

Chai logs error into console instead of failing the test

I have the following test code that verifies that after calling a function that returns a promise, the resulting array is the same as the stubbed one.

it('Should return locations', () => {
  const result = loginSvc.getLocations()

  expect(result).to.eventually.eql(['location1', 'asd', 'location3', 'location4', 'location5'])   
})

loginSvc.getLocations() is just a function that is mocked and returns an array: ['location1', 'location2', 'location3', 'location4', 'location5']

When I run the test it doesn't fail as it should or even succeed as a false positive, and in the console I get this:

ERROR LOG: 'Unhandled promise rejection', AssertionError{message: 'expected [ Array(5) ] to have the same members as [ Array(5) ]', showDiff: true, actual: ['location1', 'location2', 'location3', 'location4', 'location5'], expected: ['location1', 'asd', 'location3', 'location4', 'location5'], stack: 'AssertionError@ http://localhost:9000/base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae:9320:24 assert@ http://localhost:9000/base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae:239:31 somethingMethod@ http://localhost:9000/base/node_modules/chai-things/lib/chai-things.js?da5f13ef7d7d30f512b1cd8c3a12b3ed43cd7d31:97:30 overwritingMethodWrapper@ http://localhost:9000/base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae:8932:38 allMethod@ http://localhost:9000/base/node_modules/chai-things/lib/chai-things.js?da5f13ef7d7d30f512b1cd8c3a12b3ed43cd7d31:165:30 overwritingMethodWrapper@ http://localhost:9000/base/node_modules/chai/chai.js ?40e7aa72e9665366bfd82579520de4fb0754dfae:8932:38 http://localhost:9000/base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae:3379:16 methodWrapper@ http://localhost:9000/base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae:7709:30 http://localhost:9000/base/node_modules/chai-as-promised/lib/chai-as-promised.js?ac71de40b7ca85a0488f7d3c971a22ddd0e149a8:308:31 run@ http://localhost:9000/base/spec.js?20bf9e1ddf32e8fc2bfe38226be11b7e65336abf:72447:29 http://localhost:9000/base/spec.js?20bf9e1ddf32e8fc2bfe38226be11b7e65336abf:72460:33 flush@ http://localhost:9000/base/spec.js?20bf9e1ddf32e8fc2bfe38226be11b7e65336abf:72685:11 ', line: 243, sourceURL: ' http://localhost:9000/base/node_modules/chai/chai.js?40e7aa72e9665366bfd82579520de4fb0754dfae '}

But the test passes

I believe I found a solution, or rather a workaround:

it('Should return locations', done => {
  loginSvc.getLocations()
    .then(locations => {
      expect(locations ).to.eql(['location1', 'asd', 'location3', 'location4', 'location5'])
      done()
    })
    .catch(done)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM