Skip to content

Commit

Permalink
Merge injectify-loader and inject-loader
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-tikhonov committed Mar 18, 2017
1 parent 41415c5 commit 659852f
Show file tree
Hide file tree
Showing 39 changed files with 4,047 additions and 707 deletions.
4 changes: 0 additions & 4 deletions .babelrc

This file was deleted.

12 changes: 12 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
'extends': ['airbnb-base'],
'rules': {
'import/no-extraneous-dependencies': 'off',
'import/no-webpack-loader-syntax': 'off',
'import/no-unresolved': 'off',
'import/extensions': 'off',
},
'env': {
'mocha': true
},
};
15 changes: 0 additions & 15 deletions .flowconfig

This file was deleted.

18 changes: 15 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
node_modules
yarn.lock
flow-typed
# Logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Dependency directories
node_modules/

# Optional npm cache directory
.npm

# Yarn Integrity file
.yarn-integrity

tmp/
1 change: 0 additions & 1 deletion .npmignore

This file was deleted.

20 changes: 11 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
sudo: false
before_install:
- curl -o- -L https://yarnpkg.com/install.sh | bash
- export PATH=$HOME/.yarn/bin:$PATH
language: node_js
cache: yarn
node_js:
- "node"
- "6"
- "5"
- 7
- 6
- 5
- 4

cache: yarn

script:
- npm test
- make lint
- make test
- make build-release
- make integration-test
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
lint:
npm run eslint

test:
npm run build
npm run build-test
npm run test

integration-test:
./script/integration_test

build-release:
npm run build-prod
cp -f ./tmp/index.js ./dist/index.js
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This is particularly useful for writing tests where mocking things inside your m

[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html)

Use the inject loader by adding `inject!` when you use `require`, this will return a function that can be passed things to inject.
Use the inject loader by adding `inject-loader!` when you use `require`, this will return a function that can be passed things to inject.

By default all `require` statements in an injected module will be altered to be replaced with an injector, though if a replacement it not specified the default will be used.

Expand All @@ -35,7 +35,7 @@ You can manipulate it’s dependencies when you come to write tests as follows:
```javascript
// If no flags are provided when using the loader then
// all require statements will be wrapped in an injector
MyModuleInjector = require('inject!MyStore')
MyModuleInjector = require('inject-loader!MyStore')
MyModule = MyModuleInjector({
'lib/dispatcher': DispatcherMock,
'events': EventsMock,
Expand Down
26 changes: 0 additions & 26 deletions __tests__/createRequireStringRegex.js

This file was deleted.

107 changes: 0 additions & 107 deletions __tests__/index.js

This file was deleted.

5 changes: 5 additions & 0 deletions __tests__/modules/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
a() {
return 'a - original';
},
};
14 changes: 14 additions & 0 deletions __tests__/modules/amd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
define((require) => { // eslint-disable-line no-undef
const a = require('./a.js');
const b = require('./b.js');

return {
getA() {
return a.a();
},

getB() {
return b();
},
};
});
3 changes: 3 additions & 0 deletions __tests__/modules/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function b() {
return 'b - original';
};
12 changes: 12 additions & 0 deletions __tests__/modules/commonjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const a = require('./a.js');
const b = require('./b.js');

module.exports = {
getA() {
return a.a();
},

getB() {
return b();
},
};
10 changes: 10 additions & 0 deletions __tests__/modules/es6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { a } from './a.js';
import b from './b.js';

export function getA() {
return a();
}

export function getB() {
return b();
}
60 changes: 60 additions & 0 deletions __tests__/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-disable global-require */

const assert = require('assert');

const MODULE_A_STUB = {
a() {
return 'a - stubbed';
},
};

const MODULE_B_STUB = () => 'b - stubbed';

describe('injectify-loader', () => {
const injectors = [
{ moduleType: 'commonjs', moduleInjector: require('self!./modules/commonjs.js') },
{ moduleType: 'amd', moduleInjector: require('self!./modules/amd.js') },
{ moduleType: 'es6', moduleInjector: require('self!./modules/es6.js') },
];

injectors.forEach((injector) => {
describe(`${injector.moduleType} modules`, () => {
it('works when no injections were provided', () => {
const module = injector.moduleInjector();

assert.equal(module.getA(), 'a - original');
assert.equal(module.getB(), 'b - original');
});

it('works when one injection was provided', () => {
const module = injector.moduleInjector({
'./a.js': MODULE_A_STUB,
});

assert.equal(module.getA(), 'a - stubbed');
assert.equal(module.getB(), 'b - original');
});

it('works when multiple injections were provided', () => {
const module = injector.moduleInjector({
'./a.js': MODULE_A_STUB,
'./b.js': MODULE_B_STUB,
});

assert.equal(module.getA(), 'a - stubbed');
assert.equal(module.getB(), 'b - stubbed');
});

it('throws an error when invalid dependencies are provided', () => {
const injectInvalidDependencies = () => {
injector.moduleInjector({
'./b.js': null,
'./c.js': null,
});
};

assert.throws(injectInvalidDependencies, /The following injections are invalid:\n- \.\/c\.js/);
});
});
});
});
42 changes: 42 additions & 0 deletions config/shared.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const path = require('path');
const webpackBlocks = require('@webpack-blocks/webpack2');

const ROOT_PATH = path.resolve(__dirname, '..');
const SOURCE_PATH = path.resolve(ROOT_PATH, 'src');
const TESTS_PATH = path.resolve(ROOT_PATH, '__tests__');
const TEMP_PATH = path.resolve(ROOT_PATH, 'tmp');

const babelLoader = () => () => ({
module: {
loaders: [
{
test: /\.js$/,
include: [
SOURCE_PATH,
TESTS_PATH,
],
loader: 'babel-loader',
query: {
cacheDirectory: true,
presets: ['es2015'],
},
},
],
},
});

const nodeTarget = () => () => ({
target: 'node',
});

const baseConfig = () => webpackBlocks.group([
babelLoader(),
nodeTarget(),
]);

module.exports = {
baseConfig,
SOURCE_PATH,
TESTS_PATH,
TEMP_PATH,
};

0 comments on commit 659852f

Please sign in to comment.