我目前正在制作一个带有打字稿、节点、快递的 API,并使用 jest 和 supertest 进行测试。我在使用 Javascript 时没有问题,但我最近将我的项目文件从 JS 更改为 TS,包括测试文件,当我开始测试时,我在所有测试套件中的超级测试请求部分都收到以下错误,这是其中之一当我开始测试时,我的终端上的测试套件。
TypeError: app.address is not a function
37 | it("should return 400 if email is invalid", async () => {
38 | const res = await request(server)
> 39 | .post("/api/users/auth/register")
| ^
40 | .send({
41 | email: "nomail",
42 | password: "validpassword123",
Run Code Online (Sandbox Code Playgroud)
这是我的测试文件auth.test.ts:
import * as request from 'supertest';
import { User } from '../../../../src/models/User';
import * as mongoose from 'mongoose';
import getKeys from '../../../../src/config/keys';
describe("/api/users/auth", …Run Code Online (Sandbox Code Playgroud) 我目前正在使用 typescript 使用 node-express,我通过 tsconfig.json 文件的 baseUrl 和路径使用绝对导入路径,并使用 ts-node 运行服务器并使用 tsconfig-paths 模块到 ts-node 可以识别我的 tsconfig 绝对路径在运行时。我通过npm start运行服务器没有问题,
但是每当我在 vscode 上以调试模式启动服务器时,它似乎无法解析我的 tsconfig 路径并且抛出错误找不到模块“我的绝对模块路径”。这是我的 tsconfig.json 文件和 package.json 文件
配置文件
{
"compilerOptions": {
"outDir": "./dist",
"moduleResolution": "node",
"sourceMap": true,
"module": "commonjs",
"allowJs": true,
"target": "es5",
"noUnusedParameters": false,
"allowUnreachableCode": true,
"allowUnusedLabels": true,
"lib": [
"es2015"
],
"baseUrl": ".",
"paths": {
"@routes": [
"src/routes/*"
],
"@client": [
"../client/*"
]
},
}
}
Run Code Online (Sandbox Code Playgroud)
package.json - 脚本
"scripts": {
"test": "jest --watchAll --runInBand",
"coverage": …Run Code Online (Sandbox Code Playgroud) visual-studio-debugging typescript tsconfig visual-studio-code
嘿,我正在使用 express 开发 SSR 应用程序。当请求命中时,渲染服务器(在 localhost:3000 上运行)将请求发送到 api 服务器(在 localhost:3001 上运行)以获取初始用户的会话存储。但是在发送请求时,它会在此 loadFromSessionStorage 函数中引发此未知错误
我的路线处理程序的一部分。它在 axios.get 请求上抛出错误
const isServer = (typeof window === 'undefined') || (!window.document);
if (isServer) {
axios.defaults.headers = { cookie: req.get('cookie') || '' };
}
const loadFromSessionStorage = async () => {
try {
const res = await axios.get(`localhost:3001/session`)
if (res) {
return {
user: res.data
}
}
} catch (err) {
if (err.response.data.sessionNotFound) {
return {};
}
console.log("Unknown error from loading session storage")
console.log(err);
return;
// TODO: …Run Code Online (Sandbox Code Playgroud)