Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest using globalThis property in fixture.mdx #5159

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions docs/api/commands/fixture.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down