forked from plasticine/inject-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reference: plasticine#74
- Loading branch information
Showing
51 changed files
with
1,822 additions
and
693 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ node_js: | |
- 12 | ||
- 10 | ||
- 8 | ||
- 6 | ||
cache: yarn | ||
script: | ||
- yarn test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import assert from 'assert'; | ||
import moduleInjector from 'self!./modules/es6.js'; | ||
|
||
const MODULE_A_STUB = { | ||
a() { | ||
return 'a - stubbed'; | ||
}, | ||
}; | ||
|
||
const MODULE_B_STUB = () => 'b - stubbed'; | ||
|
||
describe('inject-loader (ESM)', () => { | ||
it('works when no injections were provided', () => { | ||
const module = moduleInjector(); | ||
|
||
assert.equal(module.getA(), 'a - original'); | ||
assert.equal(module.getB(), 'b - original'); | ||
assert.equal(module.getC(), 'c - original'); | ||
}); | ||
|
||
it('works when one injection was provided', () => { | ||
const module = moduleInjector({ | ||
'./a.js': MODULE_A_STUB, | ||
}); | ||
|
||
assert.equal(module.getA(), 'a - stubbed'); | ||
assert.equal(module.getB(), 'b - original'); | ||
assert.equal(module.getC(), 'c - original'); | ||
}); | ||
|
||
it('works when a falsey injection was provided', () => { | ||
const module = moduleInjector({ | ||
'./c.js': undefined, | ||
}); | ||
|
||
assert.equal(module.getA(), 'a - original'); | ||
assert.equal(module.getB(), 'b - original'); | ||
assert.equal(module.getC(), undefined); | ||
}); | ||
|
||
it('works when multiple injections were provided', () => { | ||
const module = moduleInjector({ | ||
'./a.js': MODULE_A_STUB, | ||
'./b.js': MODULE_B_STUB, | ||
}); | ||
|
||
assert.equal(module.getA(), 'a - stubbed'); | ||
assert.equal(module.getB(), 'b - stubbed'); | ||
assert.equal(module.getC(), 'c - original'); | ||
}); | ||
|
||
it('throws an error when invalid dependencies are provided', () => { | ||
const injectInvalidDependencies = () => { | ||
moduleInjector({ | ||
'./b.js': null, | ||
'./d.js': null, | ||
}); | ||
}; | ||
|
||
assert.throws(injectInvalidDependencies, /Injection Error in/); | ||
assert.throws( | ||
injectInvalidDependencies, | ||
/The following injections are invalid:\n - \.\/d\.js\n/ | ||
); | ||
assert.throws( | ||
injectInvalidDependencies, | ||
/The following injections were passed in:\n - \.\/b\.js\n - \.\/d\.js\n/ | ||
); | ||
assert.throws( | ||
injectInvalidDependencies, | ||
/Valid injection targets for this module are:\n - \.\/a\.js\n - \.\/b\.js\n - \.\/c\.js/ | ||
); | ||
}); | ||
|
||
it('does not break someObject.require calls', () => { | ||
const module = moduleInjector(); | ||
|
||
assert.equal(module.callRequireMethod(), 'require method in a.js'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const path = require('path'); | ||
const constants = require('./shared'); | ||
|
||
module.exports = { | ||
entry: path.resolve(constants.TESTS_PATH, 'tests.esm.js'), | ||
target: 'node', | ||
mode: 'production', | ||
output: { | ||
path: constants.TEMP_PATH, | ||
filename: 'testBundleESM.js', | ||
}, | ||
resolveLoader: { | ||
alias: { | ||
self: constants.TEMP_PATH, | ||
}, | ||
}, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.js$/, | ||
loader: 'babel-loader', | ||
include: [constants.SOURCE_PATH, constants.TESTS_PATH], | ||
query: { | ||
cacheDirectory: true, | ||
presets: [ | ||
[ | ||
'@babel/preset-env', | ||
{ | ||
modules: false, | ||
}, | ||
], | ||
], | ||
plugins: ['@babel/plugin-transform-flow-strip-types'], | ||
}, | ||
}, | ||
], | ||
}, | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"presets": "es2015" | ||
"presets": ["@babel/preset-env"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": [["@babel/preset-env", {"modules": false}]] | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"private": true, | ||
"scripts": { | ||
"test": "karma start", | ||
"build": "webpack --config webpack.conf.js" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.9.6", | ||
"@babel/preset-env": "^7.9.6", | ||
"babel-loader": "^8.1.0", | ||
"jasmine-core": "^3.1.0", | ||
"jsdom": "^11.7.0", | ||
"karma": "^2.0.0", | ||
"karma-jasmine": "^1.1.1", | ||
"karma-jsdom-launcher": "^6.1.3", | ||
"karma-webpack": "^3.0.0", | ||
"webpack": "~2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const BAR = 2; |
2 changes: 0 additions & 2 deletions
2
example/webpack1-babel/src/getFoo.js → example/webpack2-esm/src/getFoo.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
/* global __VALUEA__ */ | ||
|
||
export default function getFoo() { | ||
return __VALUEA__; | ||
} |
4 changes: 1 addition & 3 deletions
4
example/webpack1-babel/src/main.js → example/webpack2-esm/src/main.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {getValue as mainGetValue} from 'main'; | ||
import mainModuleInjector from 'inject-loader!main'; | ||
|
||
describe('Main', () => { | ||
it('works without injecting', () => { | ||
expect(mainGetValue()).toEqual(20); | ||
}); | ||
|
||
describe('injecting code into module dependencies', () => { | ||
it('allows for injecting code into a subset of dependencies', () => { | ||
let mainModuleInjected = mainModuleInjector({ | ||
bar: {BAR: 5}, | ||
}); | ||
expect(mainModuleInjected.getValue()).toEqual(50); | ||
|
||
mainModuleInjected = mainModuleInjector({ | ||
getFoo: () => 10, | ||
}); | ||
expect(mainModuleInjected.getValue()).toEqual(20); | ||
}); | ||
|
||
it('allows for injecting code mulitple dependencies', () => { | ||
let mainModuleInjected = mainModuleInjector({ | ||
getFoo: () => 5, | ||
bar: {BAR: 5}, | ||
}); | ||
expect(mainModuleInjected.getValue()).toEqual(25); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.