Reputation: 16673
I would like to run a loop over an array with a set of tests including beforeEach
setting up corresponding value, and it
also using the values from the array.
The example below shows the general structure. The problem is myArray
is not recognized when calling forEach
on it (see the line with comment)
let myArray;
beforeAll(() => {
myArray = getArray();
log(myArray) // I can see its defined here
});
myArray.forEach((myValue) => { // myArray has no value here!!!
beforeEach(function() {
// do something with myValue
log(myArray) // I can see its defined here
});
it('my test', () => {
// do something with myValue
log(myArray) // I can see its defined here
});
}); // close loop
There are examples like this to loop over it
, but that doesn't address the scope of the values being looped over
Upvotes: 1
Views: 2772
Reputation: 151
You don't need to use beforeAll
to get data for your array.
You could get data for tests in advance before you define test suite.
let myArray = [1, 2, 3]; //get data for tests
describe('Name of the group', () => {
myArray.forEach(element => {
beforeEach(() => {
console.log('Data set before each: ' + myArray)
});
it('My Test', () => {
console.log('Test#' + element)
});
});
});
Possible to use data driven approach using jasmine-data-provider
const using = require('jasmine-data-provider');
let myArray = [1, 2, 3]; //get data for tests
describe('Name of the group using "using"', () => {
using(myArray, element => {
beforeEach(() => {
console.log('Data set before each: ' + myArray)
});
it('My Test', () => {
console.log('Test#' + element)
});
});
});
Upvotes: 1
Reputation: 2045
You should wrap your myArray.forEach
in one of the beforeEach, it, afterEach, or afterAll
, as beforeAll
is running asynchronously.
Upvotes: 0