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

[Feature] clone jest-next-dynamic (#2) #4

Merged
merged 1 commit into from Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .babelrc.js
@@ -0,0 +1,4 @@
module.exports = {
presets: ["next/babel"],
plugins: ["babel-plugin-dynamic-import-node"]
};
67 changes: 67 additions & 0 deletions .gitignore
@@ -0,0 +1,67 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# Build cache
.cache

# next.js build output
.next

## Test result
__snapshots__
54 changes: 54 additions & 0 deletions index.js
@@ -0,0 +1,54 @@
const mockInitializers = [];

module.exports = function preloadAll() {
if (mockInitializers.length) {
// Copy and empty out `mockInitializers` right away so that any newly
// enqueued components are found in the next pass.
const initializers = mockInitializers.slice();
mockInitializers.length = 0;
// While loading the components in this round of initializers, more
// components may have been dynamically imported, adding more initializers
// we should run and await.
return Promise.all(initializers.map(preload => preload())).then(preloadAll);
} else {
return Promise.resolve();
}
};

function createFactory(moduleName) {
return () => {
const dynamicModule = jest.requireActual(moduleName);
const dynamic = dynamicModule.default;

const mockDynamic = (loader, options) => {
// Remove webpack-specific functionality added by the next/babel preset.
if (loader && typeof loader === "object" && loader.loadableGenerated) {
const { loadableGenerated, ...mockLoader } = loader;
loader = mockLoader;
}
if (options && typeof options === "object" && options.loadableGenerated) {
const { loadableGenerated, ...mockOptions } = options;
options = mockOptions;
}
const LoadableComponent = dynamic(loader, options);
mockInitializers.push(() =>
LoadableComponent.preload
? LoadableComponent.preload()
: LoadableComponent.render.preload());
return LoadableComponent;
};

// Replace the `default` export on the existing module so that other exports
// and non-enumerable properties remain.
dynamicModule.default = mockDynamic;
return dynamicModule;
};
}

// In order to more easily include this feature in shared Jest setups (like
// presets), use `virtual: true` to avoid throwing an error when `next` isn't
// actually installed or it's a version without `next-server` (introduced in
// Next.js 8).
for (const moduleName of ["next/dynamic", "next-server/dynamic"]) {
jest.doMock(moduleName, createFactory(moduleName), { virtual: true });
}
12 changes: 12 additions & 0 deletions index.test.js
@@ -0,0 +1,12 @@
import React from "react";
import TestRenderer from "react-test-renderer";
import preloadAll from "./index";
import NextDynamicNested from "./test/NextDynamicNested";

beforeAll(async () => {
await preloadAll();
});

it("renders nested next/dynamic components", () => {
expect(TestRenderer.create(<NextDynamicNested />).toJSON()).toMatchSnapshot();
});