GithubHelp home page GithubHelp logo

stevenmusumeche / jest-next-dynamic Goto Github PK

View Code? Open in Web Editor NEW

This project forked from formidablelabs/jest-next-dynamic

0.0 1.0 0.0 149 KB

Resolve Next.js dynamic import components in Jest tests

License: MIT License

JavaScript 100.00%

jest-next-dynamic's Introduction

Maintenance Status

jest-next-dynamic

Unlike react-loadable, the dynamic import support offered by Next.js does not expose a way to preload the dynamically imported components in Jest’s environment (which mimics a DOM environment). Even if you get a handle on the Loadable component bundled with Next.js, it skips queuing up the components in DOM-like environments. Bummer!

This module can be imported in your test or setup files to make sure any component created with next/dynamic is added to a queue, which can then be flushed with the exported preloadAll function.

Using this, your component tests (including snapshots) can render the full component tree instead of the loading placeholders.

Setup

In order to transform import() successfully in Jest’s environment, you must use a different transform than the one provided by next/babel (which expects to be built with webpack).

Both of these work fine:

{
  "plugins": ["babel-plugin-dynamic-import-node"]
}

Usage

// This will mock `next/dynamic`, so make sure to import it before any of your
// components.
import preloadAll from "jest-next-dynamic";
// This component can have dynamic imports anywhere in its rendered tree,
// including nested dynamic imports.
import SomeComponent from "./SomeComponent";

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

// Your tests here!

Maintenance Status

Experimental: This project is quite new. We're not sure what our ongoing maintenance plan for this project will be. Bug reports, feature requests and pull requests are welcome. If you like this project, let us know!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.