Skip to content

Commit 5267445

Browse files
authored
Merge pull request #4 from FormidableLabs/fix/next-8
Improve support for both Next 7 and 8
2 parents 610d0ac + f060a52 commit 5267445

File tree

4 files changed

+1216
-1627
lines changed

4 files changed

+1216
-1627
lines changed

.babelrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
module.exports = {
2-
presets: ["@babel/preset-env", "@babel/preset-react"],
2+
presets: ["next/babel"],
33
plugins: ["babel-plugin-dynamic-import-node"]
44
};

index.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,37 @@ module.exports = function preloadAll() {
1515
}
1616
};
1717

18-
jest.doMock(
19-
"next/dynamic",
20-
() => {
21-
const { default: dynamic } = jest.requireActual("next/dynamic");
18+
function createFactory(moduleName) {
19+
return () => {
20+
const dynamicModule = jest.requireActual(moduleName);
21+
const dynamic = dynamicModule.default;
2222

23-
const mockDynamic = (...args) => {
24-
const LoadableComponent = dynamic(...args);
23+
const mockDynamic = (loader, options) => {
24+
// Remove webpack-specific functionality added by the next/babel preset.
25+
if (loader && typeof loader === "object" && loader.loadableGenerated) {
26+
const { loadableGenerated, ...mockLoader } = loader;
27+
loader = mockLoader;
28+
}
29+
if (options && typeof options === "object" && options.loadableGenerated) {
30+
const { loadableGenerated, ...mockOptions } = options;
31+
options = mockOptions;
32+
}
33+
const LoadableComponent = dynamic(loader, options);
2534
mockInitializers.push(() => LoadableComponent.preload());
2635
return LoadableComponent;
2736
};
2837

29-
return mockDynamic;
30-
},
31-
// In order to more easily include this feature in shared Jest setups (like
32-
// presets), use `virtual: true` to avoid throwing an error when `next` isn't
33-
// actually installed.
34-
{
35-
virtual: true
36-
}
37-
);
38+
// Replace the `default` export on the existing module so that other exports
39+
// and non-enumerable properties remain.
40+
dynamicModule.default = mockDynamic;
41+
return dynamicModule;
42+
};
43+
}
44+
45+
// In order to more easily include this feature in shared Jest setups (like
46+
// presets), use `virtual: true` to avoid throwing an error when `next` isn't
47+
// actually installed or it's a version without `next-server` (introduced in
48+
// Next.js 8).
49+
for (const moduleName of ["next/dynamic", "next-server/dynamic"]) {
50+
jest.doMock(moduleName, createFactory(moduleName), { virtual: true });
51+
}

package.json

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@
1010
"test": "jest"
1111
},
1212
"devDependencies": {
13-
"@babel/core": "^7.2.0",
14-
"@babel/preset-env": "^7.2.0",
15-
"@babel/preset-react": "^7.0.0",
13+
"@babel/core": "^7.3.3",
1614
"babel-core": "^7.0.0-bridge",
17-
"babel-jest": "^23.6.0",
15+
"babel-jest": "^24.1.0",
1816
"babel-plugin-dynamic-import-node": "^2.2.0",
19-
"jest": "^23.6.0",
20-
"next": "^7.0.2",
21-
"react": "^16.6.3",
22-
"react-dom": "^16.6.3",
23-
"react-test-renderer": "^16.6.3"
17+
"jest": "^24.1.0",
18+
"next": "^8.0.2",
19+
"react": "^16.8.3",
20+
"react-dom": "^16.8.3",
21+
"react-test-renderer": "^16.8.3"
2422
}
2523
}

0 commit comments

Comments
 (0)