disposable.test.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. const node_1 = require("../node");
  13. const chai_1 = require("chai");
  14. describe('disposable', () => {
  15. describe('using', () => {
  16. let disposed;
  17. let disposable;
  18. beforeEach(() => {
  19. disposed = false;
  20. disposable = { dispose: () => (disposed = true) };
  21. });
  22. it('disposes after sync call', () => {
  23. const v = node_1.using(disposable, d => {
  24. chai_1.expect(d).to.equal(disposable);
  25. chai_1.expect(disposed).to.be.false;
  26. return 42;
  27. });
  28. chai_1.expect(v).to.equal(42);
  29. chai_1.expect(disposed).to.be.true;
  30. });
  31. it('disposes after sync throw', () => {
  32. const err = new Error();
  33. try {
  34. node_1.using(disposable, () => {
  35. throw err;
  36. });
  37. throw new Error('expected to throw');
  38. }
  39. catch (e) {
  40. chai_1.expect(e).to.equal(err);
  41. }
  42. chai_1.expect(disposed).to.be.true;
  43. });
  44. it('disposes after promise resolve', () => __awaiter(void 0, void 0, void 0, function* () {
  45. const v = yield node_1.using(disposable, () => __awaiter(void 0, void 0, void 0, function* () {
  46. yield Promise.resolve();
  47. chai_1.expect(disposed).to.be.false;
  48. return 42;
  49. }));
  50. chai_1.expect(v).to.equal(42);
  51. chai_1.expect(disposed).to.be.true;
  52. }));
  53. it('disposes after promise reject', () => __awaiter(void 0, void 0, void 0, function* () {
  54. const err = new Error();
  55. try {
  56. yield node_1.using(disposable, () => __awaiter(void 0, void 0, void 0, function* () {
  57. yield Promise.resolve();
  58. chai_1.expect(disposed).to.be.false;
  59. throw err;
  60. }));
  61. throw new Error('expected to throw');
  62. }
  63. catch (e) {
  64. chai_1.expect(e).to.equal(err);
  65. }
  66. chai_1.expect(disposed).to.be.true;
  67. }));
  68. });
  69. });
  70. //# sourceMappingURL=disposable.test.js.map