Skip to content

Commit

Permalink
Merge pull request #36 from plasticine/injectify-loader-integration
Browse files Browse the repository at this point in the history
Merge injectify-loader and inject-loader
  • Loading branch information
vladimir-tikhonov committed Mar 19, 2017
2 parents 41415c5 + f8e61d8 commit d005d1f
Show file tree
Hide file tree
Showing 52 changed files with 4,315 additions and 743 deletions.
4 changes: 0 additions & 4 deletions .babelrc

This file was deleted.

13 changes: 13 additions & 0 deletions .eslintrc.js
@@ -0,0 +1,13 @@
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,
jasmine: true
},
};
15 changes: 0 additions & 15 deletions .flowconfig

This file was deleted.

18 changes: 15 additions & 3 deletions .gitignore
@@ -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
@@ -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
- make integration-test
16 changes: 16 additions & 0 deletions Makefile
@@ -0,0 +1,16 @@
lint:
yarn run eslint

test:
yarn run build
yarn run build-test
yarn run test

integration-test:
./script/integration_test

build:
yarn run build
mkdir -p ./dist
cp -f ./tmp/index.js ./dist/index.js
cp -f ./tmp/index.js.map ./dist/index.js.map
4 changes: 2 additions & 2 deletions README.md
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.

8 changes: 8 additions & 0 deletions __tests__/modules/a.js
@@ -0,0 +1,8 @@
module.exports = {
a() {
return 'a - original';
},
require() {
return 'require method in a.js';
},
};
18 changes: 18 additions & 0 deletions __tests__/modules/amd.js
@@ -0,0 +1,18 @@
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();
},

callRequireMethod() {
return a.require();
},
};
});
3 changes: 3 additions & 0 deletions __tests__/modules/b.js
@@ -0,0 +1,3 @@
module.exports = function b() {
return 'b - original';
};
16 changes: 16 additions & 0 deletions __tests__/modules/commonjs.js
@@ -0,0 +1,16 @@
const a = require('./a.js');
const b = require('./b.js');

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

getB() {
return b();
},

callRequireMethod() {
return a.require();
},
};
14 changes: 14 additions & 0 deletions __tests__/modules/es6.js
@@ -0,0 +1,14 @@
import a, { a as methodA } from './a.js';
import b from './b.js';

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

export function getB() {
return b();
}

export function callRequireMethod() {
return a.require();
}
66 changes: 66 additions & 0 deletions __tests__/tests.js
@@ -0,0 +1,66 @@
/* eslint-disable global-require */

const assert = require('assert');

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

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

describe('inject-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/);
});

it('does not break someObject.require calls', () => {
const module = injector.moduleInjector();

assert.equal(module.callRequireMethod(), 'require method in a.js');
});
});
});
});

0 comments on commit d005d1f

Please sign in to comment.