Skip to content

Commit eafaef6

Browse files
committed
Suggest using globalThis property in fixture.mdx
1 parent ad556ed commit eafaef6

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

docs/api/commands/fixture.mdx

+23-3
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,29 @@ the encoding in order to read the file as a
248248

249249
### `this` context
250250

251-
If you store and access the fixture data using `this` test context object, make
252-
sure to use `function () { ... }` callbacks. Otherwise the test engine will NOT
253-
have `this` pointing at the test context.
251+
To store and access the fixture data, prefer to use the global `globalThis`
252+
property over directly the `this` keyword.
253+
254+
```javascript
255+
describe('User page', () => {
256+
beforeEach(() => {
257+
cy.fixture('user').then((user) => {
258+
// "globalThis" points at global this value
259+
globalThis.user = user
260+
})
261+
})
262+
263+
// the test callback is in "() => { ... }" form
264+
it('has user', () => {
265+
// globalThis.user exists
266+
expect(globalThis.user.firstName).to.equal('Jane')
267+
})
268+
})
269+
```
270+
271+
If you store and access the fixture data using directly `this` keyword in test
272+
context object, make sure to use `function () { ... }` callbacks. Otherwise
273+
the test engine will NOT have `this` pointing at the test context.
254274

255275
```javascript
256276
describe('User page', () => {

0 commit comments

Comments
 (0)