gpt4 book ai didi

javascript - 如何在 Cypress 中使用模块 'fs'?

转载 作者:行者123 更新时间:2023-12-05 00:25:39 24 4
gpt4 key购买 nike

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

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

...
然而 Cypress 发生了这个错误:
enter image description here
这是我的 cypress 项目中的 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, ()=>{
...
})

最佳答案

您应该能够加载您的 profile.json与要求声明。

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

关于javascript - 如何在 Cypress 中使用模块 'fs'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63205147/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com