Skip to content

Commit 285a379

Browse files
feat: add examples
1 parent 4abef74 commit 285a379

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

src/content/docs/curriculum-help.mdx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,20 @@ spy.returns; // ["called by: arg + another arg", "called by: 2nd call + undefine
427427
spy.restore();
428428
```
429429

430+
Because the spy calls the original method with `.apply(this, args)`, `this` is preserved. This lets you spy on a shared prototype method to detect that an instance method was used without knowing the variable's name. For example, to check that `.push()` was called on an array:
431+
432+
```js
433+
const spy = __helpers.spyOn(Array.prototype, 'push');
434+
const arr = [1, 2];
435+
arr.push(3);
436+
spy.calls; // [[3]] — arguments to every push call
437+
spy.returns; // [3] — the new length push returned
438+
arr; // [1, 2, 3] — the real push still ran, since this === arr is preserved
439+
spy.restore();
440+
```
441+
442+
Spying on a prototype allows to capture the arguments the method is called with, but not the specific instance it is called on.
443+
430444
#### spyOnCallbacks
431445

432446
Available via `__helpers.spyOnCallbacks`, this function wraps a method in a spy object that records calls to callbacks passed to that method.
@@ -675,10 +689,13 @@ c.matches('const c = () => {}'); // true
675689
Returns an `Explorer` instance representing the assigned value of a variable, property, parameter, or property assignment. Returns an empty `Explorer` if there is no initializer.
676690

677691
```js
678-
const explorer = await __helpers.Explorer('const a = 1; const b = { x: 10 };');
679-
const { a, b } = explorer.variables;
692+
const explorer = await __helpers.Explorer(
693+
'const a = 1; const b = { x: 10 }; const c = a + 1;'
694+
);
695+
const { a, b, c } = explorer.variables;
680696
a.value.toString(); // "1"
681697
b.value.matches('{ x: 10 }'); // true
698+
c.value.toString(); // "a + 1"
682699
```
683700

684701
#### `objectProps`

0 commit comments

Comments
 (0)