Pieter Switten
Pieter Switten

Reputation: 81

jest projects different setupfiles for each project not working

My file structure is something like:

Product/
    |- httpdocs/
        |- jest.config.js
        |- modules/
            |- modules1/
                |- jest.config.js
            |- modules2/
                |- jest.config.js
            |- modules3/
                |- jest.config.js

In each jest.config.js under the modules I have some configuration for that specific module like: setupFiles: ['<rootDir>/testHelpers/setup.js'].

In my top jest.config.js (under httpdocs) I have defined my different projects:

projects: [
    '<rootDir>/modules/module1',
    '<rootDir>/modules/module2',
    '<rootDir>/modules/module3'
]

When I run jest under httpdocs, the tests fail because the setupfiles (and other configuration) are ignored inside my module/jest.config.js files.

When I run jest under module1, all the tests succeed

Am I doing something wrong or does the option projects not work like this?

-------- EDIT 1 --------

So I restructered my configs like @cimenx suggested.

Product/
    |- jest.config.js
    |- httpdocs/
        |- modules/
            |- modules1/
                |- foo/
                    |- bar/
                        |- foobar.js
                        |- foobar.test.js
            |- modules2/
                |- ...

my jest.config.js file looks like this:

module.exports = {
  rootDir: './httpdocs/modules',
  testPathIgnorePatterns: ['/node_modules/', '/smsc/'],
  projects: [
    {
      displayName: 'module1',
      testMatch: ['<rootDir>/module1/*.test.js']
    },
    {
      displayName: 'module2',
      testMatch: ['<rootDir>/module2/*.test.js']
    }
  ]
};

At the moment the issue is that jest cannot find any tests inside my modules.

-------- SOLUTION --------

While I was writing my EDIT 1, I tried some other configs... Apparently my <rootDir> is always the directory where my jest.config.js file is in.

The solution for me was to write the whole path from the config file to the modules without the option rootDir: './some/path'

module.exports = {
    projects: [
        {
          displayName: 'module1',
          testMatch: ['<rootDir>/httpdocs/modules/module1/**/*.test.js']
        },
        {
          displayName: 'module2',
          testMatch: ['<rootDir>/httpdocs/modules/module2/**/*.test.js']
        }
    ]
};

Thanks for the help!

Upvotes: 8

Views: 8661

Answers (1)

DrSensor
DrSensor

Reputation: 496

Probably you should remove all jest.config.js on each modules and rewrite the Product/httpdocs/jest.config.js to be something like this:

module.exports = {
    projects: [
        {
            displayName: "modules1",
            setupTestFrameworkScriptFile: '<rootDir>/modules/modules1/jest.setup.js',
            /*
               Jest config that is specific for modules1
            */
            testMatch: ["<rootDir>/modules/modules1/*.spec.js"],
        },
        {
            displayName: "modules2",
            setupTestFrameworkScriptFile: '<rootDir>/modules/modules2/jest.setup.js',
            /*
              Jest config that is specific for modules2
            */
            testMatch: ["<rootDir>/modules/modules2/*.spec.js"]
        }
    ]
}

Upvotes: 0

Related Questions