Sobes.tech
Intern — Senior

Adjusting variable scope in tests

livecode

Task condition

In the example, the variable textValue is declared inside the before() hook, but it is later used outside its scope, which leads to a ReferenceError. To fix this issue, the variable should be declared in a broader context, such as within the describe block. After moving the declaration, the variable will be accessible both in the hooks and in the tests themselves.

describe('TEST', async () => {
    let textValue; // declare the variable in the describe scope

    before(async function () {
        textValue = 'Test String'; // initialize it
    });

    after(async function () {
        endTest();
    });

    describe('First test case', async () => {
        it('should update textValue', async () => {
            textValue = 'New string'; // now the variable is accessible
            console.log(textValue); // will output: "New string"
        });
    });
});