Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Preact + Jest: "Could not locate module react mapped as ... node_modules/preact/compat$1" #1552

Closed
davidje13 opened this issue Apr 10, 2020 · 1 comment · Fixed by #1577
Closed
Labels

Comments

@davidje13
Copy link
Contributor

Please try to answer the following questions:

  • What version of Neutrino are you using? 9.1.0
  • Are you trying to use any presets? If so, which ones, and what versions? Preact, Jest
  • Are you using the Yarn client or the npm client? What version? npm 6.14.4
  • What version of Node.js are you using? 13.12.0
  • What operating system are you using? macOS 10.14.6

I am experimenting with converting a codebase from React to Preact. When running the Jest tests, I see the following error:

    Configuration error:
    
    Could not locate module react mapped as:
    /<project-dir>/node_modules/preact/compat$1.
    
    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/^react$/": "/<project-dir>/node_modules/preact/compat$1"
      },
      "resolver": undefined
    }

      at createNoMappedModuleFoundError (node_modules/jest-resolve/build/index.js:540:17)
      at Object.<anonymous> (node_modules/react-router/cjs/react-router.js:5:29)

The dependency node_modules/preact/compat is installed

My suspicion (unconfirmed) is that this is happening due to the trailing $1 in the rewritten module, which is being set by this line: https://github.com/neutrinojs/neutrino/blob/master/packages/jest/src/index.js#L60

Judging by eye, it looks like the rule is expecting a capturing group but the regex doesn't contain one (the actual mapping is coming from https://github.com/neutrinojs/neutrino/blob/master/packages/preact/index.js#L31)

This could also be connected to preact/compat now living in core since preact X


This is easy to replicate in an empty project:

package.json

{
  "scripts": { "test": "jest" },
  "dependencies": { "preact": "^10.4.0" },
  "devDependencies": {
    "@neutrinojs/jest": "^9.1.0",
    "@neutrinojs/preact": "^9.1.0",
    "jest": "^25.3.0",
    "neutrino": "^9.1.0",
    "webpack": "^4.42.1",
    "webpack-cli": "^3.3.11",
    "webpack-dev-server": "^3.10.3"
  }
}

.neutrinorc.js

const jest = require('@neutrinojs/jest');
const preact = require('@neutrinojs/preact');
module.exports = { use: [jest(), preact()] };

jest.config.js

const neutrino = require('neutrino');
module.exports = neutrino().jest();

webpack.config.js

const neutrino = require('neutrino');
module.exports = neutrino().webpack();

src/index.js

import React from 'react'; // should be auto-mapped to preact/compat by config
export default React;

tests/index.spec.js

import x from '../src/index';
it('does something', () => {
  expect(x).not.toBeNull();
});
@edmorley
Copy link
Member

edmorley commented Jun 6, 2020

Hi @davidje13! Sorry for the delayed reply.

This is a bug and will be fixed by #1577.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

2 participants