Throughout my test cases, I store data in a variable to be used consistently. The variable maintains its value until the very end of the test, but when trying to access it in the @afterEach teardown function for global clean up, it appears empty. It seems like the variable is unexpectedly reset.
I need the stored data to be accessible even if the test case fails before completion, hence the desire to access it in the teardown phase. How can I ensure the variable retains its value in the Cypress test teardown?
I attempted using a static variable within a class, but that solution did not work as expected.
In each test case, a method stores user data and locks it in a session to prevent interference from parallel running tests until it is released.
class myUser {
static user;
public setUser(params: object): void {
myUser.user = setAndLockUser(params);
}
public releaseLockedUser(): void {
if (myUser.user == undefined) return;
releaseTheUserLock(myUser.user);
}
}
At the conclusion of the test case, releasing the user session is essential to make the user available for subsequent test cases. To achieve this, we include releaseUser
in the afterEach
function:
afterEach(() => {
myUser.releaseLockedUser();
});
The issue arises when the stored variable is not being recognized correctly during the afterEach teardown. While the user variable is stored successfully throughout the test, it appears empty during the teardown process causing the session clearance to fail.