test.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (c) 2015 David M. Lee, II
  2. 'use strict';
  3. var pt = require('../index.js');
  4. var assert = require('assert');
  5. function later(when) {
  6. return new Promise(function(resolve, reject) {
  7. setTimeout(resolve, when);
  8. });
  9. }
  10. describe('promise-timeout', function() {
  11. describe('a slow promise', function() {
  12. it('should time out', function() {
  13. return pt.timeout(later(1000), 10)
  14. .then(function() {
  15. assert.fail('should not have resolved');
  16. }, function(err) {
  17. assert(err instanceof pt.TimeoutError);
  18. });
  19. });
  20. it('have a decent stack trace', function() {
  21. return pt.timeout(later(1000), 10)
  22. .then(function() {
  23. assert.fail('should not have resolved');
  24. }, function(err) {
  25. assert(err.stack.includes('test.js'));
  26. });
  27. });
  28. });
  29. describe('a fast promise', function() {
  30. it('should resolve with correct value', function() {
  31. return pt.timeout(Promise.resolve('some value'), 1000)
  32. .then(function(val) {
  33. assert.equal(val, 'some value');
  34. }, function(err) {
  35. assert.fail('should have resolved');
  36. });
  37. });
  38. it('should reject with correct exception', function() {
  39. return pt.timeout(Promise.reject(new Error('some error')), 1000)
  40. .then(function(val) {
  41. assert.fail('should have rejected');
  42. }, function(err) {
  43. assert.equal(err.message, 'some error');
  44. });
  45. });
  46. });
  47. });