如何在赛普拉斯中使用模块“ fs”?

时间:2020-08-01 12:21:23

标签: javascript node.js cypress

我想从本地存储库导入JSON数据。

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

...

但是Cypress发生此错误:

enter image description here

这是我的赛普拉斯项目中的package.json。

{
  "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"
  }
}

我不想使用cy.fixture,因为我需要在“ it测试说明”中使用json中的数据。例如,我要进行这样的测试。

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

1 个答案:

答案 0 :(得分:2)

您应该能够使用require语句加载profile.json

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