最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cypress-compare file contents with an array always return false - Stack Overflow

programmeradmin1浏览0评论

I am new to java script and cypress. I am reading contents from a file using cy.readFile mand and pare it with another array(which contains run time values). My intention is to pare values in baseline file and run time values.I tried to use below assertion but it failing.

cy.readFile('Latest.txt').should('eq',tableValues1); 

this assertion fails-console Output shows like below- as you can see the contents in expected and actual are same -something with the format -can any one give me a hint.

Actual: [
"Gender",
"Age group ",
"Source Total ",
"21 to 30 ",
"30 to 35 ",
"36 to 40 ",
"41 to 45 ",
"46 to 50 ",
"51 to 55 ",
"56 to 60 ",
"61 to 65 ",
"Over 66 ",
"123",
 "%",
"%",
"%",
"%",
"%",
"%",
"%",
"%",
"%"
]

  cypress_runner.js:163813 
Expected:  (22) ["Gender", "Age group ", "Source Total ", "21 to 30 ", "30 to 35 ", "36 to 40 ", "41 to 45 ", "46 to 50 ", "51 to 55 ", "56 to 60 ", "61 to 65 ", "Over 66 ", "123", "%", "%", "%", "%", "%", "%", "%", "%", "%"]

I have also tried to pare like

tableValues1==cy.readFile('Latest.txt');

this also return false

I am new to java script and cypress. I am reading contents from a file using cy.readFile mand and pare it with another array(which contains run time values). My intention is to pare values in baseline file and run time values.I tried to use below assertion but it failing.

cy.readFile('Latest.txt').should('eq',tableValues1); 

this assertion fails-console Output shows like below- as you can see the contents in expected and actual are same -something with the format -can any one give me a hint.

Actual: [
"Gender",
"Age group ",
"Source Total ",
"21 to 30 ",
"30 to 35 ",
"36 to 40 ",
"41 to 45 ",
"46 to 50 ",
"51 to 55 ",
"56 to 60 ",
"61 to 65 ",
"Over 66 ",
"123",
 "%",
"%",
"%",
"%",
"%",
"%",
"%",
"%",
"%"
]

  cypress_runner.js:163813 
Expected:  (22) ["Gender", "Age group ", "Source Total ", "21 to 30 ", "30 to 35 ", "36 to 40 ", "41 to 45 ", "46 to 50 ", "51 to 55 ", "56 to 60 ", "61 to 65 ", "Over 66 ", "123", "%", "%", "%", "%", "%", "%", "%", "%", "%"]

I have also tried to pare like

tableValues1==cy.readFile('Latest.txt');

this also return false

Share Improve this question edited Feb 25, 2019 at 1:46 user8710571 asked Feb 25, 2019 at 1:40 user8710571user8710571 4552 gold badges14 silver badges25 bronze badges 1
  • 1 If tableValues1 is a string, you need to parse it as well as the fixture file. cy.readFile('Latest.txt').then(txt => JSON.parse(txt)).should('deep.eq', JSON.parse(tableValues1)); The parsing removes differences that can be ignored for testing purposes, such as newlines and spaces. – Richard Matsen Commented Feb 25, 2019 at 19:19
Add a ment  | 

2 Answers 2

Reset to default 3

1) eq checks object references. You want deep.eq to assert contents of an object/array are equal.

2) like NoriSte mentioned below, you need to parse the .txt into a javascript object:

cy.readFile('example.txt')
  .then(text => JSON.parse(text))
  .should('deep.eq', tableValues1)

or let cypress do it for you (rename the file to .json):

cy.readFile('example.json')
  .should('deep.eq', tableValues1)

I made a test locally and:

  • rename your Latest.txt file into Latest.json so we can leverage a Cypress feature that automatically parses the file
  • the file content must be
[
 "Gender",
 "Age group ",
 "Source Total ",
 "21 to 30 ",
 "30 to 35 ",
 "36 to 40 ",
 "41 to 45 ",
 "46 to 50 ",
 "51 to 55 ",
 "56 to 60 ",
 "61 to 65 ",
 "Over 66 ",
 "123",
 "%",
 "%",
 "%",
 "%",
 "%",
 "%",
 "%",
 "%",
 "%"
]

that is a valid JSON file (you can check it pasting it on a validator)

Now that we're sure that there aren't any decoding issue etc. (because Cypress automatically converts the JSON file into a JavaScript object) we can pare them.

Anyway: Cypress will still tell you that they aren't equal but it's not a big issue, other testing libraries (Jest etc.) sometimes fail making parisons like yours. All you have to do is to convert both the objects to a baseline JSON string and pare them.

Try that

cy.readFile('example.json').then(json => JSON.stringify(json)).should('eq',JSON.stringify(tableValues1));

where

cy.readFile('example.json') // reads the file as expected
.then(json => JSON.stringify(json)) // once read, it converts the file into a JSON string
.should('eq', JSON.stringify(tableValues1)); // and pare it to the stringified version of your array

It works for me locally and you can find it working on my GitHub repository, let me know if you need something more

发布评论

评论列表(0)

  1. 暂无评论