hapijs / hapi

The Simple, Secure Framework Developers Trust

Home Page:https://hapi.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"Missing validator" error on server.validator(joi) using Hapi 21.1.0

fmlukaszmatusik opened this issue · comments

Support plan

  • is this issue currently blocking your project?: YES
  • is this issue affecting a production system?: NO

Context

  • node version: 18.14.2
  • module version: es2020
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): hapi 21.1.0
  • any other relevant information: using joi 17.9.1 and Jest 29.0.0

How can we help?

After upgrading to Hapi 21.1.0 and joi 17.9.1 I'm facing an issue.

I create hapi server like this:

// IMPORT SECTION
import * as Hapi from '@hapi/hapi';
import joi from 'joi';

export async function createServer(host: string, port: number): Promise<Hapi.Server> {
  const server = Hapi.server({ ...options, host, port });
  server.validator(joi);
  await server.register([...basicPlugins]);
  await server.register(additionalPlugins());
  server.route(routes);

  return server;
}

But if I run Jest tests which calls this function and then calls server.initialize() I get:

Missing validator on this line: server.validator(joi).

What's weird is after I run server normally (function from above and then server.start()) all my routes works as expected. Perhaps, there's no problem using Hapi 20. Any idea on how can it be resolved?

Pretty odd! That error occurs when the argument passed to server.validator() is missing/falsy. Could you add some debugging to see the value of joi that you're passing there to server.validator()? Also, does this only happen when you run in jest, or does it also happen when you run this directly?

+  console.log({ joi });
   server.validator(joi);

Does it work better with import * as joi from 'joi'?

@Marsup yes, indeed. Thanks!

Pretty odd! That error occurs when the argument passed to server.validator() is missing/falsy. Could you add some debugging to see the value of joi that you're passing there to server.validator()? Also, does this only happen when you run in jest, or does it also happen when you run this directly?

+  console.log({ joi });
   server.validator(joi);

Only if I run jest tests, but if I run it via npm it works like a charm. Nevertheless, @Marsup code fixes this weird behaviour.

Are you using a different tsconfig with jest?

Not really, this is a main tsconfig.json

{
  "compilerOptions": {
    "target": "es2020",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "declaration": false,
    "noImplicitAny": false,
    "rootDir": "./src",
    "outDir": "build",
    "allowSyntheticDefaultImports": true,
    "removeComments": true,
    "jsx": "react",
    "allowJs": true,
    "noEmit": false,
    "importHelpers": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "skipLibCheck": true,
    "strict": true,
    "resolveJsonModule": true,
    "esModuleInterop": false
  },
  "include": ["src/**/*", "declarations/**/*"],
  "exclude": ["src/**/__mocks__"]
}

That's weird, disabling esModuleInterop should prevent you from using import joi in both runtime and jest, unless you have another bundler on top that takes care of commonjs modules.