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

Adding documentation on .js fixture files #4324

Open
wants to merge 1 commit 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
41 changes: 41 additions & 0 deletions content/api/commands/fixture.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,47 @@ it('loads the same object', () => {
})
})
```
#### JavaScript file
Cypress allows a `.js` file as a fixture, in places where JSON is expected.
The contents of this file are treated as a something that evaluates to a JavaScript object (within Cypress, the content of the file is `eval()`-ed, with parentheses:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor grammar fix: remove the article "a" from "treated as a something" -> "treated as something"

Spotted by Graphite Reviewer

Is this helpful? React 👍 or 👎 to let us know.


```javascript
let fixtureValue = eval("(" + fileContent + ")");
```

The `.js` file should evaluate to a JavaScript object (see examples below); many things that are valid JavaScript, are not allowed in this file, specifically, the file should _not_ end in a semi-colon (`;`).

Although it is possible to make complex behaviour through this method, note that fixtures are meant to be "a fixed set of data".
If you want dynamic responses, a better solution is to use a [`routeHandler` function](/api/commands/intercept#Using-the-routeHandler-function).

Note that any valid JSON is valid as a `.js` fixture.
The advantage is that that "sloppy" JSON is allowed: use single-quotes (or no quotes for keys), trailing commas, and comments.

<Alert type="danger">
The code in the `.js` fixture gets executed.
If you import your fixtures (or values in your fixtures) from an external source, into fixture files with a `.js` extension, this may be a security problem!
In general, never use `.js` fixtures, unless you 100% control what is in the file.
</Alert>

##### Examples

"Sloppy" JSON `fixture/users.js` with comments
```javascript
[
{
name: 'Bob' + '\u2022', // add a unicode black dot to the name
age: 25,
}
]
```

Generate many Bobs `fixture/users.js`
```javascript
Array(100).fill(0).map((_, index) => ({
name: `Bob_${index}`,
age: 25 + Math.floor(index / 2),
}))
```

### Images

Expand Down