FormidableLabs / jest-next-dynamic

Resolve Next.js dynamic import components in Jest tests
MIT License
69 stars 7 forks source link

TypeError: require.resolveWeak is not a function #11

Open ellioseven opened 4 years ago

ellioseven commented 4 years ago

I am getting the following error when attempting to dynamically load within a babel compiled component:

TypeError: require.resolveWeak is not a function

The compiled contents looks like so:

var AuthMenu = (0, _dynamic.default)(function () {
  return _promise.default.resolve().then(function () {
    return (0, _interopRequireWildcard2.default)(require("@connect/web-auth/lib/container/AuthMenu/AuthMenu"));
  });
}, {
  loadableGenerated: {
    webpack: function webpack() {
      return [require.resolveWeak("@connect/web-auth/lib/container/AuthMenu/AuthMenu")];
    },
    modules: ["@connect/web-auth/lib/container/AuthMenu/AuthMenu"]
  }
});

I am wondering if this package supports this scenario?

JoaoFGuiomar commented 4 years ago

I'm getting the same error after updating to Next 9.1.

But I get different errors when I run test or test --watch

with test Captura de ecrã 2019-12-04, às 10 37 10

with test --watch Captura de ecrã 2019-12-04, às 10 27 14

jeffryang24 commented 4 years ago

I also got error in Next 9.1, but mine is: TypeError: LoadableComponent.preload is not a function. Temporarily, I solved it by mocking next-server/dynamic and next/dynamic. Don't know why the error occurred while in line 35 (index.js) already prevented it. :thinking:

jest.mock('next-server/dynamic', () => {
  return jest.fn(() => 'Dynamic');
});
jest.mock('next/dynamic', () => {
  return jest.fn(() => 'Dynamic');
});