Gathering detailed insights and metrics for sinon-test
Gathering detailed insights and metrics for sinon-test
Gathering detailed insights and metrics for sinon-test
Gathering detailed insights and metrics for sinon-test
npm install sinon-test
Typescript
Module System
Node Version
NPM Version
95.6
Supply Chain
99
Quality
81.3
Maintenance
100
Vulnerability
99.6
License
JavaScript (96.75%)
Shell (3.25%)
Total Downloads
8,299,870
Last Day
5,508
Last Week
21,300
Last Month
103,428
Last Year
1,205,654
51 Stars
288 Commits
16 Forks
8 Watching
9 Branches
20 Contributors
Minified
Minified + Gzipped
Latest Version
3.1.6
Package Id
sinon-test@3.1.6
Unpacked Size
24.27 kB
Size
5.93 kB
File Count
10
NPM Version
9.8.0
Node Version
20.5.0
Publised On
15 May 2024
Cumulative downloads
Total Downloads
Last day
-24.2%
5,508
Compared to previous day
Last week
-14.4%
21,300
Compared to previous week
Last month
20.7%
103,428
Compared to previous month
Last year
-1.7%
1,205,654
Compared to previous year
Automatic sandbox setup and teardown for SinonJS
Instead of writing tedious setup and teardown code for each individual test case you can let Sinon do all the cleanup for you.
So instead of doing this (using Mocha syntax):
1var spy1; 2var spy2; 3 4afterEach(() => { 5 spy1.restore(); 6 spy2.restore(); 7}); 8 9it("should do something", () => { 10 spy1 = sinon.spy(myFunc); 11 spy2 = sinon.spy(myOtherFunc); 12 myFunc(1); 13 myFunc(2); 14 assert(spy1.calledWith(1)); 15 assert(spy1.calledWith(2)); 16});
You could write just this
1it( 2 "should do something", 3 test(function () { 4 var spy1 = this.spy(myFunc); 5 var spy2 = this.spy(myOtherFunc); 6 myFunc(1); 7 myFunc(2); 8 assert(spy1.calledWith(1)); 9 assert(spy1.calledWith(2)); 10 }) 11); //auto-cleanup
Sinon will take care of removing all the spies and stubs
from the wrapped functions for you. It does this by using
sinon.sandbox
internally.
Do notice that we use a function
and not a arrow function (ES2015)
when wrapping the test with sinon.test
as it needs
to be able to access the this
pointer used inside
of the function, which using an arrow function would prevent.
See the Usage section for more details.
via npm (node package manager)
$ npm install sinon-test
Once initialized, the package creates a context for your test based on a sinon sandbox.
You can use this
in a wrapped test function to create sinon spies, stubs, etc.
After your test completes, the sandbox restores anything modified to its original value.
If your test function takes any arguments, pass then to the test
wrapper
after the test function. If the last argument is a function, it is assumed to be a callback
for an asynchronous test. The test function may also return a promise.
See the sinon documentation for more documentation on sandboxes.
sinon-test
instances need to be configured with a sinon
instance (version 2+)
before they can be used.
1var sinon = require("sinon"); 2var sinonTest = require("sinon-test"); 3var test = sinonTest(sinon); 4var assert = require("assert"); 5 6describe("my function", function () { 7 var myFunc = require("./my-func"); 8 9 it( 10 "should do something", 11 test(function () { 12 var spy = this.spy(myFunc); 13 myFunc(1); 14 assert(spy.calledWith(1)); 15 }) 16 ); //auto-cleanup 17});
In place of the require
statements indicated above, in the
browser, you should simply reference the global sinonTest
after
including a script tag in your HTML:
1<script src="dist/sinon-test.js"></script>
Or if you are in an ES6 Modules environment (modern browsers only), you only need to add an import statement:
1<script type="module"> 2 import sinon from "./node_modules/sinon/pkg/sinon-esm.js"; 3 import sinonTest from "./node_modules/sinon-test/dist/sinon-test-es.js"; 4 const test = sinonTest(sinon); 5 6 it( 7 "should work", 8 test(function () { 9 pass(); 10 }) 11 ); 12</script>
1const test = require("sinon-test")(sinon);
In order to configure the sandbox that is created, a configuration hash can be passed as a 2nd argument to sinonTest
:
1const test = require("sinon-test")(sinon, { useFakeTimers: false });
The only difference to the standard configuration object for Sinon's sandbox is the addition of the injectIntoThis
property, which is used to inject the sandbox' props into the context object (this
).
Sinon 1.x used to ship with this functionality built-in, exposed as sinon.test()
. You can keep all your existing test code by configuring an instance of sinon-test
, as done above, and then assigning it to sinon
like this in your tests:
1sinon.test = test;
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
GitHub workflow tokens follow principle of least privilege
Details
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
Reason
Found 0/26 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
22 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More