Reputation: 1658
I am following the HapiJS tutorial to create a server and using unit test.
'use strict'
const Hapi = require('@hapi/hapi')
module.exports = async (host, port, router) => {
if (!host) {
const error = new Error('Host missed')
console.error(error)
process.exit(1)
}
if (!port) {
const error = new Error('Port missed')
console.error(error)
process.exit(1)
}
const server = Hapi.server({
host,
port,
compression: {
minBytes: 2048
},
router: {
isCaseSensitive: false
},
state: {
isSameSite: false, // change later
}
})
server.route(router)
await server.start()
console.log('Server running on %s', server.info.uri)
}
This is my server.js file. I create an export function who will be called at index.js. This function has three parameters. The first parameter is the host, the second is the port, and the third is an unstructured object with routes. Apparently it works fine.
Here is an example of my routes:
const users = [
{
method: 'GET',
path: '/users',
handler: (req, h) => {
return 'Hello World'
}
},
{
method: 'POST',
path: '/users',
handler: (req, h) => {
return 'Hello World'
}
},
{
method: 'PUT',
path: '/users/:id',
handler: (req, h) => {
return 'Hello World'
}
},
{
method: 'DELETE',
path: '/users/:id',
handler: (req, h) => {
return 'Hello World'
}
}
]
module.exports = users
and this is the handler:
const users = require('./components/users/network')
const articles = require('./components/articles/network') // This one is very similar to users
module.exports = [...users, ...articles]
This is the test I create to verify if server initialization is working:
'use strict';
const Lab = require('@hapi/lab');
const { expect } = require('@hapi/code');
const { beforeEach, describe, it } = exports.lab = Lab.script();
const init = require('../../src/server');
const routes = require('../../src/router')
describe('GET /users', () => {
let server;
beforeEach(async () => {
server = await init('localhost', 8080, routes);
});
console.log(server)
it('responds with 200', async () => {
const res = await server.inject({
method: 'get',
url: '/users'
});
expect(res.statusCode).to.equal(200);
});
});
But I get the following output error:
lab
undefined Server running on http://localhost:8080
x
Failed tests:
GET /users responds with 200:
Cannot read property 'inject' of undefined
at C:\Users\diesa\Dropbox\My PC (LAPTOP-21VAS36L)\Desktop\kemus-blog\test\server\init.test.js:19:34 at Immediate. (C:\Users\diesa\Dropbox\My PC (LAPTOP-21VAS36L)\Desktop\kemus-blog\node_modules@hapi\lab\lib\runner.js:661:35) at processImmediate (internal/timers.js:461:21)
1 of 1 tests failed Test duration: 59 ms Leaks: No issues
npm ERR! Test failed. See above for more details.
Why is the variable server undefined? I thought it was a node issue regarding asynchrony, but no.
Upvotes: 0
Views: 531
Reputation: 5289
Your init function is not returning a value. I think you want to change it to return server.
await server.start();
console.log('Server running on %s', server.info.uri);
return server;
Upvotes: 1