disposable.test.js 2.7 KB

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