Reputation: 35
Problem summary:
I wrote a function called getImagePathsFromCatalog that takes a string representing the location of a catalog (XML) file. It reads product info from the catalog file and produces an array "map"(*) consisting of productIds (key), each with an array of their associated images (value).
In my application, this runs fine and always returns the expected output, which should look something like this (formatted for readability):
[
'TEST-123': [ 'products/TEST-123.jpg' ],
'TEST-12345': [ 'products/Equipment/TEST-12345.jpg',
'products/Equipment/TEST-12345_1.jpg' ]
]
Background Info:
I have tried using various syntax to construct the it block (using async/await, using .then); using return and expect; using various Chai methods (be, should, assert, etc.); assigning the resolve value of the promise to a let for use in the assertion; using done; and some other things I'm sure I'm forgetting.
I have also tried different expectations, including to.be.array, to.be.ok, to.exist, and etc., but all end in the same error.
I have installed and am importing all of the required libraries and functions: chai, chai-as-promised, chai-arrays, chai-asserttype, chai.expect (etc.) and am also "using" them, as in chai.use(chaiAsPromised), etc.
(*) Note: Due to project specs, I have to use an array that behaves more or less like a map, rather than using an actual map object. I know this may cause issues with tests like to.be.array, but it shouldn't prevent me from expecting things like the result existing or coming back non-null (at least I wouldn't think it would?.)
Code:
describe('catalogImages.getImagePathsFromCatalog(catalogFile)', function () {
it('function should return an array', function() {
catalogImages.getImagePathsFromCatalog('../imports/catalog.xml')
.then(catalogImagePathsMap => {
return expect(catalogImagePathsMap).to.be.array();
});
});
});
Expected and actual result summary:
Expected result: Some tests (even just basic assertions about the existence or non-null/non-undefined status of the result) should pass.
Actual result: All tests written for this function return the same error ("cannot read property 'length' of undefined").
I've been researching this error and haven't found a solution that works for me yet. Thank you in advance for any assistance.
Upvotes: 0
Views: 1616
Reputation: 35
Very unexpected, but the issue was actually the file path I was passing-into the function as an argument. The path was referencing the file from the test's location in the file system, but when my coworker suggested doing a console.log(process.cwd()); in the test just to double-check (after trying A LOT of other things that seemed more obvious), we found that the file was behaving as if it was in the root instead of in its subfolder. After switching the path argument to start at the root level rather than the subfolder the test file resides in, the tests started working perfectly. I still don't understand why this is the case, but maybe this will help someone else in the future who is banging their head against a similar Mocha/Chai mystery!
Doesn't work:
describe('catalogImages.getImagePathsFromCatalog(catalogFile)', function () {
it('function should return an array', function() {
catalogImages.getImagePathsFromCatalog('../imports/catalog.xml')
.then(catalogImagePathsMap => {
return expect(catalogImagePathsMap).to.be.array();
});
});
});
Does work:
describe('catalogImages.getImagePathsFromCatalog(catalogFile)', function () {
it('function should return an array', function() {
catalogImages.getImagePathsFromCatalog('./imports/catalog.xml')
.then(catalogImagePathsMap => {
return expect(catalogImagePathsMap).to.be.array();
});
});
});
Upvotes: 0
Reputation: 1639
Looking at the chai expect
documentation, it looks like array expectations are managed through the following syntax:
expect(foo).to.be.an('array');
Can you try this? You may need to update to the latest chai version.
Upvotes: 0