kevzettler
kevzettler

Reputation: 5213

node-config Config is `undefined` in typescript jest context?

I have a typescript module that works in dev and production. it uses https://github.com/lorenwest/node-config I am trying to import it into jest to write tests against and it throws an error that indicates the config object is undefined

TypeError: Cannot read property 'get' of undefined
at Object.<anonymous> (src/email/email.service.ts:1877:43)

from

import config from 'config'
console.log('**********CONFIG GET*********', config); // undefined
const sendgridToken: string = config.get('socialApi.value') // throws error

I'd expect config to be defined its the default export of the config module

environment:

node-config version: [email protected]
node-version: 12.13.1
tsc -v
Version 3.7.4
[email protected]

Upvotes: 2

Views: 1666

Answers (2)

Demid Mosko
Demid Mosko

Reputation: 11

You can also enable esModuleInterop in your tsconfig

  "compilerOptions": {
    "esModuleInterop": true,
   }

By default, import config from "config" acts the same as const config = require("config").default, and config has module.exports = ... (ie has no default export).

esModuleInterop wraps module.exports with default.

module.exports = ...
module.exports = { default: module.exports }
// it is pseudo-code

See more here

Upvotes: 1

Shubham
Shubham

Reputation: 779

instead of

import config from 'config'

write this

import * as config from 'config'

Upvotes: 3

Related Questions