In Cypress you can save values under aliases which is pretty handy. You can get the aliased value using the cy.get, or by using a cy.then(function () { ... }) callback, or (my favorite solution) using the alias in the next hook or test callback via this.[alias] property
1 | beforeEach(() => { |
Using the function () { ... } rather than () => { ... } is very important; we need the this reference to point at the Mocha context object where Cypress aliases are stored.
All is good, except types; our linter / code editor does not know that this test context is expected to have a new custom property answer, what do we do?

If we look at the type definition for the it(title, ...) function, we can find the Mocha types bundled with Cypress


The type for the callback function Func is simply
1 | /** |
The first argument is this: Context and that is what we need to extend with our custom property answer. Unfortunately, it is not exposed, so we cannot simply merge interfaces (like we do with custom Cypress commands), so we need to overwrite the type Func signature.
1 | declare namespace Mocha { |
By putting the Mocha.MyContext interface in the spec file we restrict its effect on the current spec. Now it should work

Strong typing for the win.