Skip to content

Commit

Permalink
Merge 6d83ba1 into d4bc650
Browse files Browse the repository at this point in the history
  • Loading branch information
bobpace committed Jun 30, 2014
2 parents d4bc650 + 6d83ba1 commit 4feddd6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 49 deletions.
25 changes: 15 additions & 10 deletions lib/moduleEnv.js
Expand Up @@ -47,22 +47,27 @@ function requireProxy(path) {

function registerExtensions() {
originalExtensions.coffee = require.extensions[".coffee"];
require.extensions[".coffee"] = coffeeExtension;
originalExtensions.cjsx = require.extensions[".cjsx"];
require.extensions[".coffee"] = compileCoffeeWith("coffee-script");
require.extensions[".cjsx"] = compileCoffeeWith("coffee-react");
}

function restoreExtensions() {
require.extensions[".coffee"] = originalExtensions.coffee;
require.extensions[".cjsx"] = originalExtensions.cjsx;
}

function coffeeExtension(module, filename) {
var coffee = require("coffee-script"),
content = stripBOM(fs.readFileSync(filename, "utf8"));
function compileCoffeeWith(coffeeCompiler) {
return function(module, filename) {
var coffee = require(coffeeCompiler),
content = stripBOM(fs.readFileSync(filename, "utf8"));

content = coffee.compile(content, {
filename: filename,
bare: true
});
module._compile(content, filename);
content = coffee.compile(content, {
filename: filename,
bare: true
});
module._compile(content, filename);
};
}

/**
Expand All @@ -79,4 +84,4 @@ function stripBOM(content) {
}

exports.load = load;
exports.inject = inject;
exports.inject = inject;
77 changes: 39 additions & 38 deletions package.json
@@ -1,39 +1,40 @@
{
"name" : "rewire",
"version" : "2.0.1",
"description" : "Easy dependency injection for node.js unit testing",
"keywords" : [
"dependency",
"injection",
"mock",
"shim",
"module",
"unit",
"test",
"leak",
"inspect"
],
"author" : {
"name" : "Johannes Ewald",
"email" : "mail@johannesewald.de"
},
"main" : "lib/index.js",
"homepage": "https://github.com/jhnns/rewire",
"bugs" : {
"url" : "https://github.com/jhnns/rewire/issues",
"email" : "mail@johannesewald.de"
},
"repository": {
"type": "git",
"url": "git://github.com/jhnns/rewire.git"
},
"devDependencies": {
"mocha": "1.x",
"expect.js": "0.x",
"coffee-script": "1.x"
},
"scripts" : {
"test" : "node node_modules/mocha/bin/mocha -R spec",
"coverage": "istanbul cover ./node_modules/mocha/bin/_mocha"
}
}
"name": "rewire",
"version": "2.0.1",
"description": "Easy dependency injection for node.js unit testing",
"keywords": [
"dependency",
"injection",
"mock",
"shim",
"module",
"unit",
"test",
"leak",
"inspect"
],
"author": {
"name": "Johannes Ewald",
"email": "mail@johannesewald.de"
},
"main": "lib/index.js",
"homepage": "https://github.com/jhnns/rewire",
"bugs": {
"url": "https://github.com/jhnns/rewire/issues",
"email": "mail@johannesewald.de"
},
"repository": {
"type": "git",
"url": "git://github.com/jhnns/rewire.git"
},
"devDependencies": {
"mocha": "1.x",
"expect.js": "0.x",
"coffee-script": "1.x",
"coffee-react": "0.4.0"
},
"scripts": {
"test": "node node_modules/mocha/bin/mocha -R spec",
"coverage": "istanbul cover ./node_modules/mocha/bin/_mocha"
}
}
14 changes: 13 additions & 1 deletion test/rewire.test.js
Expand Up @@ -31,4 +31,16 @@ describe("rewire", function () {
});
expect(coffeeModule.readFileSync()).to.be("It works!");
});
});
it("should also work with cjsx", function () {
var cjsxModule;

rewire = require("../");
cjsxModule = rewire("./testModules/module.cjsx");
cjsxModule.__set__("fs", {
readFileSync: function () {
return "It works!";
}
});
expect(cjsxModule.readFileSync()).to.be("It works!");
});
});
9 changes: 9 additions & 0 deletions test/testModules/module.cjsx
@@ -0,0 +1,9 @@
fs = require "fs"
#fake react dependency
React =
DOM:
div: (->)
component = <div></div>

exports.readFileSync = () -> fs.readFileSync()
exports.component = component

0 comments on commit 4feddd6

Please sign in to comment.