Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wallaby cannot find jest mapped module relative to <rootDir> in monorepo #1590

Closed
suchipi opened this issue Mar 28, 2018 · 4 comments
Closed

Comments

@suchipi
Copy link

suchipi commented Mar 28, 2018

Issue description or question

I am using wallaby with the jest framework in a monorepo.
My wallaby config file and jest config (in my package.json) both reside in a path deeper than my editor's project root:

packages/
  my-package/
    wallaby.conf.js
    package.json (contains jest config)

I use <rootDir> in my Jest config to refer to packages/my-package, and I have my jest moduleNameMappers referring to content in <rootDir>/...

Jest resolves those properly, but wallaby does not. I suspect (but have not confirmed) that wallaby is using the project root as <rootDir> instead of the location of packages/my-package.

Here is the error I get from wallaby:

Configuration error:

Could not locate module react (mapped as ./../react-compat/index.js)

Please check:

"moduleNameMapper": {
  "/^react$/": "./../react-compat/index.js"
},
"resolver": undefined

I have also tried opening my-package directly in atom as the project root, but I get the same error.

Wallaby.js configuration file

const path = require("path");

module.exports = function(wallaby) {
  return {
    files: [
      "src/**/*.js?(x)",
      "!src/**/*.test.js?(x)",
      "!src/**/*.stories.js?(x)",
    ],

    tests: [
      "src/**/*.test.js?(x)",
    ],

    env: {
      type: "node",
      runner: "node",
      params: {
        env: `NODE_PATH=${path.resolve(__dirname, "..", "..", "node_modules")}`,
      },
    },

    testFramework: "jest",

    compilers: {
      "src/**/*.js?(x)": wallaby.compilers.babel(),
    },
  };
};

Jest config (in package.json)

"jest": {
    "setupTestFrameworkScriptFile": "<rootDir>/src/testHelpers/setup.js",
    "testPathIgnorePatterns": [
      "<rootDir>/node_modules/",
      "<rootDir>/build/"
    ],
    "moduleNameMapper": {
      "^react$": "<rootDir>/../react-compat/index.js",
      "^react/lib/(.*)$": "<rootDir>/../../node_modules/react-15/lib/$1"
    }
  }

Code editor or IDE name and version

Atom v1.24.0
atom-wallaby 1.0.22

OS name and version

macOS Sierra 10.12.6

@ArtemGovorov
Copy link
Member

Wallaby maps <rootDir> to its local cache (with instrumented files), unless the path is pointing to node_modules. However, files/folders that are above wallaby config file level are not copied to the wallaby cache, hence the issue.

Try the config below, it should help:

const path = require("path");

module.exports = function(wallaby) {
  return {
    files: [
      "src/**/*.js?(x)",
      "!src/**/*.test.js?(x)",
      "!src/**/*.stories.js?(x)",
    ],

    tests: [
      "src/**/*.test.js?(x)",
    ],

    env: {
      type: "node",
      runner: "node",
      params: {
        env: `NODE_PATH=${path.resolve(__dirname, "..", "..", "node_modules")}`,
      },
    },

    testFramework: "jest",

    compilers: {
      "src/**/*.js?(x)": wallaby.compilers.babel(),
    },
+   setup: function(wallaby) {
+     const config = require('./package.json').jest;
+     Object.keys(config.moduleNameMapper).forEach(k => (config.moduleNameMapper[k] = config.moduleNameMapper[k].replace('<rootDir>', wallaby.localProjectDir)))
+     wallaby.testFramework.configure(config);
+   }
  };
};

@suchipi
Copy link
Author

suchipi commented Mar 29, 2018

This works, thank you!

@suchipi suchipi closed this as completed Mar 29, 2018
@csi-lk
Copy link

csi-lk commented Apr 2, 2018

Not to re-open but I just wanted to point out I had the same issue, may be related to a wallaby update?

(was working fine previously)

Cheers

@ArtemGovorov
Copy link
Member

@csi-lk Yep, the issue is related to a recent update (that was few weeks ago). We'll investigate the ways to fix if (to work without the suggested <rootDir>) for this scenario (wallaby config residing deeper than project's root).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants