diff --git a/packages/driver/src/cy/commands/files.coffee b/packages/driver/src/cy/commands/files.coffee index 7c80d6cc7502..6578adbc4d88 100644 --- a/packages/driver/src/cy/commands/files.coffee +++ b/packages/driver/src/cy/commands/files.coffee @@ -94,13 +94,15 @@ module.exports = (Commands, Cypress, cy, state, config) -> }) if _.isObject(contents) + objContents = contents contents = JSON.stringify(contents, null, 2) Cypress.backend("write:file", fileName, contents, _.pick(options, ["encoding", "flag"])) .then ({ contents, filePath }) -> consoleProps["File Path"] = filePath consoleProps["Contents"] = contents - + if objContents? + return objContents return contents .catch Promise.TimeoutError, (err) -> $utils.throwErrByPath "files.timed_out", { diff --git a/packages/driver/test/cypress/integration/commands/files_spec.coffee b/packages/driver/test/cypress/integration/commands/files_spec.coffee index 82fc7e64e4cf..b767fbcb5369 100644 --- a/packages/driver/test/cypress/integration/commands/files_spec.coffee +++ b/packages/driver/test/cypress/integration/commands/files_spec.coffee @@ -291,6 +291,12 @@ describe "src/cy/commands/files", -> cy.writeFile("foo.txt", "contents").then (subject) -> expect(subject).to.equal("contents") + + it "sets a JSON as the subject", -> + Cypress.backend.resolves(okResponse) + + cy.writeFile("foo.json", { name: "Test" }).then (subject) -> + expect(subject.name).to.equal("Test") it "can write a string", -> Cypress.backend.resolves(okResponse)