I’m using the delightful chai-string to test responses that should start with a known string. When working with promises, and specifically, the awesome chai-as-promised, the order in which you use the chai plugins is important, regardless if you have one file or multiple!
I first came across this in chai-string issue #9. Since then I have added more test files and had tests that
should.eventually.startWith('some string') fail. Oddly, when I ran the test individually, or ran all the tests in a single file, all completed successfully. It was not until I ran tests in multiple files that these tests failed. It turned out
chai.use(require('chai-as-promised')) existed in a file loaded prior to tests having
My solution was to create a file that loaded the plugins in the appropriate order and have that run prior to other tests. This is accomplished by naming the file such that it comes first alphabetically. For this scenario, I called the file
test_!initialization.js. It consisted of the following:
// chai-string must be loaded before chai-as-promised as per https://github.com/onechiporenko/chai-string/issues/9.
// If other test files that use(chai-as-promised) then need to use(chai-string) first.
// This file enables that behavior.
var chai = require('chai');