require_optional_tests.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. var assert = require('assert'),
  2. require_optional = require('../'),
  3. nestedTest = require('./nestedTest');
  4. describe('Require Optional', function() {
  5. describe('top level require', function() {
  6. it('should correctly require co library', function() {
  7. var promise = require_optional('es6-promise');
  8. assert.ok(promise);
  9. });
  10. it('should fail to require es6-promise library', function() {
  11. try {
  12. require_optional('co');
  13. } catch(e) {
  14. assert.equal('OPTIONAL_MODULE_NOT_FOUND', e.code);
  15. return;
  16. }
  17. assert.ok(false);
  18. });
  19. it('should ignore optional library not defined', function() {
  20. assert.equal(undefined, require_optional('es6-promise2'));
  21. });
  22. });
  23. describe('internal module file require', function() {
  24. it('should correctly require co library', function() {
  25. var Long = require_optional('bson/lib/bson/long.js');
  26. assert.ok(Long);
  27. });
  28. });
  29. describe('top level resolve', function() {
  30. it('should correctly use exists method', function() {
  31. assert.equal(false, require_optional.exists('co'));
  32. assert.equal(true, require_optional.exists('es6-promise'));
  33. assert.equal(true, require_optional.exists('bson/lib/bson/long.js'));
  34. assert.equal(false, require_optional.exists('es6-promise2'));
  35. });
  36. });
  37. describe('require_optional inside dependencies', function() {
  38. it('should correctly walk up module call stack searching for peerOptionalDependencies', function() {
  39. assert.ok(nestedTest.findPackage('bson'))
  40. });
  41. it('should return null when a package is defined in top-level package.json but not installed', function() {
  42. assert.equal(null, nestedTest.findPackage('es6-promise2'))
  43. });
  44. it('should error when searching for an optional dependency that is not defined in any ancestor package.json', function() {
  45. try {
  46. nestedTest.findPackage('bison')
  47. } catch (err) {
  48. assert.equal(err.message, 'no optional dependency [bison] defined in peerOptionalDependencies in any package.json')
  49. }
  50. })
  51. });
  52. });