Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4,676 changes: 1,185 additions & 3,491 deletions package-lock.json

Large diffs are not rendered by default.

33 changes: 13 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,24 @@
"author": "",
"license": "ISC",
"devDependencies": {
"@ava/typescript": "^1.1.0",
"@types/node": "latest",
"ava": "^1.2.0",
"ts-node": "^7.0.0",
"ava": "^3.3.0",
"typescript": "^3.0.0"
},
"ava": {
"files": [
"./dist/spec/**/*.js"
],
"sources": [
"./src/**/*.ts",
"./spec/**/*.test.ts",
"./dist/src/**/*.js",
"./dist/spec/**/*.test.js"
],
"typescript": {
"rewritePaths": {
"/": "dist/"
},
"extensions": [
"ts"
]
},
"cache": false,
"concurrency": 1,
"failFast": true,
"failWithoutAssertions": true,
"compileEnhancements": false,
"extensions": [
"ts"
],
"require": [
"ts-node/register"
]
}
"failWithoutAssertions": true
},
"dependencies": {}
}
157 changes: 76 additions & 81 deletions spec/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@ export class Example {
return 1337;
}

set v(x: string|null|undefined) {
set v(x: string | null | undefined) {
}

received(stuff: number|string) {
received(stuff: number | string) {

}

returnPromise() {
return Promise.resolve(new Dummy());
}

foo(): string|undefined|null {
foo(): string | undefined | null {
return 'stuff';
}
bar (a: number, b?: number): number{
return a + b || 0
}
}

bar(a: number, b?: number): number {
return a + b || 0
}
}

let instance: Example;
Expand All @@ -46,20 +46,15 @@ function initialize() {
substitute = Substitute.for<Example>();
};

test('can call received twice', t => {
test('can call received twice', t => {
initialize();

substitute.c('blah', 'fuzz');
const expectedMessage = 'Expected 1337 calls to the method c with arguments [\'foo\', \'bar\'], but received none of such calls.\nAll calls received to method c:\n-> call with arguments [\'blah\', \'fuzz\']'
t.throws(() => substitute.received(1337).c('foo', 'bar'), { message: expectedMessage });

t.throws(() => substitute.received(1337).c('foo', 'bar'),
`Expected 1337 calls to the method c with arguments ['foo', 'bar'], but received none of such calls.
All calls received to method c:
-> call with arguments ['blah', 'fuzz']`);

t.throws(() => substitute.received(2117).c('foo', 'bar'),
`Expected 2117 calls to the method c with arguments ['foo', 'bar'], but received none of such calls.
All calls received to method c:
-> call with arguments ['blah', 'fuzz']`);
const expectedMessage2 = 'Expected 2117 calls to the method c with arguments [\'foo\', \'bar\'], but received none of such calls.\nAll calls received to method c:\n-> call with arguments [\'blah\', \'fuzz\']'
t.throws(() => substitute.received(2117).c('foo', 'bar'), { message: expectedMessage2 });
});

test('class string field get returns', t => {
Expand All @@ -75,7 +70,7 @@ test('class string field get returns', t => {

test('class with method called "received" can be used for call count verification when proxies are suspended', t => {
initialize();

Substitute.disableFor(substitute).received(2);

t.throws(() => substitute.received(2).received(2));
Expand All @@ -84,7 +79,7 @@ test('class with method called "received" can be used for call count verificatio

test('class with method called "received" can be used for call count verification', t => {
initialize();

Substitute.disableFor(substitute).received('foo');

t.notThrows(() => substitute.received(1).received('foo'));
Expand All @@ -93,15 +88,15 @@ test('class with method called "received" can be used for call count verificatio

test('partial mocks using function mimicks with all args', t => {
initialize();

substitute.c(Arg.all()).mimicks(instance.c);

t.deepEqual(substitute.c('a', 'b'), 'hello a world (b)');
});

test('class string field get received', t => {
initialize();

void substitute.a;
void substitute.a;
void substitute.a;
Expand All @@ -114,13 +109,13 @@ test('class string field get received', t => {

test('class string field set received', t => {
initialize();

substitute.v = undefined;
substitute.v = null;
substitute.v = 'hello';
substitute.v = 'hello';
substitute.v = 'world';

t.notThrows(() => substitute.received().v = 'hello');
t.notThrows(() => substitute.received(5).v = Arg.any());
t.notThrows(() => substitute.received().v = Arg.any());
Expand All @@ -135,9 +130,9 @@ test('class string field set received', t => {

test('class method returns with placeholder args', t => {
initialize();

substitute.c(Arg.any(), "there").returns("blah", "haha");

t.is(substitute.c("hi", "there"), 'blah');
t.is(substitute.c("his", "there"), 'haha');
t.is<any>(substitute.c("his", "there"), void 0);
Expand All @@ -146,15 +141,15 @@ test('class method returns with placeholder args', t => {

test('partial mocks using function mimicks with specific args', t => {
initialize();

substitute.c('a', 'b').mimicks(instance.c);

t.is(substitute.c('a', 'b'), 'hello a world (b)');
});

test('class method returns with specific args', t => {
initialize();

substitute.c("hi", "there").returns("blah", "haha");

t.is(substitute.c("hi", "there"), 'blah');
Expand All @@ -165,32 +160,32 @@ test('class method returns with specific args', t => {

test('returning other fake from promise works', async t => {
initialize();

const otherSubstitute = Substitute.for<Dummy>();
substitute.returnPromise().returns(Promise.resolve(otherSubstitute));
t.is(otherSubstitute, await substitute.returnPromise());
t.is(otherSubstitute, await substitute.returnPromise());
});

test('returning resolved promises works', async t => {
initialize();

substitute.returnPromise().returns(Promise.resolve(1338));

t.is(1338, await substitute.returnPromise());
});

test('class void returns', t => {
initialize();

substitute.foo().returns(void 0, null);

t.is(substitute.foo(), void 0);
t.is(substitute.foo(), null);
});
});

test('class method received', t => {
initialize();

void substitute.c("hi", "there");
void substitute.c("hi", "the1re");
void substitute.c("hi", "there");
Expand All @@ -201,92 +196,92 @@ test('class method received', t => {
t.notThrows(() => substitute.received(1).c('hi', 'the1re'));
t.notThrows(() => substitute.received().c('hi', 'there'));

t.throws(() => substitute.received(7).c('hi', 'there'),
`Expected 7 calls to the method c with arguments ['hi', 'there'], but received 4 of such calls.
All calls received to method c:
-> call with arguments ['hi', 'there']
-> call with arguments ['hi', 'the1re']
-> call with arguments ['hi', 'there']
-> call with arguments ['hi', 'there']
-> call with arguments ['hi', 'there']`);
const expectedMessage = 'Expected 7 calls to the method c with arguments [\'hi\', \'there\'], but received 4 of such calls.\n' +
'All calls received to method c:\n' +
'-> call with arguments [\'hi\', \'there\']\n' +
'-> call with arguments [\'hi\', \'the1re\']\n' +
'-> call with arguments [\'hi\', \'there\']\n' +
'-> call with arguments [\'hi\', \'there\']\n' +
'-> call with arguments [\'hi\', \'there\']'
t.throws(() => { substitute.received(7).c('hi', 'there') }, { message: expectedMessage });
});

test('received call matches after partial mocks using property instance mimicks', t => {
initialize();

substitute.d.mimicks(() => instance.d);
substitute.c('lala', 'bar');

substitute.received(1).c('lala', 'bar');
substitute.received(1).c('lala', 'bar');

t.notThrows(() => substitute.received(1).c('lala', 'bar'));
t.throws(() => substitute.received(2).c('lala', 'bar'),
`Expected 2 calls to the method c with arguments ['lala', 'bar'], but received 1 of such call.
All calls received to method c:
-> call with arguments ['lala', 'bar']`);
const expectedMessage = 'Expected 2 calls to the method c with arguments [\'lala\', \'bar\'], but received 1 of such call.\n' +
'All calls received to method c:\n' +
'-> call with arguments [\'lala\', \'bar\']'
t.throws(() => substitute.received(2).c('lala', 'bar'), { message: expectedMessage });

t.deepEqual(substitute.d, 1337);
});

test('partial mocks using property instance mimicks', t => {
initialize();

substitute.d.mimicks(() => instance.d);

t.deepEqual(substitute.d, 1337);
});

test('verifying with more arguments fails', t => {
initialize()
substitute.bar(1)
substitute.received().bar(1)
t.throws(() => substitute.received().bar(1, 2))
initialize()
substitute.bar(1)
substitute.received().bar(1)
t.throws(() => substitute.received().bar(1, 2))
})

test('verifying with less arguments fails', t => {
initialize()
substitute.bar(1, 2)
substitute.received().bar(1, 2)
t.throws(() => substitute.received().bar(1))
initialize()
substitute.bar(1, 2)
substitute.received().bar(1, 2)
t.throws(() => substitute.received().bar(1))
})

test('return with more arguments is not matched fails', t => {
initialize()
substitute.bar(1, 2).returns(3)
t.is(3, substitute.bar(1, 2))
t.is('function', typeof(substitute.bar(1)))
initialize()
substitute.bar(1, 2).returns(3)
t.is(3, substitute.bar(1, 2))
t.is('function', typeof (substitute.bar(1)))
})

test('return with less arguments is not matched', t => {
initialize()
substitute.bar(1).returns(3)
t.is(3, substitute.bar(1))
t.is('function', typeof(substitute.bar(1, 2).toString))
initialize()
substitute.bar(1).returns(3)
t.is(3, substitute.bar(1))
t.is('function', typeof (substitute.bar(1, 2).toString))
})

test('can stub multiple primitive return values', t => {
initialize()
substitute.bar(1).returns(2)
substitute.bar(2).returns(3)
t.is(2, substitute.bar(1))
t.is(3, substitute.bar(2))
initialize()
substitute.bar(1).returns(2)
substitute.bar(2).returns(3)
t.is(2, substitute.bar(1))
t.is(3, substitute.bar(2))
})

test('can stub multiple Arg values', t => {
initialize()
substitute.bar(Arg.is(x => x % 2 === 0)).returns(1)
substitute.bar(Arg.is(x => x % 2 !== 0)).returns(2)
t.is(1, substitute.bar(4))
t.is(2, substitute.bar(5))
initialize()
substitute.bar(Arg.is(x => x % 2 === 0)).returns(1)
substitute.bar(Arg.is(x => x % 2 !== 0)).returns(2)
t.is(1, substitute.bar(4))
t.is(2, substitute.bar(5))
})


test.skip('can stub primitive & Arg values', t => {
initialize()
substitute.bar(1).returns(2)
substitute.bar(Arg.any()).returns(3) // throws 'substitute.bar(...).returns is not a function'
t.is(5, substitute.bar(2))
t.is(2, substitute.bar(1))
t.is(3, substitute.bar(2))
initialize()
substitute.bar(1).returns(2)
substitute.bar(Arg.any()).returns(3) // throws 'substitute.bar(...).returns is not a function'
t.is(5, substitute.bar(2))
t.is(2, substitute.bar(1))
t.is(3, substitute.bar(2))
})
2 changes: 1 addition & 1 deletion spec/issues/51.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ test('issue 51 - All functions shares the same state', async t => {
try {
calculator.received().divide(1, 2);
} catch (e) {
t.regex(e.toString(), /Error: there are no mock for property: divide/);
t.regex(e.toString(), /Error: there is no mock for property: divide/);
}
});
Loading