我正在尝试测试服务器的端点,由于babel错误(或者这就是我的想法),我什至无法运行测试
我有此配置:
babel.config.js:
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
],
};
package.json:
{
"name": "my awesome server",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"start": "babel-node src/server.js",
"dev": "nodemon --exec babel-node src/server.js",
"test": "jest"
},
"author": "",
"license": "ISC",
"dependencies": {
"@truffle/hdwallet-provider": "^1.0.22",
"app-module-path": "^2.2.0",
"dotenv": "^8.0.0",
"express": "^4.16.4",
"morgan": "^1.9.1",
"web3": "^1.2.2"
},
"devDependencies": {
"@babel/core": "^7.6.4",
"@babel/node": "^7.2.2",
"@babel/preset-env": "^7.6.3",
"jest": "^24.9.0",
"nodemon": "^1.19.0",
"supertest": "^4.0.2"
}
}
测试文件:
import app from '../src/app';
import request from 'supertest';
const getAddressTest = async () => {
const response = await request(app)
.get('/address')
expect(response.body.status).toBe(200)
}
const addressEnpoint = test("Address test", getAddressTest)
describe("Check address", () => addressEnpoint)
我的应用主文件:
import express from "express";
import morgan from "morgan";
import dotenv from "dotenv";
import { routes } from "api/routes";
dotenv.config();
const app = express();
const requestHeaders = (_, response, next) => {
response.header("Access-Control-Allow-Origin", "*");
next()
};
const appUse = (a, b) => (b ? app.use(a, b) : app.use(a));
const toUse = [ morgan("combined"), requestHeaders, ...routes ];
toUse.forEach(object => appUse(object));
export default app;
当我运行npm start时,一切进展顺利,但是当我运行npm run test时,出现以下错误:
> jest
FAIL tests/address.test.js
● Test suite failed to run
TypeError: methods.forEach is not a function
> 1 | import express from "express";
| ^
2 | import morgan from "morgan";
3 | import dotenv from "dotenv";
我想知道我在这里想念的是什么-我已经尝试了过去几天在google中找到的所有内容,但是我没有做到这一点
注意:如果我更改应用主文件的第一行以使用require而不是import,则会在我导入Express模块的下一个文件中给我一个错误(表示问题出在Express模块上)-不知道为什么这是发生在