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

javascript - How to use a module 'fs' in Cypress? - Stack Overflow

programmeradmin2浏览0评论

I want to import JSON data from my local repository.

var fs = require('fs');
var data = fs.readFileSync('./profile.json', 'utf8');
console.log(data);

...

However Cypress occurs this error:

Here is my package.json from my cypress project.

{
  "name": "cypress_test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "npx cypress run",
    "clean-reports": "rm -rf cypress/reports",
    "merge-report": "npx mochawesome-merge --reportDir cypress/reports/separate-reports cypress/reports/full_report.json",
    "generate-report": "npx mochawesome-report-generator --reportDir cypress/reports cypress/reports/full_report.json",
    "after:tests": "npm run merge-report; npm run generate-report",
    "cypress": "npm run clean-reports; npm run test; npm run after:tests"
  },
  "keywords": [],
  "type": "module",
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@cypress/webpack-preprocessor": "^5.4.1",
    "cypress": "^4.11.0",
    "fs": "0.0.1-security",
    "mocha": "5.2.0",
    "mochawesome": "4.1.0",
    "mochawesome-merge": "2.0.1",
    "mochawesome-report-generator": "4.0.1",
    "papaparse": "^5.2.0",
    "selenium-webdriver": "^4.0.0-alpha.7",
    "xlsx": "^0.16.4"
  },
  "devDependencies": {
    "@babel/core": "^7.11.0",
    "@babel/preset-env": "^7.11.0",
    "babel-loader": "^8.1.0",
    "webpack": "^4.44.1"
  }
}

I don't want to use cy.fixture because I need to use data from json in my 'it test discription'. For example, I want to make tests like this.

it (datas.data, ()=>{
   ...
})

I want to import JSON data from my local repository.

var fs = require('fs');
var data = fs.readFileSync('./profile.json', 'utf8');
console.log(data);

...

However Cypress occurs this error:

Here is my package.json from my cypress project.

{
  "name": "cypress_test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "npx cypress run",
    "clean-reports": "rm -rf cypress/reports",
    "merge-report": "npx mochawesome-merge --reportDir cypress/reports/separate-reports cypress/reports/full_report.json",
    "generate-report": "npx mochawesome-report-generator --reportDir cypress/reports cypress/reports/full_report.json",
    "after:tests": "npm run merge-report; npm run generate-report",
    "cypress": "npm run clean-reports; npm run test; npm run after:tests"
  },
  "keywords": [],
  "type": "module",
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@cypress/webpack-preprocessor": "^5.4.1",
    "cypress": "^4.11.0",
    "fs": "0.0.1-security",
    "mocha": "5.2.0",
    "mochawesome": "4.1.0",
    "mochawesome-merge": "2.0.1",
    "mochawesome-report-generator": "4.0.1",
    "papaparse": "^5.2.0",
    "selenium-webdriver": "^4.0.0-alpha.7",
    "xlsx": "^0.16.4"
  },
  "devDependencies": {
    "@babel/core": "^7.11.0",
    "@babel/preset-env": "^7.11.0",
    "babel-loader": "^8.1.0",
    "webpack": "^4.44.1"
  }
}

I don't want to use cy.fixture because I need to use data from json in my 'it test discription'. For example, I want to make tests like this.

it (datas.data, ()=>{
   ...
})
Share Improve this question edited Aug 1, 2020 at 12:54 halfer 20.4k19 gold badges108 silver badges201 bronze badges asked Aug 1, 2020 at 12:21 loone96loone96 8493 gold badges16 silver badges27 bronze badges 5
  • 4 If you want to use node commands like fs.readFileSync within a Cypress test you must put them in a task, see first example in the docs. But why do you not use cy.fixture()? – user12697177 Commented Aug 2, 2020 at 9:39
  • agree with @MarionMorrison , why are u not using fixtures here ? – Karan Commented Aug 2, 2020 at 11:05
  • @MarionMorrison as far as I know fixture's data can only be accessed in testing area(it, specify). I wanted to use data in the title of each tests. For example, it('this test is'+data.test_name, () => ...). – loone96 Commented Aug 2, 2020 at 12:31
  • 1 You can run cy.fixture() in a before() at the top of the spec. – user12697177 Commented Aug 4, 2020 at 21:28
  • There is also cy.readFile(), the difference is cy.fixture() will parse the JSON to an object but cy.readFile() will give you the file contents as a string (same as fs.readFileSync() if used in a task). – user12697177 Commented Aug 4, 2020 at 21:38
Add a comment  | 

3 Answers 3

Reset to default 7

You should be able to load your profile.json with the require statement.

const data = require('profile.json');

To use fs, or various other Node modules, you must put it in a plugin. From https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests#Plugins-file:

While the Cypress tests execute in the browser, the plugins file runs in the background Node process, giving your tests the ability to access the file system and the rest of the operating system by calling the cy.task() command

This is alluded to in the comments above, but I missed it there so I want to make it clear.

This is quite an old thread, but there is now the possibility to use the package cypress-fs (which I am the author of) like so:

cy.fsReadFile(path, options)

The package is internally using the fs module, it requires you to register some support file in your cypress.config.ts. Steps to do so are included in the documentation.

发布评论

评论列表(0)

  1. 暂无评论