I'm using Jasmine (2.3.2) and Protractor (2.5.1) for my UI tests. I need to load in data and generate tests for each item from that array. I have a function (checkItem
) that creates a new describe
function for each item and checks certain properties on that object. This is a very simplified version of my code and there are other tests that run before these.
If I call checkItem
in a describe
statement, itemsArr
isn't defined yet. So I'm not sure how to get the data loaded and create dynamic tests from that data.
var data = require('get-data'); //custom module here
describe('Test', function() {
var itemsArr;
beforeAll(function(done) {
data.get(function(err, result) {
itemsArr = result; //load data from module
done();
});
})
//error: Cannot read property 'forEach' of undefined
describe('check each item', function() {
itemsArr.forEach(function(item) {
checkItem(item);
});
});
function checkItem (item) {
var itemName = item.name;
describe(itemName, function() {
console.log('describe');
//this doesn't fire when 'should check item' is called
it('should work', function() {
console.log('it');
expect(false).toBeTruthy();
});
});
}
});
UPDATE: When I change my code like this it's the same issue, so maybe there's another way to load data instead of using beforeAll
/beforeEach
beforeAll(function() {
itemsArr = [
{
name: 'apple'
},
{
name: 'orange'
},
{
name: 'banana'
}
]
});