Reputation: 1769
I have a spec file:
export class StaticClass() {
public static doSomething(): Promise<any> {
public static id = randomString(8); //makes a random from letters
}
}
it('should execute n times', done => {
for (let i=0; i<20; i++) {
console.log(i);
StaticClass.doSomething(parameter).then((done) => {
let id = StaticClass.id;
console.log(id);
expect(id).toBeSomething(); //not important here
done();
});
}
afterEach(()=> console.log('after each'));
});
Problem is that I get id which is always the same and the test passes immediately.
Output is:
0
1
2
..
19
edbcjiia
edbcjiia
edbcjiia
..
19 times the same string
afterEach
I want to check something with the string generated by Static Helper class. Maybye use RxJs inteval somehow ?
Upvotes: 0
Views: 39
Reputation: 276596
You're calling done
in every then
of the function (which executes multiple times). It doesn't make a lot of sense if you want to wait for all the actions. You're also overriding done
with the then
parameter which makes no sense;
Instead, you should wait for all the requests:
it('should execute n times', done => {
var p = Promise.resolve(); // create chain
for (let i=0; i<20; i++) {
p = p.then(() => { // only execute after the last one is done.
return StaticClass.doSomething(parameter).then(() => {
expect(StaticClass.id).toBeSomething(); //not important here
});
}); // then returns a new promise and waits for the internal promise
}
afterEach(()=> console.log('after each'));
Upvotes: 1