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

Add option to avoid automatic parsing of JSON files in cy.fixture() and cy.readFile() #5395

Open
checketts opened this issue Oct 17, 2019 · 3 comments

Comments

@checketts
Copy link

@checketts checketts commented Oct 17, 2019

Current behavior:

Fixtures with comments throw an error as not being 'valid json'
image

Desired behavior:

Please allow json5 (json with comments) for fixtures. Some fixtures are huge and having comments to point out the critical part would be helpful

Steps to reproduce: (app code and test code)

https://github.com/checketts/cypress-test-tiny.git see https://github.com/checketts/cypress-test-tiny/blob/master/cypress/fixtures/example.json

Versions

Mac OSX using Cypress 3.4.1

@jennifer-shehane

This comment has been minimized.

Copy link
Member

@jennifer-shehane jennifer-shehane commented Oct 22, 2019

Hey @checketts, we do not propose supporting JSON5, as this is not valid JSON and do not intend to support every form of reading in files - and think this is a concern of the user.

ALTHOUGH, there is no way to get around our current JSON validation to read this file. So we do think there should be a way to get around the validation.

Proposal

We propose passing an option to the cy.fixture() (or at the least the cy.readFile() to enable reading of invalid JSON files.

The simplest name I can think of for the option is parse with it's default being true and you could pass in false. Someone else may have a better suggestion though.

Code

The code for cy.fixture() starts here, attempting to lint and parse any files with a .json extension. This is the point where you would say if (options.parse === false) then skip any of the specific calls to parseJson, parseJs, parseCoffee, or parseHtml.

https://github.com/cypress-io/cypress/blob/develop/packages/server/lib/fixture.coffee#L106

Here is the code for cy.readFile() that does the same thing and should be given the same options - parse: false to skip the parsing of files with .json extensions.

https://github.com/cypress-io/cypress/blob/develop/packages/server/lib/files.coffee#L8

Workaround

You could rename your file that is passed to cy.readFile() or cy.fixture() so that it does not have a .json extension (adding a .txt extension) and it will read in the file just fine in our current version of Cypress.

@jennifer-shehane jennifer-shehane changed the title Add support for json5 (json with comments) for fixtures Add option to avoid automatic parsing of JSON files in cy.fixture() and cy.readFile() Oct 22, 2019
checketts pushed a commit to checketts/cypress-test-tiny that referenced this issue Oct 22, 2019
@checketts

This comment has been minimized.

Copy link
Author

@checketts checketts commented Oct 22, 2019

Thanks for the excellent write up and included work around! I've incorporated them and they work (almost) perfectly! See checketts/cypress-test-tiny@0229f75#diff-d344d2966988ae770a3305445156ac3e

By changing my file to .json5 my IDE (WebStorm) still parses it as JSON and allows comments, so I don't lose that functionality and Cypress serves up the files without any error.

However, it appears that my frontend code (Angular) can't parse it, because, as you noted it isn't valid JSON. So I would need to create a command that uses cy.readFile and strip out comments before passing them on. (It also broke Cypress' ability to assert have.property on the response. )

So my final work around is to use a JS object directly: checketts/cypress-test-tiny@e2da55e

Thanks for this great framework!

@fr0

This comment has been minimized.

Copy link

@fr0 fr0 commented Oct 24, 2019

Duplicate of #3138 which was closed but never implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants
You can’t perform that action at this time.