Node.js package.json中的Mocha测试路径参数
注意:我是一个彻头彻尾的傻瓜。请用这样一种方式解释您的答案:有其他编程语言经验但创建了第一个nodejs应用程序的人可以理解=) 我正在尝试编写一个简单的测试应用程序,它应该根据OpenAPI3.0规范自动测试外部服务 我收集了一些示例代码,试图根据使用mocha和chai实现API的外部服务自动测试OpenApi规范 现在我的问题似乎是找不到我的摩卡测试模块 我收到以下错误消息:Node.js package.json中的Mocha测试路径参数,node.js,mocha.js,chai,chai-http,Node.js,Mocha.js,Chai,Chai Http,注意:我是一个彻头彻尾的傻瓜。请用这样一种方式解释您的答案:有其他编程语言经验但创建了第一个nodejs应用程序的人可以理解=) 我正在尝试编写一个简单的测试应用程序,它应该根据OpenAPI3.0规范自动测试外部服务 我收集了一些示例代码,试图根据使用mocha和chai实现API的外部服务自动测试OpenApi规范 现在我的问题似乎是找不到我的摩卡测试模块 我收到以下错误消息: > client_test@1.0.0 test /Users/user1/dev/app/app-rt/c
> client_test@1.0.0 test /Users/user1/dev/app/app-rt/client_test
> mocha ./test/**/*.test.js
/Users/user1/dev/app/app-rt/client_test/node_modules/yargs/yargs.js:1163
else throw err
^
Error: The "path" argument must be an absolute filepath
My package.json:
{
"name": "client_test",
"version": "1.0.0",
"description": "Client Tester",
"main": "index.js",
"scripts": {
"test": "mocha ./test/**/*.test.js"
},
"license": "UNLICENSED",
"dependencies": {
"chai-http": "^4.3.0",
"chai-openapi-response-validator": "^0.2.4",
"mocha": "^6.2.0",
"nock": "^11.3.2"
}
}
test/client_all.test.js中的小测试应用程序:
// Set up Chai
const chai = require('chai');
const expect = chai.expect;
// Import this plugin
const chaiResponseValidator = require('chai-openapi-response-validator');
const baseUrl = 'http://localhost:8081';
// Load an OpenAPI file (YAML or JSON) into this plugin
chai.use(chaiResponseValidator('./spec/app.json'));
// Get an HTTP response using chai-http
chai.use(require('chai-http'));
// Write your test (e.g. using Mocha)
describe('GET /zones', function() {
it('should satisfy OpenAPI spec', async function() {
const res = chai.request(baseUrl).get('/zones');
expect(res.status).to.equal(200);
// Assert that the HTTP response satisfies the OpenAPI spec
expect(res).to.satisfyApiSpec;
});
});
你能帮我找出为什么路径无法解析,以及如何修复它吗?如果您认为我做错了,也可以随意评论测试代码 问题出在openapi响应验证程序包
上。试着这样做:
// Import this plugin
const chaiResponseValidator = require('chai-openapi-response-validator');
const baseUrl = 'http://localhost:8081';
// New code
const path = require('path')
const specPath = path.resolve('./spec/app.json')
// Load an OpenAPI file (YAML or JSON) into this plugin
chai.use(chaiResponseValidator(specPath));
确保app.json
文件的路径相对于package.json
文件,或者使用其他方法将其转换为绝对路径