11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('node:assert');
41cb0ef41Sopenharmony_ciconst { mock, test } = require('node:test');
51cb0ef41Sopenharmony_citest('spies on a function', (t) => {
61cb0ef41Sopenharmony_ci  const sum = t.mock.fn((arg1, arg2) => {
71cb0ef41Sopenharmony_ci    return arg1 + arg2;
81cb0ef41Sopenharmony_ci  });
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci  assert.strictEqual(sum.mock.calls.length, 0);
111cb0ef41Sopenharmony_ci  assert.strictEqual(sum(3, 4), 7);
121cb0ef41Sopenharmony_ci  assert.strictEqual(sum.call(1000, 9, 1), 10);
131cb0ef41Sopenharmony_ci  assert.strictEqual(sum.mock.calls.length, 2);
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  let call = sum.mock.calls[0];
161cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, [3, 4]);
171cb0ef41Sopenharmony_ci  assert.strictEqual(call.error, undefined);
181cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, 7);
191cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, undefined);
201cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, undefined);
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  call = sum.mock.calls[1];
231cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, [9, 1]);
241cb0ef41Sopenharmony_ci  assert.strictEqual(call.error, undefined);
251cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, 10);
261cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, undefined);
271cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, 1000);
281cb0ef41Sopenharmony_ci});
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_citest('spies on a bound function', (t) => {
311cb0ef41Sopenharmony_ci  const bound = function(arg1, arg2) {
321cb0ef41Sopenharmony_ci    return this + arg1 + arg2;
331cb0ef41Sopenharmony_ci  }.bind(50);
341cb0ef41Sopenharmony_ci  const sum = t.mock.fn(bound);
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  assert.strictEqual(sum.mock.calls.length, 0);
371cb0ef41Sopenharmony_ci  assert.strictEqual(sum(3, 4), 57);
381cb0ef41Sopenharmony_ci  assert.strictEqual(sum(9, 1), 60);
391cb0ef41Sopenharmony_ci  assert.strictEqual(sum.mock.calls.length, 2);
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  let call = sum.mock.calls[0];
421cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, [3, 4]);
431cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, 57);
441cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, undefined);
451cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, undefined);
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  call = sum.mock.calls[1];
481cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, [9, 1]);
491cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, 60);
501cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, undefined);
511cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, undefined);
521cb0ef41Sopenharmony_ci});
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_citest('spies on a constructor', (t) => {
551cb0ef41Sopenharmony_ci  class ParentClazz {
561cb0ef41Sopenharmony_ci    constructor(c) {
571cb0ef41Sopenharmony_ci      this.c = c;
581cb0ef41Sopenharmony_ci    }
591cb0ef41Sopenharmony_ci  }
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  class Clazz extends ParentClazz {
621cb0ef41Sopenharmony_ci    #privateValue;
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci    constructor(a, b) {
651cb0ef41Sopenharmony_ci      super(a + b);
661cb0ef41Sopenharmony_ci      this.a = a;
671cb0ef41Sopenharmony_ci      this.#privateValue = b;
681cb0ef41Sopenharmony_ci    }
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci    getPrivateValue() {
711cb0ef41Sopenharmony_ci      return this.#privateValue;
721cb0ef41Sopenharmony_ci    }
731cb0ef41Sopenharmony_ci  }
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci  const ctor = t.mock.fn(Clazz);
761cb0ef41Sopenharmony_ci  const instance = new ctor(42, 85);
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci  assert(instance instanceof Clazz);
791cb0ef41Sopenharmony_ci  assert(instance instanceof ParentClazz);
801cb0ef41Sopenharmony_ci  assert.strictEqual(instance.a, 42);
811cb0ef41Sopenharmony_ci  assert.strictEqual(instance.getPrivateValue(), 85);
821cb0ef41Sopenharmony_ci  assert.strictEqual(instance.c, 127);
831cb0ef41Sopenharmony_ci  assert.strictEqual(ctor.mock.calls.length, 1);
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci  const call = ctor.mock.calls[0];
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, [42, 85]);
881cb0ef41Sopenharmony_ci  assert.strictEqual(call.error, undefined);
891cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, instance);
901cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, Clazz);
911cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, instance);
921cb0ef41Sopenharmony_ci});
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_citest('a no-op spy function is created by default', (t) => {
951cb0ef41Sopenharmony_ci  const fn = t.mock.fn();
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  assert.strictEqual(fn.mock.calls.length, 0);
981cb0ef41Sopenharmony_ci  assert.strictEqual(fn(3, 4), undefined);
991cb0ef41Sopenharmony_ci  assert.strictEqual(fn.mock.calls.length, 1);
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci  const call = fn.mock.calls[0];
1021cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, [3, 4]);
1031cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, undefined);
1041cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, undefined);
1051cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, undefined);
1061cb0ef41Sopenharmony_ci});
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_citest('internal no-op function can be reused', (t) => {
1091cb0ef41Sopenharmony_ci  const fn1 = t.mock.fn();
1101cb0ef41Sopenharmony_ci  fn1.prop = true;
1111cb0ef41Sopenharmony_ci  const fn2 = t.mock.fn();
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci  fn1(1);
1141cb0ef41Sopenharmony_ci  fn2(2);
1151cb0ef41Sopenharmony_ci  fn1(3);
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci  assert.notStrictEqual(fn1.mock, fn2.mock);
1181cb0ef41Sopenharmony_ci  assert.strictEqual(fn1.mock.calls.length, 2);
1191cb0ef41Sopenharmony_ci  assert.strictEqual(fn2.mock.calls.length, 1);
1201cb0ef41Sopenharmony_ci  assert.strictEqual(fn1.prop, true);
1211cb0ef41Sopenharmony_ci  assert.strictEqual(fn2.prop, undefined);
1221cb0ef41Sopenharmony_ci});
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_citest('functions can be mocked multiple times at once', (t) => {
1251cb0ef41Sopenharmony_ci  function sum(a, b) {
1261cb0ef41Sopenharmony_ci    return a + b;
1271cb0ef41Sopenharmony_ci  }
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci  function difference(a, b) {
1301cb0ef41Sopenharmony_ci    return a - b;
1311cb0ef41Sopenharmony_ci  }
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci  function product(a, b) {
1341cb0ef41Sopenharmony_ci    return a * b;
1351cb0ef41Sopenharmony_ci  }
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ci  const fn1 = t.mock.fn(sum, difference);
1381cb0ef41Sopenharmony_ci  const fn2 = t.mock.fn(sum, product);
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci  assert.strictEqual(fn1(5, 3), 2);
1411cb0ef41Sopenharmony_ci  assert.strictEqual(fn2(5, 3), 15);
1421cb0ef41Sopenharmony_ci  assert.strictEqual(fn2(4, 2), 8);
1431cb0ef41Sopenharmony_ci  assert(!('mock' in sum));
1441cb0ef41Sopenharmony_ci  assert(!('mock' in difference));
1451cb0ef41Sopenharmony_ci  assert(!('mock' in product));
1461cb0ef41Sopenharmony_ci  assert.notStrictEqual(fn1.mock, fn2.mock);
1471cb0ef41Sopenharmony_ci  assert.strictEqual(fn1.mock.calls.length, 1);
1481cb0ef41Sopenharmony_ci  assert.strictEqual(fn2.mock.calls.length, 2);
1491cb0ef41Sopenharmony_ci});
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_citest('internal no-op function can be reused as methods', (t) => {
1521cb0ef41Sopenharmony_ci  const obj = {
1531cb0ef41Sopenharmony_ci    _foo: 5,
1541cb0ef41Sopenharmony_ci    _bar: 9,
1551cb0ef41Sopenharmony_ci    foo() {
1561cb0ef41Sopenharmony_ci      return this._foo;
1571cb0ef41Sopenharmony_ci    },
1581cb0ef41Sopenharmony_ci    bar() {
1591cb0ef41Sopenharmony_ci      return this._bar;
1601cb0ef41Sopenharmony_ci    },
1611cb0ef41Sopenharmony_ci  };
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ci  t.mock.method(obj, 'foo');
1641cb0ef41Sopenharmony_ci  obj.foo.prop = true;
1651cb0ef41Sopenharmony_ci  t.mock.method(obj, 'bar');
1661cb0ef41Sopenharmony_ci  assert.strictEqual(obj.foo(), 5);
1671cb0ef41Sopenharmony_ci  assert.strictEqual(obj.bar(), 9);
1681cb0ef41Sopenharmony_ci  assert.strictEqual(obj.bar(), 9);
1691cb0ef41Sopenharmony_ci  assert.notStrictEqual(obj.foo.mock, obj.bar.mock);
1701cb0ef41Sopenharmony_ci  assert.strictEqual(obj.foo.mock.calls.length, 1);
1711cb0ef41Sopenharmony_ci  assert.strictEqual(obj.bar.mock.calls.length, 2);
1721cb0ef41Sopenharmony_ci  assert.strictEqual(obj.foo.prop, true);
1731cb0ef41Sopenharmony_ci  assert.strictEqual(obj.bar.prop, undefined);
1741cb0ef41Sopenharmony_ci});
1751cb0ef41Sopenharmony_ci
1761cb0ef41Sopenharmony_citest('methods can be mocked multiple times but not at the same time', (t) => {
1771cb0ef41Sopenharmony_ci  const obj = {
1781cb0ef41Sopenharmony_ci    offset: 3,
1791cb0ef41Sopenharmony_ci    sum(a, b) {
1801cb0ef41Sopenharmony_ci      return this.offset + a + b;
1811cb0ef41Sopenharmony_ci    },
1821cb0ef41Sopenharmony_ci  };
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ci  function difference(a, b) {
1851cb0ef41Sopenharmony_ci    return this.offset + (a - b);
1861cb0ef41Sopenharmony_ci  }
1871cb0ef41Sopenharmony_ci
1881cb0ef41Sopenharmony_ci  function product(a, b) {
1891cb0ef41Sopenharmony_ci    return this.offset + a * b;
1901cb0ef41Sopenharmony_ci  }
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ci  const originalSum = obj.sum;
1931cb0ef41Sopenharmony_ci  const fn1 = t.mock.method(obj, 'sum', difference);
1941cb0ef41Sopenharmony_ci
1951cb0ef41Sopenharmony_ci  assert.strictEqual(obj.sum(5, 3), 5);
1961cb0ef41Sopenharmony_ci  assert.strictEqual(obj.sum(5, 1), 7);
1971cb0ef41Sopenharmony_ci  assert.strictEqual(obj.sum, fn1);
1981cb0ef41Sopenharmony_ci  assert.notStrictEqual(fn1.mock, undefined);
1991cb0ef41Sopenharmony_ci  assert.strictEqual(originalSum.mock, undefined);
2001cb0ef41Sopenharmony_ci  assert.strictEqual(difference.mock, undefined);
2011cb0ef41Sopenharmony_ci  assert.strictEqual(product.mock, undefined);
2021cb0ef41Sopenharmony_ci  assert.strictEqual(fn1.mock.calls.length, 2);
2031cb0ef41Sopenharmony_ci
2041cb0ef41Sopenharmony_ci  const fn2 = t.mock.method(obj, 'sum', product);
2051cb0ef41Sopenharmony_ci
2061cb0ef41Sopenharmony_ci  assert.strictEqual(obj.sum(5, 3), 18);
2071cb0ef41Sopenharmony_ci  assert.strictEqual(obj.sum, fn2);
2081cb0ef41Sopenharmony_ci  assert.notStrictEqual(fn1, fn2);
2091cb0ef41Sopenharmony_ci  assert.strictEqual(fn2.mock.calls.length, 1);
2101cb0ef41Sopenharmony_ci
2111cb0ef41Sopenharmony_ci  obj.sum.mock.restore();
2121cb0ef41Sopenharmony_ci  assert.strictEqual(obj.sum, fn1);
2131cb0ef41Sopenharmony_ci  obj.sum.mock.restore();
2141cb0ef41Sopenharmony_ci  assert.strictEqual(obj.sum, originalSum);
2151cb0ef41Sopenharmony_ci  assert.strictEqual(obj.sum.mock, undefined);
2161cb0ef41Sopenharmony_ci});
2171cb0ef41Sopenharmony_ci
2181cb0ef41Sopenharmony_citest('spies on an object method', (t) => {
2191cb0ef41Sopenharmony_ci  const obj = {
2201cb0ef41Sopenharmony_ci    prop: 5,
2211cb0ef41Sopenharmony_ci    method(a, b) {
2221cb0ef41Sopenharmony_ci      return a + b + this.prop;
2231cb0ef41Sopenharmony_ci    },
2241cb0ef41Sopenharmony_ci  };
2251cb0ef41Sopenharmony_ci
2261cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method(1, 3), 9);
2271cb0ef41Sopenharmony_ci  t.mock.method(obj, 'method');
2281cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method.mock.calls.length, 0);
2291cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method(1, 3), 9);
2301cb0ef41Sopenharmony_ci
2311cb0ef41Sopenharmony_ci  const call = obj.method.mock.calls[0];
2321cb0ef41Sopenharmony_ci
2331cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, [1, 3]);
2341cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, 9);
2351cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, undefined);
2361cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, obj);
2371cb0ef41Sopenharmony_ci
2381cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method.mock.restore(), undefined);
2391cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method(1, 3), 9);
2401cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method.mock, undefined);
2411cb0ef41Sopenharmony_ci});
2421cb0ef41Sopenharmony_ci
2431cb0ef41Sopenharmony_citest('spies on a getter', (t) => {
2441cb0ef41Sopenharmony_ci  const obj = {
2451cb0ef41Sopenharmony_ci    prop: 5,
2461cb0ef41Sopenharmony_ci    get method() {
2471cb0ef41Sopenharmony_ci      return this.prop;
2481cb0ef41Sopenharmony_ci    },
2491cb0ef41Sopenharmony_ci  };
2501cb0ef41Sopenharmony_ci
2511cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method, 5);
2521cb0ef41Sopenharmony_ci
2531cb0ef41Sopenharmony_ci  const getter = t.mock.method(obj, 'method', { getter: true });
2541cb0ef41Sopenharmony_ci
2551cb0ef41Sopenharmony_ci  assert.strictEqual(getter.mock.calls.length, 0);
2561cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method, 5);
2571cb0ef41Sopenharmony_ci
2581cb0ef41Sopenharmony_ci  const call = getter.mock.calls[0];
2591cb0ef41Sopenharmony_ci
2601cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, []);
2611cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, 5);
2621cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, undefined);
2631cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, obj);
2641cb0ef41Sopenharmony_ci
2651cb0ef41Sopenharmony_ci  assert.strictEqual(getter.mock.restore(), undefined);
2661cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method, 5);
2671cb0ef41Sopenharmony_ci});
2681cb0ef41Sopenharmony_ci
2691cb0ef41Sopenharmony_citest('spies on a setter', (t) => {
2701cb0ef41Sopenharmony_ci  const obj = {
2711cb0ef41Sopenharmony_ci    prop: 100,
2721cb0ef41Sopenharmony_ci    // eslint-disable-next-line accessor-pairs
2731cb0ef41Sopenharmony_ci    set method(val) {
2741cb0ef41Sopenharmony_ci      this.prop = val;
2751cb0ef41Sopenharmony_ci    },
2761cb0ef41Sopenharmony_ci  };
2771cb0ef41Sopenharmony_ci
2781cb0ef41Sopenharmony_ci  assert.strictEqual(obj.prop, 100);
2791cb0ef41Sopenharmony_ci  obj.method = 88;
2801cb0ef41Sopenharmony_ci  assert.strictEqual(obj.prop, 88);
2811cb0ef41Sopenharmony_ci
2821cb0ef41Sopenharmony_ci  const setter = t.mock.method(obj, 'method', { setter: true });
2831cb0ef41Sopenharmony_ci
2841cb0ef41Sopenharmony_ci  assert.strictEqual(setter.mock.calls.length, 0);
2851cb0ef41Sopenharmony_ci  obj.method = 77;
2861cb0ef41Sopenharmony_ci  assert.strictEqual(obj.prop, 77);
2871cb0ef41Sopenharmony_ci  assert.strictEqual(setter.mock.calls.length, 1);
2881cb0ef41Sopenharmony_ci
2891cb0ef41Sopenharmony_ci  const call = setter.mock.calls[0];
2901cb0ef41Sopenharmony_ci
2911cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, [77]);
2921cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, undefined);
2931cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, undefined);
2941cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, obj);
2951cb0ef41Sopenharmony_ci
2961cb0ef41Sopenharmony_ci  assert.strictEqual(setter.mock.restore(), undefined);
2971cb0ef41Sopenharmony_ci  assert.strictEqual(obj.prop, 77);
2981cb0ef41Sopenharmony_ci  obj.method = 65;
2991cb0ef41Sopenharmony_ci  assert.strictEqual(obj.prop, 65);
3001cb0ef41Sopenharmony_ci});
3011cb0ef41Sopenharmony_ci
3021cb0ef41Sopenharmony_citest('spy functions can be bound', (t) => {
3031cb0ef41Sopenharmony_ci  const sum = t.mock.fn(function(arg1, arg2) {
3041cb0ef41Sopenharmony_ci    return this + arg1 + arg2;
3051cb0ef41Sopenharmony_ci  });
3061cb0ef41Sopenharmony_ci  const bound = sum.bind(1000);
3071cb0ef41Sopenharmony_ci
3081cb0ef41Sopenharmony_ci  assert.strictEqual(bound(9, 1), 1010);
3091cb0ef41Sopenharmony_ci  assert.strictEqual(sum.mock.calls.length, 1);
3101cb0ef41Sopenharmony_ci
3111cb0ef41Sopenharmony_ci  const call = sum.mock.calls[0];
3121cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, [9, 1]);
3131cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, 1010);
3141cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, undefined);
3151cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, 1000);
3161cb0ef41Sopenharmony_ci
3171cb0ef41Sopenharmony_ci  assert.strictEqual(sum.mock.restore(), undefined);
3181cb0ef41Sopenharmony_ci  assert.strictEqual(sum.bind(0)(2, 11), 13);
3191cb0ef41Sopenharmony_ci});
3201cb0ef41Sopenharmony_ci
3211cb0ef41Sopenharmony_citest('mocks prototype methods on an instance', async (t) => {
3221cb0ef41Sopenharmony_ci  class Runner {
3231cb0ef41Sopenharmony_ci    async someTask(msg) {
3241cb0ef41Sopenharmony_ci      return Promise.resolve(msg);
3251cb0ef41Sopenharmony_ci    }
3261cb0ef41Sopenharmony_ci
3271cb0ef41Sopenharmony_ci    async method(msg) {
3281cb0ef41Sopenharmony_ci      await this.someTask(msg);
3291cb0ef41Sopenharmony_ci      return msg;
3301cb0ef41Sopenharmony_ci    }
3311cb0ef41Sopenharmony_ci  }
3321cb0ef41Sopenharmony_ci  const msg = 'ok';
3331cb0ef41Sopenharmony_ci  const obj = new Runner();
3341cb0ef41Sopenharmony_ci  assert.strictEqual(await obj.method(msg), msg);
3351cb0ef41Sopenharmony_ci
3361cb0ef41Sopenharmony_ci  t.mock.method(obj, obj.someTask.name);
3371cb0ef41Sopenharmony_ci  assert.strictEqual(obj.someTask.mock.calls.length, 0);
3381cb0ef41Sopenharmony_ci
3391cb0ef41Sopenharmony_ci  assert.strictEqual(await obj.method(msg), msg);
3401cb0ef41Sopenharmony_ci
3411cb0ef41Sopenharmony_ci  const call = obj.someTask.mock.calls[0];
3421cb0ef41Sopenharmony_ci
3431cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, [msg]);
3441cb0ef41Sopenharmony_ci  assert.strictEqual(await call.result, msg);
3451cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, undefined);
3461cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, obj);
3471cb0ef41Sopenharmony_ci
3481cb0ef41Sopenharmony_ci  const obj2 = new Runner();
3491cb0ef41Sopenharmony_ci  // Ensure that a brand new instance is not mocked
3501cb0ef41Sopenharmony_ci  assert.strictEqual(
3511cb0ef41Sopenharmony_ci    obj2.someTask.mock,
3521cb0ef41Sopenharmony_ci    undefined
3531cb0ef41Sopenharmony_ci  );
3541cb0ef41Sopenharmony_ci
3551cb0ef41Sopenharmony_ci  assert.strictEqual(obj.someTask.mock.restore(), undefined);
3561cb0ef41Sopenharmony_ci  assert.strictEqual(await obj.method(msg), msg);
3571cb0ef41Sopenharmony_ci  assert.strictEqual(obj.someTask.mock, undefined);
3581cb0ef41Sopenharmony_ci  assert.strictEqual(Runner.prototype.someTask.mock, undefined);
3591cb0ef41Sopenharmony_ci});
3601cb0ef41Sopenharmony_ci
3611cb0ef41Sopenharmony_citest('spies on async static class methods', async (t) => {
3621cb0ef41Sopenharmony_ci  class Runner {
3631cb0ef41Sopenharmony_ci    static async someTask(msg) {
3641cb0ef41Sopenharmony_ci      return Promise.resolve(msg);
3651cb0ef41Sopenharmony_ci    }
3661cb0ef41Sopenharmony_ci
3671cb0ef41Sopenharmony_ci    static async method(msg) {
3681cb0ef41Sopenharmony_ci      await this.someTask(msg);
3691cb0ef41Sopenharmony_ci      return msg;
3701cb0ef41Sopenharmony_ci    }
3711cb0ef41Sopenharmony_ci  }
3721cb0ef41Sopenharmony_ci  const msg = 'ok';
3731cb0ef41Sopenharmony_ci  assert.strictEqual(await Runner.method(msg), msg);
3741cb0ef41Sopenharmony_ci
3751cb0ef41Sopenharmony_ci  t.mock.method(Runner, Runner.someTask.name);
3761cb0ef41Sopenharmony_ci  assert.strictEqual(Runner.someTask.mock.calls.length, 0);
3771cb0ef41Sopenharmony_ci
3781cb0ef41Sopenharmony_ci  assert.strictEqual(await Runner.method(msg), msg);
3791cb0ef41Sopenharmony_ci
3801cb0ef41Sopenharmony_ci  const call = Runner.someTask.mock.calls[0];
3811cb0ef41Sopenharmony_ci
3821cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, [msg]);
3831cb0ef41Sopenharmony_ci  assert.strictEqual(await call.result, msg);
3841cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, undefined);
3851cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, Runner);
3861cb0ef41Sopenharmony_ci
3871cb0ef41Sopenharmony_ci  assert.strictEqual(Runner.someTask.mock.restore(), undefined);
3881cb0ef41Sopenharmony_ci  assert.strictEqual(await Runner.method(msg), msg);
3891cb0ef41Sopenharmony_ci  assert.strictEqual(Runner.someTask.mock, undefined);
3901cb0ef41Sopenharmony_ci  assert.strictEqual(Runner.prototype.someTask, undefined);
3911cb0ef41Sopenharmony_ci
3921cb0ef41Sopenharmony_ci});
3931cb0ef41Sopenharmony_ci
3941cb0ef41Sopenharmony_citest('given null to a mock.method it throws a invalid argument error', (t) => {
3951cb0ef41Sopenharmony_ci  assert.throws(() => t.mock.method(null, {}), { code: 'ERR_INVALID_ARG_TYPE' });
3961cb0ef41Sopenharmony_ci});
3971cb0ef41Sopenharmony_ci
3981cb0ef41Sopenharmony_citest('it should throw given an inexistent property on a object instance', (t) => {
3991cb0ef41Sopenharmony_ci  assert.throws(() => t.mock.method({ abc: 0 }, 'non-existent'), {
4001cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_VALUE'
4011cb0ef41Sopenharmony_ci  });
4021cb0ef41Sopenharmony_ci});
4031cb0ef41Sopenharmony_ci
4041cb0ef41Sopenharmony_citest('spy functions can be used on classes inheritance', (t) => {
4051cb0ef41Sopenharmony_ci  // Makes sure that having a null-prototype doesn't throw our system off
4061cb0ef41Sopenharmony_ci  class A extends null {
4071cb0ef41Sopenharmony_ci    static someTask(msg) {
4081cb0ef41Sopenharmony_ci      return msg;
4091cb0ef41Sopenharmony_ci    }
4101cb0ef41Sopenharmony_ci    static method(msg) {
4111cb0ef41Sopenharmony_ci      return this.someTask(msg);
4121cb0ef41Sopenharmony_ci    }
4131cb0ef41Sopenharmony_ci  }
4141cb0ef41Sopenharmony_ci  class B extends A {}
4151cb0ef41Sopenharmony_ci  class C extends B {}
4161cb0ef41Sopenharmony_ci
4171cb0ef41Sopenharmony_ci  const msg = 'ok';
4181cb0ef41Sopenharmony_ci  assert.strictEqual(C.method(msg), msg);
4191cb0ef41Sopenharmony_ci
4201cb0ef41Sopenharmony_ci  t.mock.method(C, C.someTask.name);
4211cb0ef41Sopenharmony_ci  assert.strictEqual(C.someTask.mock.calls.length, 0);
4221cb0ef41Sopenharmony_ci
4231cb0ef41Sopenharmony_ci  assert.strictEqual(C.method(msg), msg);
4241cb0ef41Sopenharmony_ci
4251cb0ef41Sopenharmony_ci  const call = C.someTask.mock.calls[0];
4261cb0ef41Sopenharmony_ci
4271cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, [msg]);
4281cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, msg);
4291cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, undefined);
4301cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, C);
4311cb0ef41Sopenharmony_ci
4321cb0ef41Sopenharmony_ci  assert.strictEqual(C.someTask.mock.restore(), undefined);
4331cb0ef41Sopenharmony_ci  assert.strictEqual(C.method(msg), msg);
4341cb0ef41Sopenharmony_ci  assert.strictEqual(C.someTask.mock, undefined);
4351cb0ef41Sopenharmony_ci});
4361cb0ef41Sopenharmony_ci
4371cb0ef41Sopenharmony_citest('spy functions don\'t affect the prototype chain', (t) => {
4381cb0ef41Sopenharmony_ci
4391cb0ef41Sopenharmony_ci  class A {
4401cb0ef41Sopenharmony_ci    static someTask(msg) {
4411cb0ef41Sopenharmony_ci      return msg;
4421cb0ef41Sopenharmony_ci    }
4431cb0ef41Sopenharmony_ci  }
4441cb0ef41Sopenharmony_ci  class B extends A {}
4451cb0ef41Sopenharmony_ci  class C extends B {}
4461cb0ef41Sopenharmony_ci
4471cb0ef41Sopenharmony_ci  const msg = 'ok';
4481cb0ef41Sopenharmony_ci
4491cb0ef41Sopenharmony_ci  const ABeforeMockIsUnchanged = Object.getOwnPropertyDescriptor(A, A.someTask.name);
4501cb0ef41Sopenharmony_ci  const BBeforeMockIsUnchanged = Object.getOwnPropertyDescriptor(B, B.someTask.name);
4511cb0ef41Sopenharmony_ci  const CBeforeMockShouldNotHaveDesc = Object.getOwnPropertyDescriptor(C, C.someTask.name);
4521cb0ef41Sopenharmony_ci
4531cb0ef41Sopenharmony_ci  t.mock.method(C, C.someTask.name);
4541cb0ef41Sopenharmony_ci  C.someTask(msg);
4551cb0ef41Sopenharmony_ci  const BAfterMockIsUnchanged = Object.getOwnPropertyDescriptor(B, B.someTask.name);
4561cb0ef41Sopenharmony_ci
4571cb0ef41Sopenharmony_ci  const AAfterMockIsUnchanged = Object.getOwnPropertyDescriptor(A, A.someTask.name);
4581cb0ef41Sopenharmony_ci  const CAfterMockHasDescriptor = Object.getOwnPropertyDescriptor(C, C.someTask.name);
4591cb0ef41Sopenharmony_ci
4601cb0ef41Sopenharmony_ci  assert.strictEqual(CBeforeMockShouldNotHaveDesc, undefined);
4611cb0ef41Sopenharmony_ci  assert.ok(CAfterMockHasDescriptor);
4621cb0ef41Sopenharmony_ci
4631cb0ef41Sopenharmony_ci  assert.deepStrictEqual(ABeforeMockIsUnchanged, AAfterMockIsUnchanged);
4641cb0ef41Sopenharmony_ci  assert.strictEqual(BBeforeMockIsUnchanged, BAfterMockIsUnchanged);
4651cb0ef41Sopenharmony_ci  assert.strictEqual(BBeforeMockIsUnchanged, undefined);
4661cb0ef41Sopenharmony_ci
4671cb0ef41Sopenharmony_ci  assert.strictEqual(C.someTask.mock.restore(), undefined);
4681cb0ef41Sopenharmony_ci  const CAfterRestoreKeepsDescriptor = Object.getOwnPropertyDescriptor(C, C.someTask.name);
4691cb0ef41Sopenharmony_ci  assert.ok(CAfterRestoreKeepsDescriptor);
4701cb0ef41Sopenharmony_ci});
4711cb0ef41Sopenharmony_ci
4721cb0ef41Sopenharmony_citest('mocked functions report thrown errors', (t) => {
4731cb0ef41Sopenharmony_ci  const testError = new Error('test error');
4741cb0ef41Sopenharmony_ci  const fn = t.mock.fn(() => {
4751cb0ef41Sopenharmony_ci    throw testError;
4761cb0ef41Sopenharmony_ci  });
4771cb0ef41Sopenharmony_ci
4781cb0ef41Sopenharmony_ci  assert.throws(fn, /test error/);
4791cb0ef41Sopenharmony_ci  assert.strictEqual(fn.mock.calls.length, 1);
4801cb0ef41Sopenharmony_ci
4811cb0ef41Sopenharmony_ci  const call = fn.mock.calls[0];
4821cb0ef41Sopenharmony_ci
4831cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, []);
4841cb0ef41Sopenharmony_ci  assert.strictEqual(call.error, testError);
4851cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, undefined);
4861cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, undefined);
4871cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, undefined);
4881cb0ef41Sopenharmony_ci});
4891cb0ef41Sopenharmony_ci
4901cb0ef41Sopenharmony_citest('mocked constructors report thrown errors', (t) => {
4911cb0ef41Sopenharmony_ci  const testError = new Error('test error');
4921cb0ef41Sopenharmony_ci  class Clazz {
4931cb0ef41Sopenharmony_ci    constructor() {
4941cb0ef41Sopenharmony_ci      throw testError;
4951cb0ef41Sopenharmony_ci    }
4961cb0ef41Sopenharmony_ci  }
4971cb0ef41Sopenharmony_ci
4981cb0ef41Sopenharmony_ci  const ctor = t.mock.fn(Clazz);
4991cb0ef41Sopenharmony_ci
5001cb0ef41Sopenharmony_ci  assert.throws(() => {
5011cb0ef41Sopenharmony_ci    new ctor();
5021cb0ef41Sopenharmony_ci  }, /test error/);
5031cb0ef41Sopenharmony_ci  assert.strictEqual(ctor.mock.calls.length, 1);
5041cb0ef41Sopenharmony_ci
5051cb0ef41Sopenharmony_ci  const call = ctor.mock.calls[0];
5061cb0ef41Sopenharmony_ci
5071cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, []);
5081cb0ef41Sopenharmony_ci  assert.strictEqual(call.error, testError);
5091cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, undefined);
5101cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, Clazz);
5111cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, undefined);
5121cb0ef41Sopenharmony_ci});
5131cb0ef41Sopenharmony_ci
5141cb0ef41Sopenharmony_citest('mocks a function', (t) => {
5151cb0ef41Sopenharmony_ci  const sum = (arg1, arg2) => arg1 + arg2;
5161cb0ef41Sopenharmony_ci  const difference = (arg1, arg2) => arg1 - arg2;
5171cb0ef41Sopenharmony_ci  const fn = t.mock.fn(sum, difference);
5181cb0ef41Sopenharmony_ci
5191cb0ef41Sopenharmony_ci  assert.strictEqual(fn.mock.calls.length, 0);
5201cb0ef41Sopenharmony_ci  assert.strictEqual(fn(3, 4), -1);
5211cb0ef41Sopenharmony_ci  assert.strictEqual(fn(9, 1), 8);
5221cb0ef41Sopenharmony_ci  assert.strictEqual(fn.mock.calls.length, 2);
5231cb0ef41Sopenharmony_ci
5241cb0ef41Sopenharmony_ci  let call = fn.mock.calls[0];
5251cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, [3, 4]);
5261cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, -1);
5271cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, undefined);
5281cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, undefined);
5291cb0ef41Sopenharmony_ci
5301cb0ef41Sopenharmony_ci  call = fn.mock.calls[1];
5311cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, [9, 1]);
5321cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, 8);
5331cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, undefined);
5341cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, undefined);
5351cb0ef41Sopenharmony_ci
5361cb0ef41Sopenharmony_ci  assert.strictEqual(fn.mock.restore(), undefined);
5371cb0ef41Sopenharmony_ci  assert.strictEqual(fn(2, 11), 13);
5381cb0ef41Sopenharmony_ci});
5391cb0ef41Sopenharmony_ci
5401cb0ef41Sopenharmony_citest('mocks a constructor', (t) => {
5411cb0ef41Sopenharmony_ci  class ParentClazz {
5421cb0ef41Sopenharmony_ci    constructor(c) {
5431cb0ef41Sopenharmony_ci      this.c = c;
5441cb0ef41Sopenharmony_ci    }
5451cb0ef41Sopenharmony_ci  }
5461cb0ef41Sopenharmony_ci
5471cb0ef41Sopenharmony_ci  class Clazz extends ParentClazz {
5481cb0ef41Sopenharmony_ci    #privateValue;
5491cb0ef41Sopenharmony_ci
5501cb0ef41Sopenharmony_ci    constructor(a, b) {
5511cb0ef41Sopenharmony_ci      super(a + b);
5521cb0ef41Sopenharmony_ci      this.a = a;
5531cb0ef41Sopenharmony_ci      this.#privateValue = b;
5541cb0ef41Sopenharmony_ci    }
5551cb0ef41Sopenharmony_ci
5561cb0ef41Sopenharmony_ci    getPrivateValue() {
5571cb0ef41Sopenharmony_ci      return this.#privateValue;
5581cb0ef41Sopenharmony_ci    }
5591cb0ef41Sopenharmony_ci  }
5601cb0ef41Sopenharmony_ci
5611cb0ef41Sopenharmony_ci  class MockClazz {
5621cb0ef41Sopenharmony_ci    #privateValue;
5631cb0ef41Sopenharmony_ci
5641cb0ef41Sopenharmony_ci    constructor(z) {
5651cb0ef41Sopenharmony_ci      this.z = z;
5661cb0ef41Sopenharmony_ci    }
5671cb0ef41Sopenharmony_ci  }
5681cb0ef41Sopenharmony_ci
5691cb0ef41Sopenharmony_ci  const ctor = t.mock.fn(Clazz, MockClazz);
5701cb0ef41Sopenharmony_ci  const instance = new ctor(42, 85);
5711cb0ef41Sopenharmony_ci
5721cb0ef41Sopenharmony_ci  assert(!(instance instanceof MockClazz));
5731cb0ef41Sopenharmony_ci  assert(instance instanceof Clazz);
5741cb0ef41Sopenharmony_ci  assert(instance instanceof ParentClazz);
5751cb0ef41Sopenharmony_ci  assert.strictEqual(instance.a, undefined);
5761cb0ef41Sopenharmony_ci  assert.strictEqual(instance.c, undefined);
5771cb0ef41Sopenharmony_ci  assert.strictEqual(instance.z, 42);
5781cb0ef41Sopenharmony_ci  assert.strictEqual(ctor.mock.calls.length, 1);
5791cb0ef41Sopenharmony_ci
5801cb0ef41Sopenharmony_ci  const call = ctor.mock.calls[0];
5811cb0ef41Sopenharmony_ci
5821cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, [42, 85]);
5831cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, instance);
5841cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, Clazz);
5851cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, instance);
5861cb0ef41Sopenharmony_ci  assert.throws(() => {
5871cb0ef41Sopenharmony_ci    instance.getPrivateValue();
5881cb0ef41Sopenharmony_ci  }, /TypeError: Cannot read private member #privateValue /);
5891cb0ef41Sopenharmony_ci});
5901cb0ef41Sopenharmony_ci
5911cb0ef41Sopenharmony_citest('mocks an object method', (t) => {
5921cb0ef41Sopenharmony_ci  const obj = {
5931cb0ef41Sopenharmony_ci    prop: 5,
5941cb0ef41Sopenharmony_ci    method(a, b) {
5951cb0ef41Sopenharmony_ci      return a + b + this.prop;
5961cb0ef41Sopenharmony_ci    },
5971cb0ef41Sopenharmony_ci  };
5981cb0ef41Sopenharmony_ci
5991cb0ef41Sopenharmony_ci  function mockMethod(a) {
6001cb0ef41Sopenharmony_ci    return a + this.prop;
6011cb0ef41Sopenharmony_ci  }
6021cb0ef41Sopenharmony_ci
6031cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method(1, 3), 9);
6041cb0ef41Sopenharmony_ci  t.mock.method(obj, 'method', mockMethod);
6051cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method.mock.calls.length, 0);
6061cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method(1, 3), 6);
6071cb0ef41Sopenharmony_ci
6081cb0ef41Sopenharmony_ci  const call = obj.method.mock.calls[0];
6091cb0ef41Sopenharmony_ci
6101cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, [1, 3]);
6111cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, 6);
6121cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, undefined);
6131cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, obj);
6141cb0ef41Sopenharmony_ci
6151cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method.mock.restore(), undefined);
6161cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method(1, 3), 9);
6171cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method.mock, undefined);
6181cb0ef41Sopenharmony_ci});
6191cb0ef41Sopenharmony_ci
6201cb0ef41Sopenharmony_citest('mocks a getter', (t) => {
6211cb0ef41Sopenharmony_ci  const obj = {
6221cb0ef41Sopenharmony_ci    prop: 5,
6231cb0ef41Sopenharmony_ci    get method() {
6241cb0ef41Sopenharmony_ci      return this.prop;
6251cb0ef41Sopenharmony_ci    },
6261cb0ef41Sopenharmony_ci  };
6271cb0ef41Sopenharmony_ci
6281cb0ef41Sopenharmony_ci  function mockMethod() {
6291cb0ef41Sopenharmony_ci    return this.prop - 1;
6301cb0ef41Sopenharmony_ci  }
6311cb0ef41Sopenharmony_ci
6321cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method, 5);
6331cb0ef41Sopenharmony_ci
6341cb0ef41Sopenharmony_ci  const getter = t.mock.method(obj, 'method', mockMethod, { getter: true });
6351cb0ef41Sopenharmony_ci
6361cb0ef41Sopenharmony_ci  assert.strictEqual(getter.mock.calls.length, 0);
6371cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method, 4);
6381cb0ef41Sopenharmony_ci
6391cb0ef41Sopenharmony_ci  const call = getter.mock.calls[0];
6401cb0ef41Sopenharmony_ci
6411cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, []);
6421cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, 4);
6431cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, undefined);
6441cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, obj);
6451cb0ef41Sopenharmony_ci
6461cb0ef41Sopenharmony_ci  assert.strictEqual(getter.mock.restore(), undefined);
6471cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method, 5);
6481cb0ef41Sopenharmony_ci});
6491cb0ef41Sopenharmony_ci
6501cb0ef41Sopenharmony_citest('mocks a setter', (t) => {
6511cb0ef41Sopenharmony_ci  const obj = {
6521cb0ef41Sopenharmony_ci    prop: 100,
6531cb0ef41Sopenharmony_ci    // eslint-disable-next-line accessor-pairs
6541cb0ef41Sopenharmony_ci    set method(val) {
6551cb0ef41Sopenharmony_ci      this.prop = val;
6561cb0ef41Sopenharmony_ci    },
6571cb0ef41Sopenharmony_ci  };
6581cb0ef41Sopenharmony_ci
6591cb0ef41Sopenharmony_ci  function mockMethod(val) {
6601cb0ef41Sopenharmony_ci    this.prop = -val;
6611cb0ef41Sopenharmony_ci  }
6621cb0ef41Sopenharmony_ci
6631cb0ef41Sopenharmony_ci  assert.strictEqual(obj.prop, 100);
6641cb0ef41Sopenharmony_ci  obj.method = 88;
6651cb0ef41Sopenharmony_ci  assert.strictEqual(obj.prop, 88);
6661cb0ef41Sopenharmony_ci
6671cb0ef41Sopenharmony_ci  const setter = t.mock.method(obj, 'method', mockMethod, { setter: true });
6681cb0ef41Sopenharmony_ci
6691cb0ef41Sopenharmony_ci  assert.strictEqual(setter.mock.calls.length, 0);
6701cb0ef41Sopenharmony_ci  obj.method = 77;
6711cb0ef41Sopenharmony_ci  assert.strictEqual(obj.prop, -77);
6721cb0ef41Sopenharmony_ci  assert.strictEqual(setter.mock.calls.length, 1);
6731cb0ef41Sopenharmony_ci
6741cb0ef41Sopenharmony_ci  const call = setter.mock.calls[0];
6751cb0ef41Sopenharmony_ci
6761cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, [77]);
6771cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, undefined);
6781cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, undefined);
6791cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, obj);
6801cb0ef41Sopenharmony_ci
6811cb0ef41Sopenharmony_ci  assert.strictEqual(setter.mock.restore(), undefined);
6821cb0ef41Sopenharmony_ci  assert.strictEqual(obj.prop, -77);
6831cb0ef41Sopenharmony_ci  obj.method = 65;
6841cb0ef41Sopenharmony_ci  assert.strictEqual(obj.prop, 65);
6851cb0ef41Sopenharmony_ci});
6861cb0ef41Sopenharmony_ci
6871cb0ef41Sopenharmony_citest('mocks a getter with syntax sugar', (t) => {
6881cb0ef41Sopenharmony_ci  const obj = {
6891cb0ef41Sopenharmony_ci    prop: 5,
6901cb0ef41Sopenharmony_ci    get method() {
6911cb0ef41Sopenharmony_ci      return this.prop;
6921cb0ef41Sopenharmony_ci    },
6931cb0ef41Sopenharmony_ci  };
6941cb0ef41Sopenharmony_ci
6951cb0ef41Sopenharmony_ci  function mockMethod() {
6961cb0ef41Sopenharmony_ci    return this.prop - 1;
6971cb0ef41Sopenharmony_ci  }
6981cb0ef41Sopenharmony_ci  const getter = t.mock.getter(obj, 'method', mockMethod);
6991cb0ef41Sopenharmony_ci  assert.strictEqual(getter.mock.calls.length, 0);
7001cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method, 4);
7011cb0ef41Sopenharmony_ci
7021cb0ef41Sopenharmony_ci  const call = getter.mock.calls[0];
7031cb0ef41Sopenharmony_ci
7041cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, []);
7051cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, 4);
7061cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, undefined);
7071cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, obj);
7081cb0ef41Sopenharmony_ci
7091cb0ef41Sopenharmony_ci  assert.strictEqual(getter.mock.restore(), undefined);
7101cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method, 5);
7111cb0ef41Sopenharmony_ci});
7121cb0ef41Sopenharmony_ci
7131cb0ef41Sopenharmony_citest('mocks a setter with syntax sugar', (t) => {
7141cb0ef41Sopenharmony_ci  const obj = {
7151cb0ef41Sopenharmony_ci    prop: 100,
7161cb0ef41Sopenharmony_ci    // eslint-disable-next-line accessor-pairs
7171cb0ef41Sopenharmony_ci    set method(val) {
7181cb0ef41Sopenharmony_ci      this.prop = val;
7191cb0ef41Sopenharmony_ci    },
7201cb0ef41Sopenharmony_ci  };
7211cb0ef41Sopenharmony_ci
7221cb0ef41Sopenharmony_ci  function mockMethod(val) {
7231cb0ef41Sopenharmony_ci    this.prop = -val;
7241cb0ef41Sopenharmony_ci  }
7251cb0ef41Sopenharmony_ci
7261cb0ef41Sopenharmony_ci  assert.strictEqual(obj.prop, 100);
7271cb0ef41Sopenharmony_ci  obj.method = 88;
7281cb0ef41Sopenharmony_ci  assert.strictEqual(obj.prop, 88);
7291cb0ef41Sopenharmony_ci
7301cb0ef41Sopenharmony_ci  const setter = t.mock.setter(obj, 'method', mockMethod);
7311cb0ef41Sopenharmony_ci
7321cb0ef41Sopenharmony_ci  assert.strictEqual(setter.mock.calls.length, 0);
7331cb0ef41Sopenharmony_ci  obj.method = 77;
7341cb0ef41Sopenharmony_ci  assert.strictEqual(obj.prop, -77);
7351cb0ef41Sopenharmony_ci  assert.strictEqual(setter.mock.calls.length, 1);
7361cb0ef41Sopenharmony_ci
7371cb0ef41Sopenharmony_ci  const call = setter.mock.calls[0];
7381cb0ef41Sopenharmony_ci
7391cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, [77]);
7401cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, undefined);
7411cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, undefined);
7421cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, obj);
7431cb0ef41Sopenharmony_ci
7441cb0ef41Sopenharmony_ci  assert.strictEqual(setter.mock.restore(), undefined);
7451cb0ef41Sopenharmony_ci  assert.strictEqual(obj.prop, -77);
7461cb0ef41Sopenharmony_ci  obj.method = 65;
7471cb0ef41Sopenharmony_ci  assert.strictEqual(obj.prop, 65);
7481cb0ef41Sopenharmony_ci});
7491cb0ef41Sopenharmony_ci
7501cb0ef41Sopenharmony_citest('mocked functions match name and length', (t) => {
7511cb0ef41Sopenharmony_ci  function getNameAndLength(fn) {
7521cb0ef41Sopenharmony_ci    return {
7531cb0ef41Sopenharmony_ci      name: Object.getOwnPropertyDescriptor(fn, 'name'),
7541cb0ef41Sopenharmony_ci      length: Object.getOwnPropertyDescriptor(fn, 'length'),
7551cb0ef41Sopenharmony_ci    };
7561cb0ef41Sopenharmony_ci  }
7571cb0ef41Sopenharmony_ci
7581cb0ef41Sopenharmony_ci  function func1() {}
7591cb0ef41Sopenharmony_ci  const func2 = function(a) {}; // eslint-disable-line func-style
7601cb0ef41Sopenharmony_ci  const arrow = (a, b, c) => {};
7611cb0ef41Sopenharmony_ci  const obj = { method(a, b) {} };
7621cb0ef41Sopenharmony_ci
7631cb0ef41Sopenharmony_ci  assert.deepStrictEqual(
7641cb0ef41Sopenharmony_ci    getNameAndLength(func1),
7651cb0ef41Sopenharmony_ci    getNameAndLength(t.mock.fn(func1))
7661cb0ef41Sopenharmony_ci  );
7671cb0ef41Sopenharmony_ci  assert.deepStrictEqual(
7681cb0ef41Sopenharmony_ci    getNameAndLength(func2),
7691cb0ef41Sopenharmony_ci    getNameAndLength(t.mock.fn(func2))
7701cb0ef41Sopenharmony_ci  );
7711cb0ef41Sopenharmony_ci  assert.deepStrictEqual(
7721cb0ef41Sopenharmony_ci    getNameAndLength(arrow),
7731cb0ef41Sopenharmony_ci    getNameAndLength(t.mock.fn(arrow))
7741cb0ef41Sopenharmony_ci  );
7751cb0ef41Sopenharmony_ci  assert.deepStrictEqual(
7761cb0ef41Sopenharmony_ci    getNameAndLength(obj.method),
7771cb0ef41Sopenharmony_ci    getNameAndLength(t.mock.method(obj, 'method', func1))
7781cb0ef41Sopenharmony_ci  );
7791cb0ef41Sopenharmony_ci});
7801cb0ef41Sopenharmony_ci
7811cb0ef41Sopenharmony_citest('method() fails if method cannot be redefined', (t) => {
7821cb0ef41Sopenharmony_ci  const obj = {
7831cb0ef41Sopenharmony_ci    prop: 5,
7841cb0ef41Sopenharmony_ci  };
7851cb0ef41Sopenharmony_ci
7861cb0ef41Sopenharmony_ci  Object.defineProperty(obj, 'method', {
7871cb0ef41Sopenharmony_ci    configurable: false,
7881cb0ef41Sopenharmony_ci    value(a, b) {
7891cb0ef41Sopenharmony_ci      return a + b + this.prop;
7901cb0ef41Sopenharmony_ci    }
7911cb0ef41Sopenharmony_ci  });
7921cb0ef41Sopenharmony_ci
7931cb0ef41Sopenharmony_ci  function mockMethod(a) {
7941cb0ef41Sopenharmony_ci    return a + this.prop;
7951cb0ef41Sopenharmony_ci  }
7961cb0ef41Sopenharmony_ci
7971cb0ef41Sopenharmony_ci  assert.throws(() => {
7981cb0ef41Sopenharmony_ci    t.mock.method(obj, 'method', mockMethod);
7991cb0ef41Sopenharmony_ci  }, /Cannot redefine property: method/);
8001cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method(1, 3), 9);
8011cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method.mock, undefined);
8021cb0ef41Sopenharmony_ci});
8031cb0ef41Sopenharmony_ci
8041cb0ef41Sopenharmony_citest('method() fails if field is a property instead of a method', (t) => {
8051cb0ef41Sopenharmony_ci  const obj = {
8061cb0ef41Sopenharmony_ci    prop: 5,
8071cb0ef41Sopenharmony_ci    method: 100,
8081cb0ef41Sopenharmony_ci  };
8091cb0ef41Sopenharmony_ci
8101cb0ef41Sopenharmony_ci  function mockMethod(a) {
8111cb0ef41Sopenharmony_ci    return a + this.prop;
8121cb0ef41Sopenharmony_ci  }
8131cb0ef41Sopenharmony_ci
8141cb0ef41Sopenharmony_ci  assert.throws(() => {
8151cb0ef41Sopenharmony_ci    t.mock.method(obj, 'method', mockMethod);
8161cb0ef41Sopenharmony_ci  }, /The argument 'methodName' must be a method/);
8171cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method, 100);
8181cb0ef41Sopenharmony_ci  assert.strictEqual(obj.method.mock, undefined);
8191cb0ef41Sopenharmony_ci});
8201cb0ef41Sopenharmony_ci
8211cb0ef41Sopenharmony_citest('mocks can be auto-restored', (t) => {
8221cb0ef41Sopenharmony_ci  let cnt = 0;
8231cb0ef41Sopenharmony_ci
8241cb0ef41Sopenharmony_ci  function addOne() {
8251cb0ef41Sopenharmony_ci    cnt++;
8261cb0ef41Sopenharmony_ci    return cnt;
8271cb0ef41Sopenharmony_ci  }
8281cb0ef41Sopenharmony_ci
8291cb0ef41Sopenharmony_ci  function addTwo() {
8301cb0ef41Sopenharmony_ci    cnt += 2;
8311cb0ef41Sopenharmony_ci    return cnt;
8321cb0ef41Sopenharmony_ci  }
8331cb0ef41Sopenharmony_ci
8341cb0ef41Sopenharmony_ci  const fn = t.mock.fn(addOne, addTwo, { times: 2 });
8351cb0ef41Sopenharmony_ci
8361cb0ef41Sopenharmony_ci  assert.strictEqual(fn(), 2);
8371cb0ef41Sopenharmony_ci  assert.strictEqual(fn(), 4);
8381cb0ef41Sopenharmony_ci  assert.strictEqual(fn(), 5);
8391cb0ef41Sopenharmony_ci  assert.strictEqual(fn(), 6);
8401cb0ef41Sopenharmony_ci});
8411cb0ef41Sopenharmony_ci
8421cb0ef41Sopenharmony_citest('mock implementation can be changed dynamically', (t) => {
8431cb0ef41Sopenharmony_ci  let cnt = 0;
8441cb0ef41Sopenharmony_ci
8451cb0ef41Sopenharmony_ci  function addOne() {
8461cb0ef41Sopenharmony_ci    cnt++;
8471cb0ef41Sopenharmony_ci    return cnt;
8481cb0ef41Sopenharmony_ci  }
8491cb0ef41Sopenharmony_ci
8501cb0ef41Sopenharmony_ci  function addTwo() {
8511cb0ef41Sopenharmony_ci    cnt += 2;
8521cb0ef41Sopenharmony_ci    return cnt;
8531cb0ef41Sopenharmony_ci  }
8541cb0ef41Sopenharmony_ci
8551cb0ef41Sopenharmony_ci  function addThree() {
8561cb0ef41Sopenharmony_ci    cnt += 3;
8571cb0ef41Sopenharmony_ci    return cnt;
8581cb0ef41Sopenharmony_ci  }
8591cb0ef41Sopenharmony_ci
8601cb0ef41Sopenharmony_ci  const fn = t.mock.fn(addOne);
8611cb0ef41Sopenharmony_ci
8621cb0ef41Sopenharmony_ci  assert.strictEqual(fn.mock.callCount(), 0);
8631cb0ef41Sopenharmony_ci  assert.strictEqual(fn(), 1);
8641cb0ef41Sopenharmony_ci  assert.strictEqual(fn(), 2);
8651cb0ef41Sopenharmony_ci  assert.strictEqual(fn(), 3);
8661cb0ef41Sopenharmony_ci  assert.strictEqual(fn.mock.callCount(), 3);
8671cb0ef41Sopenharmony_ci
8681cb0ef41Sopenharmony_ci  fn.mock.mockImplementation(addTwo);
8691cb0ef41Sopenharmony_ci  assert.strictEqual(fn(), 5);
8701cb0ef41Sopenharmony_ci  assert.strictEqual(fn(), 7);
8711cb0ef41Sopenharmony_ci  assert.strictEqual(fn.mock.callCount(), 5);
8721cb0ef41Sopenharmony_ci
8731cb0ef41Sopenharmony_ci  fn.mock.restore();
8741cb0ef41Sopenharmony_ci  assert.strictEqual(fn(), 8);
8751cb0ef41Sopenharmony_ci  assert.strictEqual(fn(), 9);
8761cb0ef41Sopenharmony_ci  assert.strictEqual(fn.mock.callCount(), 7);
8771cb0ef41Sopenharmony_ci
8781cb0ef41Sopenharmony_ci  assert.throws(() => {
8791cb0ef41Sopenharmony_ci    fn.mock.mockImplementationOnce(common.mustNotCall(), 6);
8801cb0ef41Sopenharmony_ci  }, /The value of "onCall" is out of range\. It must be >= 7/);
8811cb0ef41Sopenharmony_ci
8821cb0ef41Sopenharmony_ci  fn.mock.mockImplementationOnce(addThree, 7);
8831cb0ef41Sopenharmony_ci  fn.mock.mockImplementationOnce(addTwo, 8);
8841cb0ef41Sopenharmony_ci  assert.strictEqual(fn(), 12);
8851cb0ef41Sopenharmony_ci  assert.strictEqual(fn(), 14);
8861cb0ef41Sopenharmony_ci  assert.strictEqual(fn(), 15);
8871cb0ef41Sopenharmony_ci  assert.strictEqual(fn.mock.callCount(), 10);
8881cb0ef41Sopenharmony_ci  fn.mock.mockImplementationOnce(addThree);
8891cb0ef41Sopenharmony_ci  assert.strictEqual(fn(), 18);
8901cb0ef41Sopenharmony_ci  assert.strictEqual(fn(), 19);
8911cb0ef41Sopenharmony_ci  assert.strictEqual(fn.mock.callCount(), 12);
8921cb0ef41Sopenharmony_ci});
8931cb0ef41Sopenharmony_ci
8941cb0ef41Sopenharmony_citest('local mocks are auto restored after the test finishes', async (t) => {
8951cb0ef41Sopenharmony_ci  const obj = {
8961cb0ef41Sopenharmony_ci    foo() {},
8971cb0ef41Sopenharmony_ci    bar() {},
8981cb0ef41Sopenharmony_ci  };
8991cb0ef41Sopenharmony_ci  const originalFoo = obj.foo;
9001cb0ef41Sopenharmony_ci  const originalBar = obj.bar;
9011cb0ef41Sopenharmony_ci
9021cb0ef41Sopenharmony_ci  assert.strictEqual(originalFoo, obj.foo);
9031cb0ef41Sopenharmony_ci  assert.strictEqual(originalBar, obj.bar);
9041cb0ef41Sopenharmony_ci
9051cb0ef41Sopenharmony_ci  const mockFoo = t.mock.method(obj, 'foo');
9061cb0ef41Sopenharmony_ci
9071cb0ef41Sopenharmony_ci  assert.strictEqual(mockFoo, obj.foo);
9081cb0ef41Sopenharmony_ci  assert.notStrictEqual(originalFoo, obj.foo);
9091cb0ef41Sopenharmony_ci  assert.strictEqual(originalBar, obj.bar);
9101cb0ef41Sopenharmony_ci
9111cb0ef41Sopenharmony_ci  t.beforeEach(() => {
9121cb0ef41Sopenharmony_ci    assert.strictEqual(mockFoo, obj.foo);
9131cb0ef41Sopenharmony_ci    assert.strictEqual(originalBar, obj.bar);
9141cb0ef41Sopenharmony_ci  });
9151cb0ef41Sopenharmony_ci
9161cb0ef41Sopenharmony_ci  t.afterEach(() => {
9171cb0ef41Sopenharmony_ci    assert.strictEqual(mockFoo, obj.foo);
9181cb0ef41Sopenharmony_ci    assert.notStrictEqual(originalBar, obj.bar);
9191cb0ef41Sopenharmony_ci  });
9201cb0ef41Sopenharmony_ci
9211cb0ef41Sopenharmony_ci  await t.test('creates mocks that are auto restored', (t) => {
9221cb0ef41Sopenharmony_ci    const mockBar = t.mock.method(obj, 'bar');
9231cb0ef41Sopenharmony_ci
9241cb0ef41Sopenharmony_ci    assert.strictEqual(mockFoo, obj.foo);
9251cb0ef41Sopenharmony_ci    assert.strictEqual(mockBar, obj.bar);
9261cb0ef41Sopenharmony_ci    assert.notStrictEqual(originalBar, obj.bar);
9271cb0ef41Sopenharmony_ci  });
9281cb0ef41Sopenharmony_ci
9291cb0ef41Sopenharmony_ci  assert.strictEqual(mockFoo, obj.foo);
9301cb0ef41Sopenharmony_ci  assert.strictEqual(originalBar, obj.bar);
9311cb0ef41Sopenharmony_ci});
9321cb0ef41Sopenharmony_ci
9331cb0ef41Sopenharmony_citest('reset mock calls', (t) => {
9341cb0ef41Sopenharmony_ci  const sum = (arg1, arg2) => arg1 + arg2;
9351cb0ef41Sopenharmony_ci  const difference = (arg1, arg2) => arg1 - arg2;
9361cb0ef41Sopenharmony_ci  const fn = t.mock.fn(sum, difference);
9371cb0ef41Sopenharmony_ci
9381cb0ef41Sopenharmony_ci  assert.strictEqual(fn(1, 2), -1);
9391cb0ef41Sopenharmony_ci  assert.strictEqual(fn(2, 1), 1);
9401cb0ef41Sopenharmony_ci  assert.strictEqual(fn.mock.calls.length, 2);
9411cb0ef41Sopenharmony_ci  assert.strictEqual(fn.mock.callCount(), 2);
9421cb0ef41Sopenharmony_ci
9431cb0ef41Sopenharmony_ci  fn.mock.resetCalls();
9441cb0ef41Sopenharmony_ci  assert.strictEqual(fn.mock.calls.length, 0);
9451cb0ef41Sopenharmony_ci  assert.strictEqual(fn.mock.callCount(), 0);
9461cb0ef41Sopenharmony_ci
9471cb0ef41Sopenharmony_ci  assert.strictEqual(fn(3, 2), 1);
9481cb0ef41Sopenharmony_ci});
9491cb0ef41Sopenharmony_ci
9501cb0ef41Sopenharmony_citest('uses top level mock', () => {
9511cb0ef41Sopenharmony_ci  function sum(a, b) {
9521cb0ef41Sopenharmony_ci    return a + b;
9531cb0ef41Sopenharmony_ci  }
9541cb0ef41Sopenharmony_ci
9551cb0ef41Sopenharmony_ci  function difference(a, b) {
9561cb0ef41Sopenharmony_ci    return a - b;
9571cb0ef41Sopenharmony_ci  }
9581cb0ef41Sopenharmony_ci
9591cb0ef41Sopenharmony_ci  const fn = mock.fn(sum, difference);
9601cb0ef41Sopenharmony_ci
9611cb0ef41Sopenharmony_ci  assert.strictEqual(fn.mock.calls.length, 0);
9621cb0ef41Sopenharmony_ci  assert.strictEqual(fn(3, 4), -1);
9631cb0ef41Sopenharmony_ci  assert.strictEqual(fn.mock.calls.length, 1);
9641cb0ef41Sopenharmony_ci  mock.reset();
9651cb0ef41Sopenharmony_ci  assert.strictEqual(fn(3, 4), 7);
9661cb0ef41Sopenharmony_ci  assert.strictEqual(fn.mock.calls.length, 2);
9671cb0ef41Sopenharmony_ci});
9681cb0ef41Sopenharmony_ci
9691cb0ef41Sopenharmony_citest('the getter and setter options cannot be used together', (t) => {
9701cb0ef41Sopenharmony_ci  assert.throws(() => {
9711cb0ef41Sopenharmony_ci    t.mock.method({}, 'method', { getter: true, setter: true });
9721cb0ef41Sopenharmony_ci  }, /The property 'options\.setter' cannot be used with 'options\.getter'/);
9731cb0ef41Sopenharmony_ci});
9741cb0ef41Sopenharmony_ci
9751cb0ef41Sopenharmony_citest('method names must be strings or symbols', (t) => {
9761cb0ef41Sopenharmony_ci  const symbol = Symbol();
9771cb0ef41Sopenharmony_ci  const obj = {
9781cb0ef41Sopenharmony_ci    method() {},
9791cb0ef41Sopenharmony_ci    [symbol]() {},
9801cb0ef41Sopenharmony_ci  };
9811cb0ef41Sopenharmony_ci
9821cb0ef41Sopenharmony_ci  t.mock.method(obj, 'method');
9831cb0ef41Sopenharmony_ci  t.mock.method(obj, symbol);
9841cb0ef41Sopenharmony_ci
9851cb0ef41Sopenharmony_ci  assert.throws(() => {
9861cb0ef41Sopenharmony_ci    t.mock.method(obj, {});
9871cb0ef41Sopenharmony_ci  }, /The "methodName" argument must be one of type string or symbol/);
9881cb0ef41Sopenharmony_ci});
9891cb0ef41Sopenharmony_ci
9901cb0ef41Sopenharmony_citest('the times option must be an integer >= 1', (t) => {
9911cb0ef41Sopenharmony_ci  assert.throws(() => {
9921cb0ef41Sopenharmony_ci    t.mock.fn({ times: null });
9931cb0ef41Sopenharmony_ci  }, /The "options\.times" property must be of type number/);
9941cb0ef41Sopenharmony_ci
9951cb0ef41Sopenharmony_ci  assert.throws(() => {
9961cb0ef41Sopenharmony_ci    t.mock.fn({ times: 0 });
9971cb0ef41Sopenharmony_ci  }, /The value of "options\.times" is out of range/);
9981cb0ef41Sopenharmony_ci
9991cb0ef41Sopenharmony_ci  assert.throws(() => {
10001cb0ef41Sopenharmony_ci    t.mock.fn(() => {}, { times: 3.14159 });
10011cb0ef41Sopenharmony_ci  }, /The value of "options\.times" is out of range/);
10021cb0ef41Sopenharmony_ci});
10031cb0ef41Sopenharmony_ci
10041cb0ef41Sopenharmony_citest('spies on a class prototype method', (t) => {
10051cb0ef41Sopenharmony_ci  class Clazz {
10061cb0ef41Sopenharmony_ci    constructor(c) {
10071cb0ef41Sopenharmony_ci      this.c = c;
10081cb0ef41Sopenharmony_ci    }
10091cb0ef41Sopenharmony_ci
10101cb0ef41Sopenharmony_ci    getC() {
10111cb0ef41Sopenharmony_ci      return this.c;
10121cb0ef41Sopenharmony_ci    }
10131cb0ef41Sopenharmony_ci  }
10141cb0ef41Sopenharmony_ci
10151cb0ef41Sopenharmony_ci  const instance = new Clazz(85);
10161cb0ef41Sopenharmony_ci
10171cb0ef41Sopenharmony_ci  assert.strictEqual(instance.getC(), 85);
10181cb0ef41Sopenharmony_ci  t.mock.method(Clazz.prototype, 'getC');
10191cb0ef41Sopenharmony_ci
10201cb0ef41Sopenharmony_ci  assert.strictEqual(instance.getC.mock.calls.length, 0);
10211cb0ef41Sopenharmony_ci  assert.strictEqual(instance.getC(), 85);
10221cb0ef41Sopenharmony_ci  assert.strictEqual(instance.getC.mock.calls.length, 1);
10231cb0ef41Sopenharmony_ci  assert.strictEqual(Clazz.prototype.getC.mock.calls.length, 1);
10241cb0ef41Sopenharmony_ci
10251cb0ef41Sopenharmony_ci  const call = instance.getC.mock.calls[0];
10261cb0ef41Sopenharmony_ci  assert.deepStrictEqual(call.arguments, []);
10271cb0ef41Sopenharmony_ci  assert.strictEqual(call.result, 85);
10281cb0ef41Sopenharmony_ci  assert.strictEqual(call.error, undefined);
10291cb0ef41Sopenharmony_ci  assert.strictEqual(call.target, undefined);
10301cb0ef41Sopenharmony_ci  assert.strictEqual(call.this, instance);
10311cb0ef41Sopenharmony_ci});
10321cb0ef41Sopenharmony_ci
10331cb0ef41Sopenharmony_citest('getter() fails if getter options set to false', (t) => {
10341cb0ef41Sopenharmony_ci  assert.throws(() => {
10351cb0ef41Sopenharmony_ci    t.mock.getter({}, 'method', { getter: false });
10361cb0ef41Sopenharmony_ci  }, /The property 'options\.getter' cannot be false/);
10371cb0ef41Sopenharmony_ci});
10381cb0ef41Sopenharmony_ci
10391cb0ef41Sopenharmony_citest('setter() fails if setter options set to false', (t) => {
10401cb0ef41Sopenharmony_ci  assert.throws(() => {
10411cb0ef41Sopenharmony_ci    t.mock.setter({}, 'method', { setter: false });
10421cb0ef41Sopenharmony_ci  }, /The property 'options\.setter' cannot be false/);
10431cb0ef41Sopenharmony_ci});
10441cb0ef41Sopenharmony_ci
10451cb0ef41Sopenharmony_citest('getter() fails if setter options is true', (t) => {
10461cb0ef41Sopenharmony_ci  assert.throws(() => {
10471cb0ef41Sopenharmony_ci    t.mock.getter({}, 'method', { setter: true });
10481cb0ef41Sopenharmony_ci  }, /The property 'options\.setter' cannot be used with 'options\.getter'/);
10491cb0ef41Sopenharmony_ci});
10501cb0ef41Sopenharmony_ci
10511cb0ef41Sopenharmony_citest('setter() fails if getter options is true', (t) => {
10521cb0ef41Sopenharmony_ci  assert.throws(() => {
10531cb0ef41Sopenharmony_ci    t.mock.setter({}, 'method', { getter: true });
10541cb0ef41Sopenharmony_ci  }, /The property 'options\.setter' cannot be used with 'options\.getter'/);
10551cb0ef41Sopenharmony_ci});
1056