Skip to content

Commit

Permalink
Merge pull request #47 from enb-bem/test/fix-server
Browse files Browse the repository at this point in the history
Fixed server for client tests
  • Loading branch information
blond committed May 8, 2015
2 parents a7bd4d4 + b02709f commit a08c4aa
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 153 deletions.
30 changes: 17 additions & 13 deletions test/lib/run-phantom.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,25 @@ module.exports = function (page) {
exec([phantomjsPath, mochaPhantomjsScriptPath, page, reporter].join(' '),
function (err, stdout) {
if (err) {
var json = JSON.parse(stdout),
errors = json.tests.filter(function (test) {
return test.err;
}),
testError = errors[0].err;
try {
var json = JSON.parse(stdout),
errors = json.tests.filter(function (test) {
return test.err;
}),
testError = errors[0].err;

if (testError) {
var stack = testError.stack;
testError = new Error(testError.message);
testError.stack = stack;
} else {
testError = err;
}
if (testError) {
var stack = testError.stack;
testError = new Error(testError.message);
testError.stack = stack;
} else {
testError = err;
}

defer.reject(testError);
defer.reject(testError);
} catch (e) {
defer.reject(err);
}
} else {
defer.resolve();
}
Expand Down
27 changes: 27 additions & 0 deletions test/lib/run-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var http = require('http'),
promisify = require('vow-node').promisify,
serveStatic = require('serve-static'),
finalhandler = require('finalhandler'),
runPhantom = require('./run-phantom');

module.exports = function (port) {
var serve = serveStatic(process.cwd(), { index: false }),
server = http.createServer(function (req, res) {
var done = finalhandler(req, res);

serve(req, res, done);
}),
listen = promisify(server.listen.bind(server));

return listen(port)
.then(function () {
return runPhantom('http://localhost:' + port + '/index.html');
})
.then(function () {
server.close();
})
.fail(function (err) {
server.close();
throw err;
});
};
97 changes: 12 additions & 85 deletions test/techs/bh-client-module.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
var fs = require('fs'),
path = require('path'),
mock = require('mock-fs'),
promisify = require('vow-node').promisify,
http = require('http'),
serveStatic = require('serve-static'),
finalhandler = require('finalhandler'),
TestNode = require('enb/lib/test/mocks/test-node'),
FileList = require('enb/lib/file-list'),
bhClientModule = require('../../techs/bh-client-module'),
Expand All @@ -14,20 +10,9 @@ var fs = require('fs'),
chaiFilename = require.resolve('chai/chai.js'),
ymFilename = require.resolve('ym/modules.js'),
writeFile = require('../lib/write-file'),
runPhantom = require('../lib/run-phantom'),
serve, server, listen;
runServer = require('../lib/run-server');

describe('bh-client-module', function () {
beforeEach(function () {
serve = serveStatic(process.cwd(), { index: false });
server = http.createServer(function (req, res) {
var done = finalhandler(req, res);

serve(req, res, done);
});
listen = promisify(server.listen.bind(server));
});

afterEach(function () {
mock.restore();
});
Expand Down Expand Up @@ -117,31 +102,6 @@ describe('bh-client-module', function () {
return runTest(test, options, template, lib);
});

it('sourcemap', function () {
var options = {
sourcemap: true,
bhFile: 'bh.js'
},
scheme = {
blocks: {},
bundle: {},
'bh.js': 'module.exports = BH;'
},
bundle, fileList;

mock(scheme);

bundle = new TestNode('bundle');
fileList = new FileList();
fileList.loadFromDirSync('blocks');
bundle.provideTechData('?.files', fileList);

return bundle.runTechAndGetContent(bhClientModule, options)
.spread(function (bh) {
bh.toString().must.include('sourceMappingURL');
});
});

describe('caches', function () {
var mockBhCore, scheme, bundle, fileList;

Expand All @@ -158,14 +118,11 @@ describe('bh-client-module', function () {
'index.html': fs.readFileSync(htmlFilename, 'utf-8'),
'mocha.js': fs.readFileSync(mochaFilename, 'utf-8'),
'chai.js': fs.readFileSync(chaiFilename, 'utf-8'),
'ym.js': fs.readFileSync(ymFilename, 'utf-8')
'ym.js': fs.readFileSync(ymFilename, 'utf-8'),
'some-ym-lib.js': ''
};
});

afterEach(function () {
server.close();
});

it('must use cached bhFile', function () {
scheme['test.js'] = generateTest({ block: 'block' }, '<div class="block"></div>');

Expand Down Expand Up @@ -196,16 +153,10 @@ describe('bh-client-module', function () {
return bundle.runTechAndGetContent(bhClientModule, { bhFile: 'mock.bh.js' });
})
.spread(function (bh) {
fs.writeFileSync('bundle/bundle.bh.js', bh);
// TODO: удалить, когда пофиксится https://github.com/enb-make/enb/issues/224
fs.writeFileSync('bundle/bundle.bh.js', bh);

return listen(3000);
})
.then(function () {
return runPhantom('http://localhost:3000/index.html');
})
.fail(function (err) {
throw err;
return runServer(3000);
});
});

Expand Down Expand Up @@ -239,16 +190,10 @@ describe('bh-client-module', function () {
return bundle.runTechAndGetContent(bhClientModule, { bhFile: 'mock.bh.js' });
})
.spread(function (bh) {
fs.writeFileSync('bundle/bundle.bh.js', bh);
// TODO: удалить, когда пофиксится https://github.com/enb-make/enb/issues/224
fs.writeFileSync('bundle/bundle.bh.js', bh);

return listen(3000);
})
.then(function () {
return runPhantom('http://localhost:3000/index.html');
})
.fail(function (err) {
throw err;
return runServer(3000);
});
});

Expand Down Expand Up @@ -279,16 +224,10 @@ describe('bh-client-module', function () {
return bundle.runTechAndGetContent(bhClientModule);
})
.spread(function (bh) {
fs.writeFileSync('bundle/bundle.bh.js', bh);
// TODO: удалить, когда пофиксится https://github.com/enb-make/enb/issues/224
fs.writeFileSync('bundle/bundle.bh.js', bh);

return listen(3000);
})
.then(function () {
return runPhantom('http://localhost:3000/index.html');
})
.fail(function (err) {
throw err;
return runServer(3000);
});
});
});
Expand Down Expand Up @@ -320,9 +259,7 @@ function runTest(testContent, options, template, lib) {
options.bhFile = 'bh.js';
}

if (lib) {
scheme['some-ym-lib.js'] = lib;
}
scheme['some-ym-lib.js'] = lib || '';

scheme[bhCoreFilename] = fs.readFileSync(bhCoreFilename, 'utf-8');

Expand All @@ -335,19 +272,10 @@ function runTest(testContent, options, template, lib) {

return bundle.runTechAndGetContent(bhClientModule, options)
.spread(function (bh) {
fs.writeFileSync('bundle/bundle.bh.js', bh);
// TODO: удалить, когда пофиксится https://github.com/enb-make/enb/issues/224
fs.writeFileSync('bundle/bundle.bh.js', bh);

return listen(3000);
})
.then(function () {
return runPhantom('http://localhost:3000/index.html');
})
.then(function () {
server.close();
})
.fail(function (err) {
throw err;
return runServer(3000);
});
}

Expand All @@ -364,4 +292,3 @@ function generateTest(json, result) {
'});'
].join('\n');
}

64 changes: 9 additions & 55 deletions test/techs/bh-client.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
var fs = require('fs'),
path = require('path'),
mock = require('mock-fs'),
promisify = require('vow-node').promisify,
http = require('http'),
serveStatic = require('serve-static'),
finalhandler = require('finalhandler'),
TestNode = require('enb/lib/test/mocks/test-node'),
FileList = require('enb/lib/file-list'),
bhClient = require('../../techs/bh-client'),
Expand All @@ -13,20 +9,9 @@ var fs = require('fs'),
mochaFilename = require.resolve('mocha/mocha.js'),
chaiFilename = require.resolve('chai/chai.js'),
writeFile = require('../lib/write-file'),
runPhantom = require('../lib/run-phantom'),
serve, server, listen;
runServer = require('../lib/run-server');

describe('bh-client', function () {
beforeEach(function () {
serve = serveStatic(process.cwd(), { index: false });
server = http.createServer(function (req, res) {
var done = finalhandler(req, res);

serve(req, res, done);
});
listen = promisify(server.listen.bind(server));
});

afterEach(function () {
mock.restore();
});
Expand Down Expand Up @@ -156,10 +141,6 @@ describe('bh-client', function () {
};
});

afterEach(function () {
server.close();
});

it('must use cached bhFile', function () {
scheme['test.js'] = generateTest({ block: 'block' }, '<div class="block"></div>');

Expand Down Expand Up @@ -190,16 +171,10 @@ describe('bh-client', function () {
return bundle.runTechAndGetContent(bhClient, { bhFile: 'mock.bh.js' });
})
.spread(function (bh) {
fs.writeFileSync('bundle/bundle.bh.js', bh);
// TODO: удалить, когда пофиксится https://github.com/enb-make/enb/issues/224
fs.writeFileSync('bundle/bundle.bh.js', bh);

return listen(3000);
})
.then(function () {
return runPhantom('http://localhost:3000/index.html');
})
.fail(function (err) {
throw err;
return runServer(3000);
});
});

Expand Down Expand Up @@ -233,16 +208,10 @@ describe('bh-client', function () {
return bundle.runTechAndGetContent(bhClient, { bhFile: 'mock.bh.js' });
})
.spread(function (bh) {
fs.writeFileSync('bundle/bundle.bh.js', bh);
// TODO: удалить, когда пофиксится https://github.com/enb-make/enb/issues/224
fs.writeFileSync('bundle/bundle.bh.js', bh);

return listen(3000);
})
.then(function () {
return runPhantom('http://localhost:3000/index.html');
})
.fail(function (err) {
throw err;
return runServer(3000);
});
});

Expand Down Expand Up @@ -273,16 +242,10 @@ describe('bh-client', function () {
return bundle.runTechAndGetContent(bhClient);
})
.spread(function (bh) {
fs.writeFileSync('bundle/bundle.bh.js', bh);
// TODO: удалить, когда пофиксится https://github.com/enb-make/enb/issues/224
fs.writeFileSync('bundle/bundle.bh.js', bh);

return listen(3000);
})
.then(function () {
return runPhantom('http://localhost:3000/index.html');
})
.fail(function (err) {
throw err;
return runServer(3000);
});
});
});
Expand Down Expand Up @@ -324,19 +287,10 @@ function runTest(testContent, options, template) {

return bundle.runTechAndGetContent(bhClient, options)
.spread(function (bh) {
fs.writeFileSync('bundle/bundle.bh.js', bh);
// TODO: удалить, когда пофиксится https://github.com/enb-make/enb/issues/224
fs.writeFileSync('bundle/bundle.bh.js', bh);

return listen(3000);
})
.then(function () {
return runPhantom('http://localhost:3000/index.html');
})
.then(function () {
server.close();
})
.fail(function (err) {
throw err;
return runServer(3000);
});
}

Expand Down

0 comments on commit a08c4aa

Please sign in to comment.