Coder Social home page Coder Social logo

vutran / jest-webpack-alias Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mwolson/jest-webpack-alias

0.0 3.0 0.0 71 KB

Preprocessor for Jest that is able to resolve require() statements using webpack aliases

License: MIT License

JavaScript 100.00%

jest-webpack-alias's Introduction

jest-webpack-alias

Build Status Coverage Status NPM

Preprocessor for Jest that is able to resolve require() statements using webpack aliases.

See also transform-jest-deps.

Install

npm install --save-dev jest-webpack-alias

Setup

File __tests__/preprocessor.js:

var babelJest = require('babel-jest');
require('babel-register'); // support ES6 'import' statements
var webpackAlias = require('jest-webpack-alias');

module.exports = {
  process: function(src, filename) {
    if (filename.indexOf('node_modules') === -1) {
      src = babelJest.process(src, filename);
      src = webpackAlias.process(src, filename);
    }
    return src;
  }
};

File package.json:

{
  ...
  "jest": {
    ...
    "scriptPreprocessor": "<rootDir>/__tests__/preprocessor.js",
  },
  "jest-webpack-alias": {
    "profile": "dev"
  }
}

Common problems

Importing CSS and SCSS files

In order to use statements like require('some-styles.css') in a testing environment, it's best to use an npm module like ignore-styles to ignore files that match certain file extensions in require() statements.

Manual package resolution

Code like this will not work, because an AST parser is not smart enough to evaluate variables into strings.

var moduleName = 'myModName';
var computed = require(moduleName);

It can be rewritten like this, using the resolve function:

var resolve = require('jest-webpack-alias').resolve;
var moduleName = 'myModName';
var computed = require(resolve(moduleName, __filename));

package.json options

  • jest-webpack-alias.configFile: Optional, default is "webpack.config.js". If provided, this should be a path fragment relative to your package.json file. Example: "webpack/config.dev.js".

  • jest-webpack-alias.profile: Optional. If provided, will expect your webpack config to be an array of profiles, and will match against the name field of each to choose a webpack config that applies to your Jest tests. See https://github.com/webpack/webpack/tree/master/examples/multi-compiler for an example of this kind of setup.

Known issues

  • resolve.modulesDirectories only searches the directory containing your package.json file, not all ancestors of current file

License

MIT

jest-webpack-alias's People

Contributors

dmnd avatar gyran avatar kendru avatar nhz-io avatar rhfung avatar

Watchers

 avatar  avatar  avatar

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.