gpt4 book ai didi

hapi.js - 是否可以将 supertest 与 hapi 一起使用?

转载 作者:行者123 更新时间:2023-12-02 22:02:58 36 4
gpt4 key购买 nike

我使用的是hapi,而不是express。 super 测试还应该有效吗?

如果是这样,有没有一种快速的方法来更改我的代码以使其运行?

我的测试看起来像这样,基于 documentation :

import tape = require('tape');
const supertest = require('supertest');
const app = require('../../../../src/app');

tape('creates new user in database', function (assert) {
supertest(app)
.get('/ekapi/v1/signup')
.expect(200)
......
});

但它给了我这个错误:

dist/server/app/node_modules/supertest/lib/test.js:55
var addr = app.address();
^

TypeError: app.address is not a function
at Test.serverAddress (/home/rje/projects/ekaya/dist/server/app/node_modules/supertest/lib/test.js:55:18)

这是我的应用程序代码:

app.ts

import './core/externalTypes/imports';
import 'reflect-metadata';
import kernel from './core/inversify.config';
import {IServer} from './core/types/IServer';

let server = kernel.get<IServer>("IServer");
server.start();

服务器.ts

import _ = require('lodash');
import * as hapi from "hapi";
import { injectable, inject } from "inversify";
import { IServer } from "../core/types/IServer";
import { ISignUpRoute } from "../core/types/ISignUpRoute";
import { IRouteHandler } from "../core/types/IRouteHandler";
import { IDatabase } from "../core/types/IDatabase";
import { IConfig } from "../core/types/IConfig";
import { IValidator } from "../core/types/IValidator";

@injectable()
class Server implements IServer
{
_db : IDatabase;
_server: hapi.Server;
_config: IConfig;
_validator: IValidator;
_routes: [IRouteHandler];

constructor(
@inject("IDatabase") db: IDatabase,
@inject("IConfig") config: IConfig,
@inject("IValidator") validator: IValidator,
@inject("ISignUpRoute") signUpRoute: ISignUpRoute)
{
this._db = db;
this._config = config;
this._validator = validator;
this._routes = [signUpRoute];
this._server = new hapi.Server();
this._server.connection({
host: '0.0.0.0',
port: this._config.webservice_port,
routes: { cors: true }
});
this.addRoutes();
}

start() : void
{
this._server.start((err : any) => {
if (err)
throw err;
console.log('Hapi server running at: ', this._server.info.uri);
});
}

addRoutes() : void {
const self = this;
this._routes.map(function(routeHandler : IRouteHandler) {
self._server.route(routeHandler.getRoute());
});
}

}

export = Server;

最佳答案

用 js 编写的用于测试的最小代码示例:

'use strict';

const Hapi = require('hapi');
const should = require('chai').expect
const request = require('supertest')


const server = new Hapi.Server();
server.connection({ port: 3000 });

server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply('Hello, world!');
}
});

describe('server', function() {
it('server', function(done) {
request(server.listener).get('/').expect(200, function(err, resp) {
should(resp.text).equal('Hello, world!')
done()
})
})
})

如果是“hapi”:“^16”按原样工作。

如果是"hapi": "^8.1.0",您需要在请求之前手动运行server.start()

关于hapi.js - 是否可以将 supertest 与 hapi 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37440117/

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