I wrote a unit test for HapiJS using lab
beforeEach(async ({ context }) => {
context.server = new Hapi.Server();
await context.server.register(Inert);
});
test('/ rootPath is served', async ({ context }) => {
await context.server.register({
plugin: require('../server/statics'),
options: { rootPath }
});
const res = await context.server.inject({
method: 'GET',
url: '/'
});
expect(res.statusCode).to.equal(200);
});
And I was surprised when the test fails with this error.
Method Set.prototype.add called on incompatible receiver #<Set>
at Set.add (<anonymous>)
at module.exports.internals.Core.registerServer (/home/xxxx/node_modules/hapi/lib/core.js:217:24)
The issue seems to be with the statement to add the server to the instances
.
// /home/xxx/node_modules/hapi/lib/core.js
registerServer(server) {
if (!this.root) {
this.root = server;
this._defaultRoutes();
}
this.instances.add(server); // <-- line 217
}
which was declared to be a set.
this.dependencies = [];
// Plugin dependencies
this.events = new Podium(internals.events);
this.heavy = new Heavy(this.settings.load);
this.instances = new Set(); // <-- line 68
I'm wondering why this error occurs and if I'm using the context in the wrong way. I can of course use a variable in the module scope as a workaround but I thought the context helper was created to avoid having to do that.