lundi 21 septembre 2020

Jest node typescript mock exported default const

I am trying to add tests to my node server (using typescript) and I'm having a trouble to mock a const that I am exporting as default const.

My exported default const is standing for my configuration:

const buildConfig = ():MyConfig => {...}

const config:MyConfig = buildConfig();

export default config;

at "src/config/config.ts"

and I'm importing this const at every place in my service to get the single instance configuration, and it works great. The configuration is initiated by environment variables.

The problem is when I am trying create tests for my app for functions that are using the imported configuration.

I tried to create a mock for this imported const using the jest.mock or jest.createMockFromModule but nothing works, it just ignoring the mock and try to use the original configuration which doesn't exists.

I wanted to know if someone resolved this problem before and know how to create a mock for a default exported function/const.

Thanks!

Aucun commentaire:

Enregistrer un commentaire