From eafaef6fd9702c3ee96ea6a600ee95852fcbd68f Mon Sep 17 00:00:00 2001 From: Manuel Garcia <31109774+Mandroide@users.noreply.github.com> Date: Fri, 31 Mar 2023 06:36:21 -0400 Subject: [PATCH] Suggest using globalThis property in fixture.mdx --- docs/api/commands/fixture.mdx | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/docs/api/commands/fixture.mdx b/docs/api/commands/fixture.mdx index 42c2c9047d..e32e29d5c7 100644 --- a/docs/api/commands/fixture.mdx +++ b/docs/api/commands/fixture.mdx @@ -248,9 +248,29 @@ the encoding in order to read the file as a ### `this` context -If you store and access the fixture data using `this` test context object, make -sure to use `function () { ... }` callbacks. Otherwise the test engine will NOT -have `this` pointing at the test context. +To store and access the fixture data, prefer to use the global `globalThis` +property over directly the `this` keyword. + +```javascript +describe('User page', () => { + beforeEach(() => { + cy.fixture('user').then((user) => { + // "globalThis" points at global this value + globalThis.user = user + }) + }) + + // the test callback is in "() => { ... }" form + it('has user', () => { + // globalThis.user exists + expect(globalThis.user.firstName).to.equal('Jane') + }) +}) +``` + +If you store and access the fixture data using directly `this` keyword in test +context object, make sure to use `function () { ... }` callbacks. Otherwise +the test engine will NOT have `this` pointing at the test context. ```javascript describe('User page', () => {