├── .gitignore ├── .gitattributes ├── test ├── client │ ├── index.js │ └── client.js ├── server │ ├── index.js │ └── server.js ├── shared │ ├── index.js │ └── shared.js ├── lib │ ├── main.js │ └── EnhancedMocha.js └── tests-server.js ├── lib ├── CrazyAsyncUtils.js └── CrazyAsyncUtilsImpl.js ├── README.md └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /test/client/index.js: -------------------------------------------------------------------------------- 1 | require("./client"); 2 | -------------------------------------------------------------------------------- /test/server/index.js: -------------------------------------------------------------------------------- 1 | require("./server"); 2 | -------------------------------------------------------------------------------- /test/shared/index.js: -------------------------------------------------------------------------------- 1 | require("./shared"); 2 | -------------------------------------------------------------------------------- /lib/CrazyAsyncUtils.js: -------------------------------------------------------------------------------- 1 | exports.sumTwoNumbers = function(a, b, callback) { 2 | require("bundle!./CrazyAsyncUtilsImpl")(function(impl) { 3 | impl.sumTwoNumbers(a, b, callback); 4 | }) 5 | } -------------------------------------------------------------------------------- /test/server/server.js: -------------------------------------------------------------------------------- 1 | var should = require("chai").should(); 2 | 3 | describe("server", function() { 4 | it("should succeed", function() { 5 | true.should.be.ok; 6 | }); 7 | it("should fail", function() { 8 | false.should.be.ok; 9 | }); 10 | }); -------------------------------------------------------------------------------- /test/lib/main.js: -------------------------------------------------------------------------------- 1 | document.write("
"); 2 | 3 | require("mocha/mocha.css"); 4 | require("script!mocha/mocha.js"); 5 | 6 | mocha.setup('bdd'); 7 | 8 | require("../shared/index.js"); 9 | require("../client/index.js"); 10 | 11 | mocha.run(); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # webpack-tests-example 2 | 3 | ``` javascript 4 | node test/tests-server 5 | ``` 6 | 7 | and navigate your browser to [http://localhost:8082/](http://localhost:8082/). 8 | 9 | It will do tests in your browser and in the node.js process. When you update your source files test will rerun. -------------------------------------------------------------------------------- /test/client/client.js: -------------------------------------------------------------------------------- 1 | var should = require("chai").should(); 2 | 3 | describe("client", function() { 4 | it("should succeed", function() { 5 | true.should.be.ok; 6 | }); 7 | it("should fail", function() { 8 | false.should.be.ok; 9 | }); 10 | it("should be there", function() { 11 | }); 12 | }); -------------------------------------------------------------------------------- /lib/CrazyAsyncUtilsImpl.js: -------------------------------------------------------------------------------- 1 | exports.sumTwoNumbers = function(a, b, callback) { 2 | setTimeout(function() { 3 | if(typeof a != "number") return callback(new Error("a must be a number")); 4 | if(typeof b != "number") return callback(new Error("b must be a number")); 5 | return callback(null, a + b); 6 | }, 1000); 7 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "webpack-tests-example", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "bundle-loader": "0.2.x" 7 | }, 8 | "devDependencies": { 9 | "webpack-dev-server": "0.7.x", 10 | "chai": "1.3.x", 11 | "mocha": "1.7.x", 12 | "enhanced-require": "0.4.x" 13 | } 14 | } -------------------------------------------------------------------------------- /test/shared/shared.js: -------------------------------------------------------------------------------- 1 | var should = require("chai").should(); 2 | 3 | describe("shared", function() { 4 | it("should succeed", function() { 5 | true.should.be.ok; 6 | }); 7 | it("should fail", function() { 8 | false.should.be.ok; 9 | }); 10 | it("should be there", function() { 11 | }); 12 | 13 | describe("CrazyAsyncUtils", function() { 14 | var CrazyAsyncUtils = require("../../lib/CrazyAsyncUtils"); 15 | 16 | it("should sum two numbers", function(done) { 17 | CrazyAsyncUtils.sumTwoNumbers(1, 2, function(err, result) { 18 | if(err) throw err; 19 | should.exist(result); 20 | result.should.be.eql(3); 21 | done(); 22 | }); 23 | }); 24 | }); 25 | }); -------------------------------------------------------------------------------- /test/tests-server.js: -------------------------------------------------------------------------------- 1 | // Client side tests 2 | 3 | var path = require("path"); 4 | var Server = require("webpack-dev-server"); 5 | 6 | new Server(path.join(__dirname, "lib", "main.js"), { 7 | webpack: { 8 | output: "bundle.js", 9 | debug: true, 10 | watch: true, 11 | middleware: { 12 | noInfo: true 13 | }, 14 | resolve: { 15 | alias: { 16 | chai: "chai/lib/chai" 17 | } 18 | } 19 | } 20 | }).listen(8082, "localhost"); 21 | console.log("http://localhost:8082"); 22 | 23 | // Server side test 24 | 25 | var EnhancedMocha = require("./lib/EnhancedMocha"); 26 | 27 | var mocha = new EnhancedMocha({ 28 | reporter: "spec" 29 | }); 30 | 31 | mocha.addFile(path.join(__dirname, "shared", "shared.js")); 32 | mocha.addFile(path.join(__dirname, "server", "server.js")); 33 | 34 | mocha.watch(); -------------------------------------------------------------------------------- /test/lib/EnhancedMocha.js: -------------------------------------------------------------------------------- 1 | var Mocha = require("mocha"); 2 | var er = require("enhanced-require"); 3 | 4 | function EnhancedMocha(options) { 5 | Mocha.call(this, options); 6 | } 7 | module.exports = EnhancedMocha; 8 | 9 | EnhancedMocha.prototype = Object.create(Mocha.prototype); 10 | 11 | EnhancedMocha.prototype.loadFiles = function(fn) { 12 | var self = this; 13 | 14 | var require = er(module, { 15 | recursive: true, 16 | hot: true, 17 | watch: true, 18 | applyUpdate: false, 19 | onAutomaticCheck: function(err, modules) { 20 | if(self.watching) { 21 | if(!self.running) 22 | self.run(); 23 | else 24 | self.outdated = true; 25 | } 26 | }, 27 | resolve: self.options.resolve 28 | }); 29 | var suite = this.suite; 30 | suite.suites.length = 0; 31 | suite.tests.length = 0; 32 | try { 33 | this.files.forEach(function(file){ 34 | suite.emit('pre-require', global, file, self); 35 | suite.emit('require', require(file), file, self); 36 | suite.emit('post-require', global, file, self); 37 | }); 38 | } catch(e) { 39 | suite.addTest(new Mocha.Test("fix test errors", function() { 40 | throw e; 41 | })); 42 | } 43 | if(fn) fn(); 44 | } 45 | 46 | EnhancedMocha.prototype.watch = function() { 47 | var self = this; 48 | self.outdated = false; 49 | self.running = true; 50 | self.watching = true; 51 | 52 | // reinit ui to fix ui bugs 53 | this.ui(this.options.ui); 54 | 55 | // run the tests 56 | this.run(function(failures) { 57 | self.running = false; 58 | if(self.outdated) 59 | self.watch(); 60 | }); 61 | }; 62 | --------------------------------------------------------------------------------