Giter Club home page Giter Club logo

jest-webpack-resolver's Introduction

Jest Webpack Resolver Plugin

It is a jest plugin for enchanging jest resolving behaviour to webpack resolver through any webpack config.

Why?

webpack 2 provides some custom resolving opinions for enchanging resolving behaviours like directory-named-webpack-plugin and you can't match this kind of resolves with jest's default resolver and this plugins completely changing resolving rules.

With webpack resolving you don't have to worry about these resolving options:

resolve.alias
resolve.descriptionFiles
resolve.extensions
resolve.mainFields
resolve.mainFiles
resolve.modules
resolve.plugins

Install

Jest resolver option supported for jest >=20 versions.

install with yarn

yarn add jest-webpack-resolver -D

or install with npm

npm install jest-webpack-resolver -D

add to your jest configuration

{
  "resolver": "jest-webpack-resolver"
}

Configuration

By default plugin tries to detect webpack.config.js. You can configure it from package.json or jest.config.js (or any other jest config file)

add jestWebpackResolver to your package.json file at root level:

{
  "name": "",
  "version": "",
  "jestWebpackResolver": {
    "webpackConfig": "./path/to/webpack.config.js"
  }
}

or at your jest.config.js or jest.config.json file

{
  // ...
  jestWebpackResolver: {
    webpackConfig: './path/to/webpack.config.js'
  }
}

to stop the 'Webpack Resolver using: ./...' message use the silent option

{
  "jestWebpackResolver": {
    "silent": true,
    "webpackConfig": "./path/to/webpack.config.js"
  }
}

jest-webpack-resolver's People

Contributors

guilhermelimak avatar mkg0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

jest-webpack-resolver's Issues

Add silent option

First of all, thanks a lot for this plugin, it's very simple and exactly what I was looking for.

The only think that bugs me a bit is the Webpack Resolver using: ./config/webpack.client.js message logged to the console when running tests.

Would you merge a PR that added an option to make this plugin silent or is there such option already (I couldn't find it in the docs)?
I think I could implement it in a day or two, looks like something simple to do.

Thanks again for creating this :]

Unknown option "jestWebpackResolver"

First of all, thanks for your package. It works like a charm.

But I have a small issue... I have to set a path to my webpack config, or else I have the following message:

Webpack Resolver couldn't find any configuration. Tries to resolve ./webpack.config.js
Webpack Resolver Error: Not able to find any valid webpack configuration
● Validation Error:

  Module <rootDir>/node_modules/babel-jest in the transform option was not found.
         <rootDir> is: C:\Users\LucasRIONDEL\dev\fmu\frontend

  Configuration Documentation:
  https://jestjs.io/docs/configuration.html

So I did like you said in the Readme, and i added the following to my jest.config.json:

  "jestWebpackResolver": {
    "webpackConfig": "./webpack/webpack.common.js"
  }

Which works. But when i launch jest, i have the following message:

● Validation Warning:

  Unknown option "jestWebpackResolver" with value {"webpackConfig": "./webpack/webpack.common.js"} was found.
  This is probably a typing mistake. Fixing it will remove this message.

  Configuration Documentation:
  https://jestjs.io/docs/configuration.html

Webpack Resolver using: ./webpack/webpack.common.js

Which is really weird. Do you have any idea why ?

(using jest ^23.6.0)

Any chance of reviving this for webpack 5

We need something like this in webpack 5 with module federation. Do you know of anything or would be willing to update it?

Jest through webpack is a must for federated architecture

Watch flag won't work

"test": "jest --watch",

My error message is: Test suite failed to run UNKNOWN_OPTION: Unknown option: --watch

Jest module mocks do not work with this resolver

I've got an example project here to provide some context. It provides one test that fails with the webpack resolver and succeeds when turning the resolver off. The repository can be found here: https://github.com/mlippens/jest-webpack-resolver-mock-issue.

The issue seems to be the combination of the webpack resolver with jest.mock('../../someModule'), when using the variant of mock using a factory then all is well jest.mock('../../someModule', () => ({ getLogLevel() {} })).

Performance issues

Hi.

Thank you for this library. It has solved a CI issue of ours.

But it might have room for improvements. Our tests are 10 times slower with the webpack resolver.

image
with resolver/without resolver

Do you have any performance advices for us?

Import statements won't work

My package.json

"jest": { "resolver": "jest-webpack-resolver", "transform": { "^.+\\.jsx?$": "babel-jest" } }

My test script is just "test": "jest"

My error message comes from my webpack config and states: import path from 'path'; SyntaxError: Unexpected token import

Please support the case where the webpack config exports a promise!

I think Promise support has been in there since webpack 2.0.

Eg.

var path = require("path");
var directoryNamed = require("directory-named-webpack-plugin");

module.exports = Promise.resolve({
  entry: "./main.js",
  output: {
    filename: "main.build.js"
  },
  resolve: {
    alias: {
      testAlias: path.resolve(__dirname, "alias/")
    },
    plugins: [
      new directoryNamed()
    ]
  }
})

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.