New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@fluffy-spoon/substitute

Package Overview
Dependencies
Maintainers
1
Versions
123
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fluffy-spoon/substitute - npm Package Compare versions

Comparing version 1.0.8 to 1.0.9

dist/index.js

10

package.json
{
"name": "@fluffy-spoon/substitute",
"version": "1.0.8",
"version": "1.0.9",
"description": "",
"main": "src/index.ts",
"main": "dist/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "ava"
},
"author": "",
"license": "ISC",
"devDependencies": {
"typescript": "next",
"@types/node": "latest"
},
"homepage": "https://github.com/ffMathy/FluffySpoon.JavaScript.Testing"
}

@@ -1,1 +0,53 @@

# FluffySpoon.JavaScript.Testing
# FluffySpoon.JavaScript.Testing
More concretely named `@fluffy-spoon/substitute` on NPM is a TypeScript port of [NSubstitute](http://nsubstitute.github.io), which aims to provide a much more fluent mocking opportunity for strong-typed languages.
## Requirements
* `TypeScript^3.0.0`
## Usage
Experience full strong-typing of your fakes all the way, and let the TypeScript compiler help with all the dirty work! In the usage example given below, the `exFake` instance is strong-typed all the way, and can be used naturally in a fluent interface!
```
class Example {
a = "1337";
b = 1337;
c(arg1: string, arg2: string) {
return "hello " + arg1 + " world (" + arg2 + ")";
}
get d() {
return 1337;
}
set v(x) {
console.log('define: ' + x);
}
}
var exFake = Substitute.for<Example>();
exFake.a.returns("foo", "bar");
console.log(exFake.a); //prints "foo"
console.log(exFake.a); //prints "bar"
console.log(exFake.a); //prints undefined
exFake.b.returns(10, 30, 99);
console.log(exFake.b); //prints 10
console.log(exFake.b); //prints 30
console.log(exFake.b); //prints 99
console.log(exFake.b); //prints undefined
exFake.c("hi", "there").returns("blah", "haha", "oooh", "lala");
console.log(exFake.c("hi", "there")); //prints "blah"
console.log(exFake.c("hi", "there")); //prints "haha"
console.log(exFake.c("hi", "the1re")); //prints undefined (since it doesn't match the parameters)
console.log(exFake.c("hi", "there")); //prints "ooh"
console.log(exFake.c("something", "there")); //prints undefined (since it doesn't match the parameters)
exFake.d.returns(9);
console.log(exFake.d); //prints 9
```
## But how?
`@fluffy-spoon/substitute` works the same way that NSubstitute does, except that it uses the EcmaScript 6 `Proxy` class to produce the fakes. You can read more about how NSubstitute works to get inspired.

@@ -21,3 +21,4 @@ export type FunctionSubstitute<F extends any[], T> = (...args: F) => (T & {

currentReturnOffset: number,
proxy: any
proxy: any,
property: string|number
};

@@ -30,3 +31,4 @@

proxy: null,
currentReturnOffset: 0
currentReturnOffset: 0,
property: null
};

@@ -79,4 +81,8 @@

get: (target, property) => {
if(typeof property === 'symbol')
if(typeof property === 'symbol') {
if(property === Symbol.toPrimitive)
return () => void 0;
return void 0;
}

@@ -87,3 +93,3 @@ if(property === 'valueOf')

if(property === 'toString')
return target[property].toString();
return (target[property] || '').toString();

@@ -96,16 +102,15 @@ if(property === 'inspect')

if(property === 'returns') {
return (...args: any[]) => {
localRecord.shouldReturn = args;
};
}
if(property === 'returns')
return (...args: any[]) => localRecord.shouldReturn = args;
if(localRecord) {
if(localRecord && localRecord.property === property) {
if(localRecord.arguments)
return thisProxy;
return localRecord.shouldReturn[localRecord.currentReturnOffset];
return localRecord.shouldReturn[localRecord.currentReturnOffset++];
}
localRecord = createRecord();
localRecord.property = property;
return thisProxy;

@@ -118,42 +123,2 @@ }

}
}
class Example {
a = "1337";
b = 1337;
c(arg1: string, arg2: string) {
return "hello " + arg1 + " world (" + arg2 + ")";
}
get d() {
return 1337;
}
set v(x) {
console.log('define: ' + x);
}
}
var exFake = Substitute.for<Example>();
exFake.a.returns("foo", "bar");
console.log('returned', exFake.a);
console.log('returned', exFake.a);
exFake.b.returns(10, 30);
exFake.c("hi", "there").returns("blah", "haha");
exFake.d.returns(9);
console.log(exFake.a);
console.log(exFake.b);
console.log('assert');
console.log(exFake.c("hi", "there"));
console.log(exFake.c("hi", "the1re"));
console.log(exFake.c("hi", "there"));
console.log(exFake.c("hi", "there"));
console.log(exFake.c("something", "there"));
console.log(exFake.d);
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc