|
| 1 | +const { describe, it } = require('node:test'); |
| 2 | +const { strictEqual } = require('node:assert'); |
| 3 | +const serialize = require('../../'); |
| 4 | + |
| 5 | +describe('method bodies containing other functions', function () { |
| 6 | + it('round trips a method containing an arrow with destructured parameters', function () { |
| 7 | + const input = { |
| 8 | + method() { |
| 9 | + const nested = ({ value }) => value; |
| 10 | + return nested({ value: 42 }); |
| 11 | + } |
| 12 | + }; |
| 13 | + const output = eval('(' + serialize(input) + ')'); |
| 14 | + strictEqual(output.method(), 42); |
| 15 | + }); |
| 16 | + |
| 17 | + it('round trips a method containing a function declaration', function () { |
| 18 | + const input = { |
| 19 | + method() { |
| 20 | + function nested() { return 42; } |
| 21 | + return nested(); |
| 22 | + } |
| 23 | + }; |
| 24 | + const output = eval('(' + serialize(input) + ')'); |
| 25 | + strictEqual(output.method(), 42); |
| 26 | + }); |
| 27 | + |
| 28 | + it('preserves async methods containing arrows', async function () { |
| 29 | + const input = { |
| 30 | + async method() { |
| 31 | + const nested = () => 42; |
| 32 | + return nested(); |
| 33 | + } |
| 34 | + }; |
| 35 | + const output = eval('(' + serialize(input) + ')'); |
| 36 | + strictEqual(await output.method(), 42); |
| 37 | + }); |
| 38 | + |
| 39 | + it('preserves generator methods containing arrows', function () { |
| 40 | + const input = { |
| 41 | + *method() { |
| 42 | + const nested = () => 42; |
| 43 | + yield nested(); |
| 44 | + } |
| 45 | + }; |
| 46 | + const output = eval('(' + serialize(input) + ')'); |
| 47 | + strictEqual(output.method().next().value, 42); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | + |
| 52 | +describe('outer function signatures', function () { |
| 53 | + it('round trips a class with a nested arrow', function () { |
| 54 | + class A { method() { return value => value; } } |
| 55 | + const output = eval('(' + serialize({ A }) + ')'); |
| 56 | + strictEqual(new output.A().method()(42), 42); |
| 57 | + strictEqual(output.A.toString(), A.toString()); |
| 58 | + }); |
| 59 | + |
| 60 | + it('round trips a class without nested functions', function () { |
| 61 | + class A { method() { return 42; } } |
| 62 | + const output = eval('(' + serialize({ A }) + ')'); |
| 63 | + strictEqual(new output.A().method(), 42); |
| 64 | + strictEqual(output.A.toString(), A.toString()); |
| 65 | + }); |
| 66 | + |
| 67 | + it('still converts concise methods named class', function () { |
| 68 | + const input = { class () { return 42; } }; |
| 69 | + const output = eval('(' + serialize(input) + ')'); |
| 70 | + strictEqual(output.class(), 42); |
| 71 | + }); |
| 72 | + |
| 73 | + it('preserves async signatures with comments instead of spaces', async function () { |
| 74 | + for (const fn of [async/**/function () { return 42; }, async/**/() => 42]) { |
| 75 | + const output = eval('(' + serialize(fn) + ')'); |
| 76 | + strictEqual(await output(), 42); |
| 77 | + strictEqual(output.constructor.name, 'AsyncFunction'); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + it('preserves arrows whose default arguments contain functions', function () { |
| 82 | + const fn = (callback = function () { return 42; }) => callback(); |
| 83 | + const output = eval('(' + serialize(fn) + ')'); |
| 84 | + strictEqual(output(), 42); |
| 85 | + }); |
| 86 | +}); |
0 commit comments