From 60871111439c1f2d07bd98bf1aba0a21e287e11f Mon Sep 17 00:00:00 2001 From: rezaxdi Date: Wed, 31 Oct 2018 23:46:58 +0330 Subject: [PATCH] fix: writeFile returns string instead JSON (#2373) * Returning obj instead of text If input of writeFile is an object, it should return an object after successful write instead of a string. * Testing returned JSON of writeFile --- packages/driver/src/cy/commands/files.coffee | 4 +++- .../test/cypress/integration/commands/files_spec.coffee | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) 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)