You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(Taking a stab at) adding documentation on `.js` fixture files. Current documentation mentions that `.js` fixture files are possible, but doesn't describe how to use them, leading to issues like cypress-io/cypress#1271 .
I do think the current `.js` fixture support should get a good rethink (as mentioned 4 years ago in that issue) , but in the meantime it should have some documentation on how to use it (or fully remove any mention of it from the documentation, making it "unsupported").
Copy file name to clipboardExpand all lines: content/api/commands/fixture.md
+41Lines changed: 41 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -125,6 +125,47 @@ it('loads the same object', () => {
125
125
})
126
126
})
127
127
```
128
+
#### JavaScript file
129
+
Cypress allows a `.js` file as a fixture, in places where JSON is expected.
130
+
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:
131
+
132
+
```javascript
133
+
let fixtureValue =eval("("+ fileContent +")");
134
+
```
135
+
136
+
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 (`;`).
137
+
138
+
Although it is possible to make complex behaviour through this method, note that fixtures are meant to be "a fixed set of data".
139
+
If you want dynamic responses, a better solution is to use a [`routeHandler` function](/api/commands/intercept#Using-the-routeHandler-function).
140
+
141
+
Note that any valid JSON is valid as a `.js` fixture.
142
+
The advantage is that that "sloppy" JSON is allowed: use single-quotes (or no quotes for keys), trailing commas, and comments.
143
+
144
+
<Alerttype="danger">
145
+
The code in the `.js` fixture gets executed.
146
+
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!
147
+
In general, never use `.js` fixtures, unless you 100% control what is in the file.
148
+
</Alert>
149
+
150
+
##### Examples
151
+
152
+
"Sloppy" JSON `fixture/users.js` with comments
153
+
```javascript
154
+
[
155
+
{
156
+
name:'Bob'+'\u2022', // add a unicode black dot to the name
0 commit comments