Skip to content

A webpack loader that turns a commonjs module into one whose dependencies can be injected

Notifications You must be signed in to change notification settings

jauco/webpack-injectable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

webpack-injectable

A webpack loader that turns a commonjs module into one whose dependencies can be injected

Let's say we have a file called src/lib/db.js

var retrieveHello = require("db/retrieveHello")
module.exports = function db() {
    return retrieveHello("pgsql://mypgserver")
}

and that hello.js uses that:

var db = require("./db");
function hello() {
    return db();
}

module.exports = hello;

Then you're tests will fail because (a) you're missing the javascript module that implements retrieveHello and even if you would npm install it, your test would be dependant on pgsql://mypgserver.

Instead your test should inject a shim for db.js

/**globals console*/
function dbShim() {
    return "Hello tested and injected world!";
}
var helloMaker = require("jester-tester/src/injectable!./hello");
var hello = helloMaker({"./db": dbShim})

describe("Greetings", function() {
    it("returns `hello world`", function() {
        expect(hello()).toBe("Hello tested and injected world!");
    });
});

I encourage you to log the helloMaker function to the console so you can see what happens under the hood. It's not really magical.

About

A webpack loader that turns a commonjs module into one whose dependencies can be injected

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published