Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Socket
Sign inDemoInstall

jest-webpack-alias

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jest-webpack-alias - npm Package Compare versions

Comparing version 1.2.2 to 2.0.0

test/fixture/setup.js

35

lib/webpackInfo.js

@@ -5,2 +5,6 @@ var _ = require('lodash');

function genProfileError(profile, rest) {
return 'Specified jest-webpack-alias.webpackProfile = "' + profile + '", but ' + rest;
}
function read(pmodule, dir) {

@@ -10,21 +14,28 @@ var packageJson = pkginfo.read(pmodule, dir);

var pkgJsonDir = path.dirname(pkgJsonFile);
var webpackProfile = _.get(packageJson['package'], 'jest-webpack-alias.webpackProfile');
if (!webpackProfile) {
throw new Error('Missing setting jest-webpack-alias.webpackProfile in ' + pkgJsonFile);
}
var webpackFile = path.join(pkgJsonDir, 'webpack.config.js');
var profile = _.get(packageJson['package'], 'jest-webpack-alias.profile');
var configFile = _.get(packageJson['package'], 'jest-webpack-alias.configFile', 'webpack.config.js');
var webpackFile = path.join(pkgJsonDir, configFile);
var webpackSettings = require(webpackFile);
if (!webpackSettings[webpackProfile]) {
console.log('settings', webpackSettings);
throw new Error('Missing key "' + webpackProfile + '" in ' + webpackFile);
if (profile) {
if (!_.isArray(webpackSettings)) {
throw new Error(genProfileError(profile, webpackFile + ' does not export an array'));
}
webpackSettings = _.find(webpackSettings, 'name', profile);
if (!webpackSettings) {
throw new Error(genProfileError(profile, webpackFile + ' does not contain this profile'));
}
} else {
if (_.isArray(webpackSettings)) {
throw new Error('jest-webpack-alias.webpackProfile not specified, but ' + webpackFile + ' exports an array');
}
}
var profSettings = webpackSettings[webpackProfile];
if (!_.get(profSettings, 'resolve.root')) {
throw new Error('Missing setting "' + webpackProfile + '.resolve.root' + '" in ' + webpackFile);
if (!_.get(webpackSettings, 'resolve.root')) {
throw new Error('Missing setting "resolve.root" in ' + webpackFile);
}
return {
config: profSettings,
config: webpackSettings,
file: webpackFile

@@ -31,0 +42,0 @@ };

{
"name": "jest-webpack-alias",
"version": "1.2.2",
"version": "2.0.0",
"description": "Preprocessor for Jest that is able to resolve require() statements using webpack aliases.",

@@ -5,0 +5,0 @@ "main": "lib/preprocessor.js",

@@ -57,3 +57,3 @@ # jest-webpack-alias

"jest-webpack-alias": {
"webpackProfile": "dev"
"profile": "dev"
}

@@ -63,2 +63,11 @@ }

## 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.
## Missing features

@@ -65,0 +74,0 @@

@@ -1,6 +0,5 @@

var rewire = require('rewire');
var sinon = require('sinon');
var unwin = require('unwin');
var Setup = require('./setup');
var fixture = new Setup();
var readdir = {
fixture.readdir = {
'/top': ['node_modules', 'package.json', 'src', 'test', 'web_modules', 'webpack.config.js'],

@@ -18,28 +17,36 @@ '/top/node_modules': ['aliasNodeFileDest.js', 'node1', 'node2'],

var webpackProfile = 'dev';
var webpackProfile = fixture.webpackProfile = 'dev';
var webpackSettings = {};
webpackSettings[webpackProfile] = {
resolve: {
root: ['/top/src', '/top/bogus_dir'],
extensions: ["", ".js", ".jsx"],
// omitted: fallback
// omitted: modulesDirectories
alias: {
aliasNodeSubdir1Src: 'node1',
aliasNodeSubdir2Src: 'node1/lib/submodule',
aliasNodeFileSrc: 'aliasNodeFileDest',
aliasPlainSubdirSrc: 'dir1/lib1a'
var webpackSettings = fixture.webpackSettings = [
{
name: 'wrongProfile'
},
{
otherField: 'nope'
},
{
name: webpackProfile,
resolve: {
root: ['/top/src', '/top/bogus_dir'],
extensions: ["", ".js", ".jsx"],
// omitted: fallback
// omitted: modulesDirectories
alias: {
aliasNodeSubdir1Src: 'node1',
aliasNodeSubdir2Src: 'node1/lib/submodule',
aliasNodeFileSrc: 'aliasNodeFileDest',
aliasPlainSubdirSrc: 'dir1/lib1a'
}
}
}
};
];
var requireContents = {
fixture.requireContents = {
'/top/webpack.config.js': webpackSettings
};
var readFile = {
fixture.readFile = {
'/top/package.json': JSON.stringify({
'jest-webpack-alias': {
webpackProfile: webpackProfile
profile: webpackProfile
}

@@ -49,82 +56,2 @@ })

function getDirHas() {
var dirHas = rewire('../../lib/dirHas');
var fs = {
readdirSync: sinon.spy(function(inPath) {
inPath = unwin(inPath);
var dirList = readdir[inPath];
if (!dirList) {
throw new Error('unmocked readdirSync for path ' + inPath);
}
return dirList;
})
};
dirHas.__set__('fs', fs);
return {
cache: dirHas.__get__('cache'),
dirHas: dirHas,
fs: fs,
readdir: readdir
};
}
function getWebpackInfo() {
var webpackInfo = rewire('../../lib/webpackInfo');
var fakeRequire = sinon.spy(function(inPath) {
return requireContents[unwin(inPath)] || require(inPath);
});
webpackInfo.__set__('require', fakeRequire);
var pkginfo = {
read: sinon.spy(function(pmodule, dir) {
return {
dir: '/top/package.json', // misleading key name. lame.
'package': JSON.parse(readFile['/top/package.json'])
};
})
};
webpackInfo.__set__('pkginfo', pkginfo);
return {
pkginfo: pkginfo,
readFile: readFile,
require: fakeRequire,
requireContents: requireContents,
webpackInfo: webpackInfo,
webpackProfile: webpackProfile
};
}
function getWebpackAlias() {
var webpackAlias = rewire('../../lib/preprocessor');
var fs = {
existsSync: sinon.spy(function(inPath) {
return !!readdir[unwin(inPath)];
})
};
webpackAlias.__set__('fs', fs);
var setup = getDirHas();
var dirHas = sinon.spy(setup.dirHas);
webpackAlias.__set__('dirHas', dirHas);
setup = getWebpackInfo();
var webpackInfo = setup.webpackInfo;
webpackInfo.read = sinon.spy(webpackInfo.read);
webpackAlias.__set__('webpackInfo', webpackInfo);
return {
dirHas: dirHas,
fs: fs,
webpackAlias: webpackAlias,
webpackInfo: webpackInfo
};
}
exports.getDirHas = getDirHas;
exports.getWebpackAlias = getWebpackAlias;
exports.getWebpackInfo = getWebpackInfo;
module.exports = fixture.getExports();

@@ -0,3 +1,3 @@

var _ = require('lodash');
var expect = require('./lib/expect');
var basicFixture = require('./fixture/basic');
var unwin = require('unwin');

@@ -7,5 +7,8 @@

var fakeRequire, pkginfo, readFile, requireContents, webpackInfo, webpackProfile;
var filename, output, webpackFile;
function setup() {
var setup = basicFixture.getWebpackInfo();
function setup(fixture) {
filename = null; output = null; webpackFile = null;
var setup = fixture.getWebpackInfo();
fakeRequire = setup.require;

@@ -18,18 +21,121 @@ pkginfo = setup.pkginfo;

beforeEach(setup);
function expectContainsProfile() {
expect(pkginfo.read).to.be.calledOnce;
expect(pkginfo.read.args[0][0]).to.eql({filename: filename});
expect(fakeRequire).to.be.calledOnce;
expect(unwin(fakeRequire.args[0][0])).to.eql(webpackFile);
expect(output).to.have.deep.property('config', _.find(requireContents[webpackFile], 'name', webpackProfile))
.and.to.have.property('name', webpackProfile);
expect(unwin(output.file)).to.eql(webpackFile);
}
describe('with package.json in top dir', function() {
function expectNoProfile() {
expect(pkginfo.read).to.be.calledOnce;
expect(pkginfo.read.args[0][0]).to.eql({filename: filename});
expect(fakeRequire).to.be.calledOnce;
expect(unwin(fakeRequire.args[0][0])).to.eql(webpackFile);
expect(output).to.have.deep.property('config', requireContents[webpackFile]);
expect(unwin(output.file)).to.eql(webpackFile);
}
describe('with default config file and package.json in top dir', function() {
beforeEach(function() {
setup(require('./fixture/basic'));
});
it('finds webpack.config.js and gets profile', function() {
var filename = '/top/test/file1.test.js';
var webpackFile = '/top/webpack.config.js';
var output = webpackInfo.read({filename: filename});
filename = '/top/test/file1.test.js';
webpackFile = '/top/webpack.config.js';
output = webpackInfo.read({filename: filename});
expect(pkginfo.read).to.be.calledOnce;
expect(pkginfo.read.args[0][0]).to.eql({filename: filename});
expect(fakeRequire).to.be.calledOnce;
expect(unwin(fakeRequire.args[0][0])).to.eql(webpackFile);
expect(output).to.have.deep.property('config', requireContents[webpackFile][webpackProfile]);
expect(unwin(output.file)).to.eql(webpackFile);
expectContainsProfile();
});
describe('but wrong profile name', function() {
beforeEach(function() {
setup(require('./fixture/webpackInfo/profile-not-found'));
});
it('throws an error', function() {
filename = '/top/test/file1.test.js';
webpackFile = '/top/webpack/dev.config.js';
var expectedMsg = 'Specified jest-webpack-alias.webpackProfile = "dev", '
+ 'but /top/webpack/dev.config.js does not contain this profile';
expect(webpackInfo.read.bind(null, {filename: filename})).to.throw(expectedMsg);
});
});
describe('but profile setting omitted', function() {
beforeEach(function() {
setup(require('./fixture/webpackInfo/profile-not-specified'));
});
it('throws an error', function() {
filename = '/top/test/file1.test.js';
webpackFile = '/top/webpack/dev.config.js';
var expectedMsg = 'jest-webpack-alias.webpackProfile not specified, '
+ 'but /top/webpack/dev.config.js exports an array';
expect(webpackInfo.read.bind(null, {filename: filename})).to.throw(expectedMsg);
});
});
describe('but missing resolve.root', function() {
beforeEach(function() {
setup(require('./fixture/webpackInfo/profile-missing-resolve-root'));
});
it('throws an error', function() {
filename = '/top/test/file1.test.js';
webpackFile = '/top/webpack/dev.config.js';
var expectedMsg = 'Missing setting "resolve.root" in /top/webpack/dev.config.js';
expect(webpackInfo.read.bind(null, {filename: filename})).to.throw(expectedMsg);
});
});
});
describe('with alternate webpack config file', function() {
beforeEach(function() {
setup(require('./fixture/webpackInfo/alt-config-location'));
});
it('finds profile', function() {
filename = '/top/test/file1.test.js';
webpackFile = '/top/webpack/dev.config.js';
output = webpackInfo.read({filename: filename});
expectContainsProfile();
});
});
describe('with no profile in webpack config', function() {
beforeEach(function() {
setup(require('./fixture/webpackInfo/no-profile'));
});
it('returns entire config file', function() {
filename = '/top/test/file1.test.js';
webpackFile = '/top/webpack/dev.config.js';
output = webpackInfo.read({filename: filename});
expectNoProfile();
});
describe('but profile specified', function() {
beforeEach(function() {
setup(require('./fixture/webpackInfo/profile-not-array'));
});
it('throws an error', function() {
filename = '/top/test/file1.test.js';
webpackFile = '/top/webpack/dev.config.js';
var expectedMsg = 'Specified jest-webpack-alias.webpackProfile = "dev", '
+ 'but /top/webpack/dev.config.js does not export an array';
expect(webpackInfo.read.bind(null, {filename: filename})).to.throw(expectedMsg);
});
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc