Skip to content

Commit 1c83ac3

Browse files
committed
feat!: simplified and made more consistent stub api
BREAKING CHANGE: changed setExpectedArgs() and setReturnValue() to expects() and returns() respectively
1 parent 5495502 commit 1c83ac3

3 files changed

Lines changed: 21 additions & 21 deletions

File tree

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ The returned stub function can be called like a normal function and includes add
103103

104104
- `getCalls(): any[]` — Returns an array of all calls made to the stub (each call is an array of arguments).
105105
- `clear(): Stub` — Clears recorded calls and resets internal expectations.
106-
- `setExpectedArgs(...args: any[]): Stub` — Defines the exact arguments the stub expects to receive. If the stub is called with different arguments, it throws an error.
107-
- `setReturnValue(value: any): Stub` — Sets the value that the stub should return when called.
106+
- `expects(...args: any[]): Stub` — Defines the exact arguments the stub expects to receive. If the stub is called with different arguments, it throws an error.
107+
- `returns(value: any): Stub` — Sets the value that the stub should return when called.
108108
- `throws(error: Error): Stub` — Configures the stub to throw the specified error when called.
109109

110110
#### Stub Type Definition
@@ -113,8 +113,8 @@ The returned stub function can be called like a normal function and includes add
113113
export type Stub = ((...args: any[]) => any) & {
114114
getCalls: () => any[];
115115
clear: () => Stub;
116-
setExpectedArgs: (...expected: any[]) => Stub;
117-
setReturnValue: (value: any) => Stub;
116+
expects: (...expected: any[]) => Stub;
117+
returns: (value: any) => Stub;
118118
throws: (error: Error) => Stub;
119119
};
120120
```
@@ -123,8 +123,8 @@ export type Stub = ((...args: any[]) => any) & {
123123

124124
```ts
125125
const myStub = stub()
126-
.setExpectedArgs('hello', 123)
127-
.setReturnValue('world');
126+
.expects('hello', 123)
127+
.returns('world');
128128

129129
console.log(myStub('hello', 123)); // 'world'
130130
console.log(myStub.getCalls()); // [['hello', 123]]
@@ -140,8 +140,8 @@ You can give your stub name for easier debugging and error messages:
140140

141141
```ts
142142
const myStub = stub("my-stub")
143-
.setExpectedArgs('hello', 123)
144-
.setReturnValue('world');
143+
.expects('hello', 123)
144+
.returns('world');
145145

146146
console.log(myStub('hello', 123)); // 'world'
147147
console.log(myStub.getCalls()); // [['hello', 123]]

src/stub.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ test('stub.clear() returns the stub and empties call history and resets config',
2020

2121
const fn = stub();
2222

23-
fn.setExpectedArgs('x');
24-
fn.setReturnValue(123);
23+
fn.expects('x');
24+
fn.returns(123);
2525
fn('x');
2626

2727
assert.equal(fn.getCalls().length, 1);
@@ -38,9 +38,9 @@ test('stub.clear() returns the stub and empties call history and resets config',
3838

3939
});
4040

41-
test('stub.setReturnValue() returns the stub, and sets the return value of the stub', (assert) => {
41+
test('stub.returns() returns the stub, and sets the return value of the stub', (assert) => {
4242

43-
const fn = stub().setReturnValue('ok');
43+
const fn = stub().returns('ok');
4444

4545
const result = fn();
4646

@@ -50,7 +50,7 @@ test('stub.setReturnValue() returns the stub, and sets the return value of the s
5050

5151
test('stub throws on unexpected arguments', (assert) => {
5252

53-
const fn = stub().setExpectedArgs('expected', 42);
53+
const fn = stub().expects('expected', 42);
5454

5555
assert.throws(() => fn('wrong', 42), /Stub called with unexpected arguments/);
5656
assert.throws(() => fn(), /Stub called with unexpected arguments/);
@@ -60,7 +60,7 @@ test('stub throws on unexpected arguments', (assert) => {
6060

6161
test('stub does recursive, strict, compare-by-value equality check for expected args', (assert) => {
6262

63-
const fn = stub().setExpectedArgs({a: 1});
63+
const fn = stub().expects({a: 1});
6464

6565
assert.throws(() => fn({a: 1, b: 2}), /Stub called with unexpected arguments/);
6666
fn({a: 1}); // Should not throw
@@ -69,15 +69,15 @@ test('stub does recursive, strict, compare-by-value equality check for expected
6969

7070
fn(obj); // Should not throw
7171

72-
fn.setExpectedArgs(2);
72+
fn.expects(2);
7373
assert.throws(() => fn(3), /Stub called with unexpected arguments/);
7474
fn(2); // Should not throw
7575

7676
});
7777

7878
test('unexpected arg error contains name if supplied in constructor', (assert) => {
7979

80-
const fn = stub('my-awesome-stub').setExpectedArgs('expected', 42);
80+
const fn = stub('my-awesome-stub').expects('expected', 42);
8181

8282
assert.throws(() => fn('wrong', 42), /Stub "my-awesome-stub" called with unexpected arguments/);
8383
assert.throws(() => fn(), /Stub "my-awesome-stub" called with unexpected arguments/);

src/stub.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ function format(value: any): string {
1010
export type Stub = ((...args: any[]) => any) & {
1111
getCalls: () => any[]
1212
clear: () => Stub
13-
setExpectedArgs: (...expected: any[]) => Stub
14-
setReturnValue: (value: any) => Stub
13+
expects: (...expected: any[]) => Stub
14+
returns: (value: any) => Stub
1515
throws: (error: Error) => Stub
1616
};
1717

@@ -55,13 +55,13 @@ export function stub(name?: string): Stub {
5555
return fn;
5656

5757
};
58-
fn.setExpectedArgs = (...args: any[]): Stub => {
58+
fn.expects = (...expected: any[]): Stub => {
5959

60-
expectedArgs = args;
60+
expectedArgs = expected;
6161
return fn;
6262

6363
};
64-
fn.setReturnValue = (value: any): Stub => {
64+
fn.returns = (value: any): Stub => {
6565

6666
returnValue = value;
6767
return fn;

0 commit comments

Comments
 (0)