{"id":21095294,"url":"https://github.com/60frames/jestpack","last_synced_at":"2025-05-16T14:34:02.626Z","repository":{"id":57280543,"uuid":"41977567","full_name":"60frames/jestpack","owner":"60frames","description":"Jest Webpack Integration","archived":false,"fork":false,"pushed_at":"2016-03-31T21:37:06.000Z","size":63,"stargazers_count":66,"open_issues_count":7,"forks_count":3,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-21T18:52:35.474Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/60frames.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-09-05T22:22:32.000Z","updated_at":"2022-08-28T05:54:37.000Z","dependencies_parsed_at":"2022-09-19T15:51:04.746Z","dependency_job_id":null,"html_url":"https://github.com/60frames/jestpack","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/60frames%2Fjestpack","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/60frames%2Fjestpack/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/60frames%2Fjestpack/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/60frames%2Fjestpack/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/60frames","download_url":"https://codeload.github.com/60frames/jestpack/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254547103,"owners_count":22089262,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-19T22:25:18.697Z","updated_at":"2025-05-16T14:34:02.353Z","avatar_url":"https://github.com/60frames.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Jestpack [![Build Status](https://travis-ci.org/60frames/jestpack.svg?branch=master)](https://travis-ci.org/60frames/jestpack) [![npm version](https://badge.fury.io/js/jestpack.svg)](https://badge.fury.io/js/jestpack)\n\nUnfortunately [Jest doesn't play nicely with Webpack](http://stackoverflow.com/questions/31547587/testing-webpack-built-react-components-with-jest), especially when using some of Webpack's more useful features such as [loaders](http://webpack.github.io/docs/loaders.html) or [code splitting](http://webpack.github.io/docs/code-splitting.html).\n\nJestpack attempts to solve this problem by ~~extending~~ replacing Jest's default module loader to support Webpack's internal module system.\n\n## Installation\n\n`npm install jestpack --save-dev`\n\n\u003e NOTE: Jestpack \u003e=0.2.0 depends on Node \u003e=5.x.x.\n\n\u003e NOTE: Jestpack declares both `jest-cli` and `webpack` as peer dependencies meaning you must declare them both as either `devDependencies` or `dependencies` in your projects `package.json`.\n\n\u003e NOTE: Jestpack doesn't currently support Jest 0.8.x https://github.com/60frames/jestpack/issues/12\n\n## Setup\nJestpack works by supplying pre-built test files to Jest so the first thing you'll want to do is tell Jest where it can expect to find your soon-to-be-bundled test files:\n\n```js\n// package.json\n{\n    ...\n    \"jest\": {\n        \"testPathDirs\": [\"\u003crootDir\u003e/__bundled_tests__\"]\n    }\n}\n```\n\nThen you'll need to get Jest to use the Jestpack module loader:\n\n```js\n// package.json\n{\n    ...\n    \"jest\": {\n        ...\n        \"moduleLoader\": \"\u003crootDir\u003e/node_modules/jestpack/ModuleLoader\",\n    }\n}\n```\n\nNow you're ready to setup your Webpack config by first specifiying the output directory:\n\n```js\n// webpack.config.js\nmodule.exports = {\n    output: {\n        path: '__bundled_tests__',\n        filename: '[name].js'\n    }\n}\n```\n\nAnd then getting Webpack to build each test as a separate entry point:\n\n```js\n// webpack.config.js\nmodule.exports = {\n    ...\n    entry: {\n        'src/__tests__/test1': './src/__tests__/test1',\n        'src/__tests__/test2': './src/__tests__/test2',\n        'src/__tests__/test3': './src/__tests__/test3'\n        // etc.\n    }\n}\n```\n\n\u003e NOTE: Using a separate entry point per test suite allows Jest to run your tests in parallel processes!\n\n\u003e NOTE: The /example demonstrates how the entry points could be dynamically generated.\n\nIf you intend to define manual `__mocks__` then you need to run your modules through the manual mock loader:\n\n```js\n// webpack.config.js\nmodule.exports = {\n    ...\n    preLoaders: [\n        {\n            test: /\\.js$/,\n            loader: 'jestpack/ManualMockLoader'\n        }\n    ]\n}\n```\n\nFinally, you need to apply the Jestpack plugin which transforms Jest's CommonJs API calls into something Webpack can understand, i.e. `jest.dontMock('../foo')` becomes `jest.dontMock(1)`:\n\n```js\n// webpack.config.js\nvar JestpackPlugin = require('jestpack/Plugin');\n\nmodule.exports = {\n    ...\n    plugins: [\n        new JestpackPlugin()\n    ]\n}\n```\n\nAnd save the `stats.json` in the root of your `config.testPathDirs` directory. So in this case `__bundled_tests__/stats.json`:\n\n```js\n// webpack.config.js\nvar StatsWebpackPlugin = require('stats-webpack-plugin');\n\nmodule.exports = {\n    ...\n    plugins: [\n        ...\n        new StatsWebpackPlugin('stats.json')\n    ]\n}\n```\n\nTests can then be run by building your tests and running Jest:\n\n`webpack \u0026\u0026 jest`\n\n\u003e NOTE: A complete working configuration can be found in the /example directory.\n\n### Optimization\nDepending on the number of modules in your dependency graph you may experience *incredibly* slow builds when building a separate entry point per test suite. This can be greatly optimized using Webpack's [`CommonsChunkPlugin`](http://webpack.github.io/docs/list-of-plugins.html#commonschunkplugin):\n\n```js\n// webpack.config.js\nvar webpack = require('webpack');\n\nmodle.exports = {\n    ...\n    plugins: [\n        ...\n        // This is telling Webpack to extract all dependencies that are used by 2 or more modules into '__bundled_tests__/common.js'\n        new webpack.optimize.CommonsChunkPlugin({\n            filename: 'common.js',\n            minChunks: 2\n        })\n    ]\n}\n\n```\n\nWhich can then be included via Jest's [`config.setupEnvScriptFile`](https://facebook.github.io/jest/docs/api.html#config-setupenvscriptfile-string):\n\n```js\n// package.json\n{\n    ...\n    \"jest\": {\n        ...\n        \"setupEnvScriptFile\": \"\u003crootDir\u003e/__bundled_tests__/common.js\"\n    }\n}\n```\n\nIn addition, if you actually need to do some environment setup you can get the common chunk to execute an entry point like this:\n\n```js\n// webpack.config.js\nmodule.exports = {\n    ...\n    entry: {\n        ...\n        setup: './setup.js'\n    },\n    ...\n    plugins: [\n        ...\n        // When the common.js chunk is included it will execute the 'setup' entry point.\n        new webpack.optimize.CommonsChunkPlugin({\n            name: 'setup',\n            filename: 'common.js',\n            minChunks: 2\n        })\n    ]\n}\n```\n\nIf you need to do some setup after Jasmine has loaded, e.g. define some global matchers, then you can use Jest's [`config.setupTestFrameworkScriptFile`](https://facebook.github.io/jest/docs/api.html#config-setuptestframeworkscriptfile-string) instead:\n\n```js\n// package.json\n{\n    ...\n    \"jest\": {\n        ...\n        \"setupTestFrameworkScriptFile\": \"\u003crootDir\u003e/__bundled_tests__/common.js\"\n    }\n}\n```\n\n### Tips\n\nIf you're using the [babel-loader](https://github.com/babel/babel-loader) it's best not to include the runtime. If for some reason you need to then make sure it's in Jest's [`config.unmockedModulePathPatterns`](https://facebook.github.io/jest/docs/api.html#config-unmockedmodulepathpatterns-array-string):\n\n```js\n// package.json\n{\n    ...\n    \"jest\": {\n        ...\n        \"unmockedModulePathPatterns\": [\n            ...\n            \"\u003crootDir\u003e/node_modules/babel-runtime\"\n        ]\n    }\n}\n```\n\nIf you're using [code splitting](http://webpack.github.io/docs/code-splitting.html) then you're better off disabling it for tests with Webpack's [`LimitChunkCountPlugin`](https://github.com/webpack/docs/wiki/list-of-plugins#limitchunkcountplugin):\n\n```js\n// webpack.config.js\nvar webpack = require('webpack');\n\nmodule.exports = {\n    ...\n    plugins: [\n        ...\n        new webpack.optimize.LimitChunkCountPlugin({\n            maxChunks: 1\n        })\n    ]\n}\n```\n\nIf you're using [css modules](https://github.com/webpack/css-loader#css-modules) you'll need to add the loader to Jest's [`config.unmockedModulePathPatterns`](https://facebook.github.io/jest/docs/api.html#config-unmockedmodulepathpatterns-array-string):\n\n```js\n// package.json\n{\n    ...\n    \"jest\": {\n        ...\n        \"unmockedModulePathPatterns\": [\n            ...\n            \"\u003crootDir\u003e/node_modules/css-loader\"\n        ]\n    }\n}\n```\n\n## Current Limitations\n\n- Code coverage isn't supported.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F60frames%2Fjestpack","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F60frames%2Fjestpack","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F60frames%2Fjestpack/lists"}