Create-react-app: Code spliting with webpack's require.ensure can't pass test

Created on 20 Nov 2016  路  11Comments  路  Source: facebook/create-react-app

I want to learn write test in my app that build with create-react-app, but run npm test cause error:

6c74cbaf-cc4e-43df-91be-75674ce71c9e

underlying tools

Most helpful comment

require.ensure is not part of CommonJS so I'm not interested in supporting it officially. The transformer (a custom version of babel-jest, which create-react-app is already using) could return something like require.ensure = (deps, cb) => cb(require); ${source} and that would work and I wouldn't mind supporting some kind of feature that makes this easier to integrate but I would probably not be happy shipping this by default.

All 11 comments

The routes.js write like this:

export const routes = [
  {
    name: 'cool store',
    path: '/',
    exactly: true,
    load: asyncLoad((cb) => {
      require.ensure([], (require) => {
        cb(require('pages/home'))
      })
    }),
  },
  {
    name: 'login',
    path: '/login',
    exactly: true,
    load: asyncLoad((cb) => {
      require.ensure([], (require) => {
        cb(require('pages/login'))
      })
    }),
  },
]

You can probably "polyfill" it in src/setupTests.js.
It's not a real Node feature (another reason we don't document it yet).
So you need to somehow make it work in Node.

If memory serves me correctly to solve this issue I had to polyfill require.ensure within each module that used the feature.

Something along the lines of...

if (!require.ensure) {
  require.ensure = (deps, cb) => cb(require);
}

If memory serves me correctly to solve this issue I had to polyfill require.ensure within each module that used the feature.

Wouldn't putting

require.ensure = (deps, cb) => cb(require);

in src/setupTests.js solve this?

Longer term, we will support this out of the box if dynamic import advances.

Create src/setupTests.js and add these lines:

require.ensure = (deps, cb) => cb(require);

But didn't work :(

Put these lines to routes.js:

if (process.env.NODE_ENV === 'test') {
  require.ensure = (deps, cb) => cb(require);
  window.matchMedia = window.matchMedia || function() {
    return {
        matches : false,
        addListener : function() {},
        removeListener: function() {}
    };
  };
  window.Moltin = function(){}
}

pass test :)

image

Wouldn't putting

require.ensure = (deps, cb) => cb(require);

in src/setupTests.js solve this?

We would have hoped so, but I think the require instance that is provided to each module is unique.

Oh that鈥檚 interesting, thanks for explaining.
I wonder if we can just hack support for require.ensure into Jest itself. cc @cpojer

require.ensure is not part of CommonJS so I'm not interested in supporting it officially. The transformer (a custom version of babel-jest, which create-react-app is already using) could return something like require.ensure = (deps, cb) => cb(require); ${source} and that would work and I wouldn't mind supporting some kind of feature that makes this easier to integrate but I would probably not be happy shipping this by default.

@suhaotian @ctrlplusb Would you be interested in sending a PR based on what @cpojer suggested?

@gaearon Sure thing, I'll give it a bash this w/end.

Closing this because require.ensure is no longer a supported behavior in master. Please switch to import() when 0.10 is released.
Since this has been a longstanding issue, I see no immediate need to mitigate this (it would have to be in a 0.9.x release anyway).

Feel free to re-open and tag as 0.9.x, @gaearon.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dualcnhq picture dualcnhq  路  3Comments

alleroux picture alleroux  路  3Comments

adrice727 picture adrice727  路  3Comments

fson picture fson  路  3Comments

ap13p picture ap13p  路  3Comments