@fluffy-spoon/substitute
Advanced tools
Comparing version 1.0.66 to 1.69.0
@@ -63,3 +63,2 @@ "use strict"; | ||
set: function (x) { | ||
console.log('define: ' + x); | ||
}, | ||
@@ -82,7 +81,23 @@ enumerable: true, | ||
var substitute; | ||
ava_1.default.beforeEach(function () { | ||
function initialize() { | ||
instance = new Example(); | ||
substitute = Index_1.Substitute.for(); | ||
} | ||
; | ||
ava_1.default('can call received twice', function (t) { | ||
initialize(); | ||
substitute.c('blah', 'fuzz'); | ||
t.throws(function () { return substitute.received(1337).c('foo', 'bar'); }, "Expected 1337 calls to the method c with arguments ['foo', 'bar'], but received none of such calls.\nAll calls received to method c:\n-> 1 call with arguments ['blah', 'fuzz']"); | ||
t.throws(function () { return substitute.received(2117).c('foo', 'bar'); }, "Expected 2117 calls to the method c with arguments ['foo', 'bar'], but received none of such calls.\nAll calls received to method c:\n-> 1 call with arguments ['blah', 'fuzz']"); | ||
}); | ||
ava_1.default('class string field get returns', function (t) { | ||
initialize(); | ||
substitute.a.returns("foo", "bar"); | ||
t.deepEqual(substitute.a, 'foo'); | ||
t.deepEqual(substitute.a, 'bar'); | ||
t.deepEqual(substitute.a, void 0); | ||
t.deepEqual(substitute.a, void 0); | ||
}); | ||
ava_1.default('class with method called "received" can be used for call count verification when proxies are suspended', function (t) { | ||
initialize(); | ||
Index_1.Substitute.disableFor(substitute).received(2); | ||
@@ -93,2 +108,3 @@ t.throws(function () { return substitute.received(2).received(2); }); | ||
ava_1.default('class with method called "received" can be used for call count verification', function (t) { | ||
initialize(); | ||
Index_1.Substitute.disableFor(substitute).received('foo'); | ||
@@ -99,11 +115,8 @@ t.notThrows(function () { return substitute.received(1).received('foo'); }); | ||
ava_1.default('partial mocks using function mimicks with all args', function (t) { | ||
initialize(); | ||
substitute.c(Index_1.Arg.all()).mimicks(instance.c); | ||
t.deepEqual(substitute.c('a', 'b'), 'hello a world (b)'); | ||
}); | ||
ava_1.default('can call received twice', function (t) { | ||
substitute.c('blah', 'fuzz'); | ||
t.throws(function () { return substitute.received(1337).c('foo', 'bar'); }, "Expected 1337 calls to the method c with arguments [foo, bar], but received none of such calls.\nAll calls received to method c:\n-> 1 call with arguments [blah, fuzz]"); | ||
t.throws(function () { return substitute.received(2117).c('foo', 'bar'); }, "Expected 2117 calls to the method c with arguments [foo, bar], but received none of such calls.\nAll calls received to method c:\n-> 1 call with arguments [blah, fuzz]"); | ||
}); | ||
ava_1.default('class string field get received', function (t) { | ||
initialize(); | ||
void substitute.a; | ||
@@ -118,2 +131,3 @@ void substitute.a; | ||
ava_1.default('class string field set received', function (t) { | ||
initialize(); | ||
substitute.v = undefined; | ||
@@ -135,2 +149,3 @@ substitute.v = null; | ||
ava_1.default('class method returns with placeholder args', function (t) { | ||
initialize(); | ||
substitute.c(Index_1.Arg.any(), "there").returns("blah", "haha"); | ||
@@ -143,2 +158,3 @@ t.is(substitute.c("hi", "there"), 'blah'); | ||
ava_1.default('partial mocks using function mimicks with specific args', function (t) { | ||
initialize(); | ||
substitute.c('a', 'b').mimicks(instance.c); | ||
@@ -148,2 +164,3 @@ t.is(substitute.c('a', 'b'), 'hello a world (b)'); | ||
ava_1.default('class method returns with specific args', function (t) { | ||
initialize(); | ||
substitute.c("hi", "there").returns("blah", "haha"); | ||
@@ -160,2 +177,3 @@ t.is(substitute.c("hi", "there"), 'blah'); | ||
case 0: | ||
initialize(); | ||
otherSubstitute = Index_1.Substitute.for(); | ||
@@ -177,2 +195,3 @@ substitute.returnPromise().returns(Promise.resolve(otherSubstitute)); | ||
case 0: | ||
initialize(); | ||
substitute.returnPromise().returns(Promise.resolve(1338)); | ||
@@ -189,2 +208,3 @@ _b = (_a = t).is; | ||
ava_1.default('class void returns', function (t) { | ||
initialize(); | ||
substitute.foo().returns(void 0, null); | ||
@@ -195,2 +215,3 @@ t.is(substitute.foo(), void 0); | ||
ava_1.default('class method received', function (t) { | ||
initialize(); | ||
void substitute.c("hi", "there"); | ||
@@ -204,5 +225,6 @@ void substitute.c("hi", "the1re"); | ||
t.notThrows(function () { return substitute.received().c('hi', 'there'); }); | ||
t.throws(function () { return substitute.received(7).c('hi', 'there'); }, "Expected 7 calls to the method c with arguments [hi, there], but received 4 of such calls.\nAll calls received to method c:\n-> 4 calls with arguments [hi, there]\n-> 1 call with arguments [hi, the1re]"); | ||
t.throws(function () { return substitute.received(7).c('hi', 'there'); }, "Expected 7 calls to the method c with arguments ['hi', 'there'], but received 4 of such calls.\nAll calls received to method c:\n-> 4 calls with arguments ['hi', 'there']\n-> 1 call with arguments ['hi', 'the1re']"); | ||
}); | ||
ava_1.default('received call matches after partial mocks using property instance mimicks', function (t) { | ||
initialize(); | ||
substitute.d.mimicks(function () { return instance.d; }); | ||
@@ -213,6 +235,7 @@ substitute.c('lala', 'bar'); | ||
t.notThrows(function () { return substitute.received(1).c('lala', 'bar'); }); | ||
t.throws(function () { return substitute.received(2).c('lala', 'bar'); }, "Expected 2 calls to the method c with arguments [lala, bar], but received 1 of such call.\nAll calls received to method c:\n-> 1 call with arguments [lala, bar]"); | ||
t.throws(function () { return substitute.received(2).c('lala', 'bar'); }, "Expected 2 calls to the method c with arguments ['lala', 'bar'], but received 1 of such call.\nAll calls received to method c:\n-> 1 call with arguments ['lala', 'bar']"); | ||
t.deepEqual(substitute.d, 1337); | ||
}); | ||
ava_1.default('partial mocks using property instance mimicks', function (t) { | ||
initialize(); | ||
substitute.d.mimicks(function () { return instance.d; }); | ||
@@ -222,2 +245,3 @@ t.deepEqual(substitute.d, 1337); | ||
ava_1.default('are arguments equal', function (t) { | ||
initialize(); | ||
t.true(Utilities_1.areArgumentsEqual(Index_1.Arg.any(), 'hi')); | ||
@@ -228,9 +252,2 @@ t.true(Utilities_1.areArgumentsEqual(Index_1.Arg.any('array'), ['foo', 'bar'])); | ||
}); | ||
ava_1.default('class string field get returns', function (t) { | ||
substitute.a.returns("foo", "bar"); | ||
t.deepEqual(substitute.a, 'foo'); | ||
t.deepEqual(substitute.a, 'bar'); | ||
t.deepEqual(substitute.a, void 0); | ||
t.deepEqual(substitute.a, void 0); | ||
}); | ||
//# sourceMappingURL=index.test.js.map |
export declare class Example { | ||
blocking(): number; | ||
a: string; | ||
blocking(): Promise<number>; | ||
} |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var _this = this; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -7,6 +43,10 @@ var ava_1 = require("ava"); | ||
function Example() { | ||
this.a = "1337"; | ||
} | ||
Example.prototype.blocking = function () { | ||
console.log('blocking'); | ||
return 123; | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, 123]; | ||
}); | ||
}); | ||
}; | ||
@@ -16,12 +56,27 @@ return Example; | ||
exports.Example = Example; | ||
var substitute; | ||
ava_1.default.beforeEach(function () { | ||
substitute = Index_1.Substitute.for(); | ||
}); | ||
ava_1.default('issue 9: can record method with 0 arguments', function (t) { | ||
substitute.blocking().returns(42); | ||
substitute.blocking(); | ||
substitute.received(1).blocking(); | ||
t.pass(); | ||
}); | ||
var substitute = Index_1.Substitute.for(); | ||
ava_1.default('issue 9: can record method with 0 arguments', function (t) { return __awaiter(_this, void 0, void 0, function () { | ||
function service() { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, substitute.blocking()]; | ||
case 1: return [2 /*return*/, _a.sent()]; | ||
} | ||
}); | ||
}); | ||
} | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
substitute.blocking().returns(Promise.resolve(42)); | ||
return [4 /*yield*/, service()]; | ||
case 1: | ||
_a.sent(); | ||
substitute.received(1).blocking(); | ||
t.pass(); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
//# sourceMappingURL=9.test.js.map |
@@ -8,3 +8,3 @@ "use strict"; | ||
return extendStatics(d, b); | ||
} | ||
}; | ||
return function (d, b) { | ||
@@ -11,0 +11,0 @@ extendStatics(d, b); |
@@ -54,5 +54,6 @@ "use strict"; | ||
return context.proxy; | ||
var returnValue = this.returns[this._callCount - 1]; | ||
// console.log('result', returnValue); | ||
return returnValue; | ||
var returnsArray = this.returns; | ||
if (returnsArray.length === 1) | ||
return returnsArray[0]; | ||
return returnsArray[this._callCount - 1]; | ||
}; | ||
@@ -59,0 +60,0 @@ FunctionState.prototype.set = function (context, property, value) { |
@@ -108,7 +108,10 @@ "use strict"; | ||
if (this.mimicks) { | ||
// console.log('mim-invoke'); | ||
return this.mimicks.apply(this.mimicks); | ||
} | ||
if (this.returns !== Nothing) | ||
return this.returns[this._callCount - 1]; | ||
if (this.returns !== Nothing) { | ||
var returnsArray = this.returns; | ||
if (returnsArray.length === 1) | ||
return returnsArray[0]; | ||
return returnsArray[this._callCount - 1]; | ||
} | ||
} | ||
@@ -115,0 +118,0 @@ context.initialState.assertCallCountMatchesExpectations(context.initialState.getPropertyStates, this.callCount, 'property', this.property, []); |
@@ -14,3 +14,5 @@ "use strict"; | ||
var Arguments_1 = require("./Arguments"); | ||
var util = require("util"); | ||
function stringifyArguments(args) { | ||
args = args.map(function (x) { return util.inspect(x); }); | ||
return args && args.length > 0 ? 'arguments [' + args.join(', ') + ']' : 'no arguments'; | ||
@@ -17,0 +19,0 @@ } |
{ | ||
"name": "@fluffy-spoon/substitute", | ||
"version": "1.0.66", | ||
"version": "1.69.0", | ||
"description": "An NSubstitute port to TypeScript called substitute.js.", | ||
@@ -15,5 +15,5 @@ "main": "dist/src/Index.js", | ||
"@types/node": "latest", | ||
"ava": "latest", | ||
"ava": "^1.2.0", | ||
"ts-node": "^7.0.1", | ||
"typescript": "^3.1.3" | ||
"typescript": "^3.3.1" | ||
}, | ||
@@ -20,0 +20,0 @@ "ava": { |
@@ -23,3 +23,2 @@ import test from 'ava'; | ||
set v(x: string) { | ||
console.log('define: ' + x); | ||
} | ||
@@ -43,8 +42,37 @@ | ||
test.beforeEach(() => { | ||
function initialize() { | ||
instance = new Example(); | ||
substitute = Substitute.for<Example>(); | ||
}; | ||
test('can call received twice', t => { | ||
initialize(); | ||
substitute.c('blah', 'fuzz'); | ||
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: | ||
-> 1 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: | ||
-> 1 call with arguments ['blah', 'fuzz']`); | ||
}); | ||
test('class string field get returns', t => { | ||
initialize(); | ||
substitute.a.returns("foo", "bar"); | ||
t.deepEqual(substitute.a, 'foo'); | ||
t.deepEqual(substitute.a, 'bar'); | ||
t.deepEqual(substitute.a, void 0); | ||
t.deepEqual(substitute.a, void 0); | ||
}); | ||
test('class with method called "received" can be used for call count verification when proxies are suspended', t => { | ||
initialize(); | ||
Substitute.disableFor(substitute).received(2); | ||
@@ -57,2 +85,4 @@ | ||
test('class with method called "received" can be used for call count verification', t => { | ||
initialize(); | ||
Substitute.disableFor(substitute).received('foo'); | ||
@@ -65,2 +95,4 @@ | ||
test('partial mocks using function mimicks with all args', t => { | ||
initialize(); | ||
substitute.c(Arg.all()).mimicks(instance.c); | ||
@@ -71,17 +103,5 @@ | ||
test('can call received twice', t => { | ||
substitute.c('blah', 'fuzz'); | ||
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: | ||
-> 1 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: | ||
-> 1 call with arguments [blah, fuzz]`); | ||
}); | ||
test('class string field get received', t => { | ||
initialize(); | ||
void substitute.a; | ||
@@ -98,2 +118,4 @@ void substitute.a; | ||
test('class string field set received', t => { | ||
initialize(); | ||
substitute.v = undefined; | ||
@@ -118,2 +140,4 @@ substitute.v = null; | ||
test('class method returns with placeholder args', t => { | ||
initialize(); | ||
substitute.c(Arg.any(), "there").returns("blah", "haha"); | ||
@@ -128,2 +152,4 @@ | ||
test('partial mocks using function mimicks with specific args', t => { | ||
initialize(); | ||
substitute.c('a', 'b').mimicks(instance.c); | ||
@@ -135,2 +161,4 @@ | ||
test('class method returns with specific args', t => { | ||
initialize(); | ||
substitute.c("hi", "there").returns("blah", "haha"); | ||
@@ -145,2 +173,4 @@ | ||
test('returning other fake from promise works', async t => { | ||
initialize(); | ||
const otherSubstitute = Substitute.for<Dummy>(); | ||
@@ -153,2 +183,4 @@ substitute.returnPromise().returns(Promise.resolve(otherSubstitute)); | ||
test('returning resolved promises works', async t => { | ||
initialize(); | ||
substitute.returnPromise().returns(Promise.resolve(1338)); | ||
@@ -160,2 +192,4 @@ | ||
test('class void returns', t => { | ||
initialize(); | ||
substitute.foo().returns(void 0, null); | ||
@@ -168,2 +202,4 @@ | ||
test('class method received', t => { | ||
initialize(); | ||
void substitute.c("hi", "there"); | ||
@@ -180,9 +216,11 @@ void substitute.c("hi", "the1re"); | ||
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. | ||
`Expected 7 calls to the method c with arguments ['hi', 'there'], but received 4 of such calls. | ||
All calls received to method c: | ||
-> 4 calls with arguments [hi, there] | ||
-> 1 call with arguments [hi, the1re]`); | ||
-> 4 calls with arguments ['hi', 'there'] | ||
-> 1 call with arguments ['hi', 'the1re']`); | ||
}); | ||
test('received call matches after partial mocks using property instance mimicks', t => { | ||
initialize(); | ||
substitute.d.mimicks(() => instance.d); | ||
@@ -196,5 +234,5 @@ substitute.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. | ||
`Expected 2 calls to the method c with arguments ['lala', 'bar'], but received 1 of such call. | ||
All calls received to method c: | ||
-> 1 call with arguments [lala, bar]`); | ||
-> 1 call with arguments ['lala', 'bar']`); | ||
@@ -205,2 +243,4 @@ t.deepEqual(substitute.d, 1337); | ||
test('partial mocks using property instance mimicks', t => { | ||
initialize(); | ||
substitute.d.mimicks(() => instance.d); | ||
@@ -212,2 +252,4 @@ | ||
test('are arguments equal', t => { | ||
initialize(); | ||
t.true(areArgumentsEqual(Arg.any(), 'hi')); | ||
@@ -218,11 +260,2 @@ t.true(areArgumentsEqual(Arg.any('array'), ['foo', 'bar'])); | ||
t.false(areArgumentsEqual(Arg.any('array'), 1337)); | ||
}); | ||
test('class string field get returns', t => { | ||
substitute.a.returns("foo", "bar"); | ||
t.deepEqual(substitute.a, 'foo'); | ||
t.deepEqual(substitute.a, 'bar'); | ||
t.deepEqual(substitute.a, void 0); | ||
t.deepEqual(substitute.a, void 0); | ||
}); |
@@ -7,4 +7,5 @@ import test from 'ava'; | ||
export class Example { | ||
blocking() { | ||
console.log('blocking'); | ||
a = "1337"; | ||
async blocking() { | ||
return 123; | ||
@@ -14,14 +15,12 @@ } | ||
let substitute: ObjectSubstitute<Example>; | ||
let substitute = Substitute.for<Example>(); | ||
test.beforeEach(() => { | ||
substitute = Substitute.for<Example>(); | ||
}); | ||
test('issue 9: can record method with 0 arguments', t => { | ||
substitute.blocking().returns(42); | ||
substitute.blocking(); | ||
substitute.received(1).blocking(); | ||
test('issue 9: can record method with 0 arguments', async t => { | ||
substitute.blocking().returns(Promise.resolve(42)); | ||
async function service() { | ||
return await substitute.blocking(); | ||
} | ||
await service(); | ||
substitute.received(1).blocking(); | ||
t.pass(); | ||
}); |
@@ -60,6 +60,7 @@ import { ContextState, PropertyKey } from "./ContextState"; | ||
const returnValue = this.returns[this._callCount - 1]; | ||
// console.log('result', returnValue); | ||
var returnsArray = this.returns as any[]; | ||
if(returnsArray.length === 1) | ||
return returnsArray[0]; | ||
return returnValue; | ||
return returnsArray[this._callCount-1]; | ||
} | ||
@@ -66,0 +67,0 @@ |
@@ -97,8 +97,12 @@ import { ContextState, PropertyKey } from "./ContextState"; | ||
if(this.mimicks) { | ||
// console.log('mim-invoke'); | ||
return this.mimicks.apply(this.mimicks); | ||
} | ||
if(this.returns !== Nothing) | ||
return this.returns[this._callCount-1]; | ||
if(this.returns !== Nothing) { | ||
var returnsArray = this.returns as any[]; | ||
if(returnsArray.length === 1) | ||
return returnsArray[0]; | ||
return returnsArray[this._callCount-1]; | ||
} | ||
} | ||
@@ -105,0 +109,0 @@ |
import { Argument, AllArguments } from "./Arguments"; | ||
import util = require('util') | ||
@@ -6,2 +7,3 @@ export type Call = {callCount: number, arguments?: any[]}; | ||
export function stringifyArguments(args: any[]) { | ||
args = args.map(x => util.inspect(x)); | ||
return args && args.length > 0 ? 'arguments [' + args.join(', ') + ']' : 'no arguments'; | ||
@@ -8,0 +10,0 @@ }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
151956
62
2021
0