args.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. var test = require('tap').test;
  2. var fromArgs = require('../bin/args.js');
  3. var path = require('path');
  4. var vm = require('vm');
  5. test('bundle from an arguments array', function (t) {
  6. t.plan(2);
  7. var b = fromArgs([ __dirname + '/entry/two.js', '-s', 'XYZ' ]);
  8. b.bundle(function (err, src) {
  9. t.ifError(err);
  10. var c = { window: {} };
  11. vm.runInNewContext(src, c);
  12. t.equal(c.window.XYZ, 2);
  13. });
  14. });
  15. test('external flag for node modules', function(t) {
  16. t.plan(2);
  17. var b = fromArgs([ __dirname + '/external_args/main.js', '-x', 'backbone' ]);
  18. b.bundle(function (err, src) {
  19. t.ifError(err);
  20. vm.runInNewContext(src, {t: t});
  21. });
  22. });
  23. test('bundle from an arguments with --insert-global-vars', function (t) {
  24. t.plan(4);
  25. var b = fromArgs([
  26. __dirname + '/global/filename.js',
  27. '--insert-global-vars=__filename,__dirname',
  28. '--basedir', __dirname
  29. ]);
  30. b.require(__dirname + '/global/filename.js', { expose: 'x' });
  31. b.bundle(function (err, src) {
  32. t.ifError(err, 'b.bundle()');
  33. var c = {}, x;
  34. vm.runInNewContext(src, c);
  35. t.doesNotThrow(function() {
  36. x = c.require('x');
  37. }, 'x = c.require(\'x\')');
  38. t.equal(x && x.filename, '/global/filename.js', 'x.filename');
  39. t.equal(x && x.dirname, '/global', 'x.dirname');
  40. })
  41. });
  42. test('numeric module names', function(t) {
  43. t.plan(1);
  44. var b = fromArgs([ '-x', '1337' ]);
  45. b.bundle(function (err, src) {
  46. t.ifError(err);
  47. });
  48. });
  49. test('entry expose', function (t) {
  50. t.plan(3)
  51. var b = fromArgs([
  52. path.join(__dirname, '/entry_expose/main.js'),
  53. '--require', path.join(__dirname, '/entry_expose/main.js') + ':x',
  54. ]);
  55. b.bundle(function (err, src) {
  56. t.ifError(err);
  57. var c = { console: { log: log } };
  58. function log (msg) { t.equal(msg, 'wow') }
  59. vm.runInNewContext(src, c);
  60. t.equal(c.require('x'), 555);
  61. })
  62. });