mdevino
mdevino

Reputation: 137

How to make jest recognize imports relative to project root directory?

Is it possible to make Jest work requiring modules relative to the root project directory?

For example, here's my project structure:

enter image description here

If I'm on notes.test.js and want to import notes.js, I need to do the following:

const notes = require('../../notes.js');

Is it possible to configure Jest, so the line below would work from notes.test.js?

const notes = require('notes.js');

I'd like to have that, so I could better understand what's being imported. It's okay to figure this out on a small project like that, but I'm wondering how complicated it would be on large projects. I tried to look for an answer on my own but couldn't figure it out.

Upvotes: 2

Views: 865

Answers (1)

mdevino
mdevino

Reputation: 137

After trying a little more, it seems adding this to my package.json did the trick:

  "jest": {
      "moduleFileExtensions": ["js"],
      "moduleDirectories": ["node_modules", "bower_components", "."]
    }

Upvotes: 2

Related Questions