How to use sinon method in apimocker

Best JavaScript code snippet using apimocker

sinon.d.ts

Source:sinon.d.ts Github

copy

Full Screen

1// Type definitions for Sinon 1.16.02// Project: http://sinonjs.org/3// Definitions by: William Sears <https://github.com/mrbigdog2u>4// Definitions: https://github.com/borisyankov/DefinitelyTyped5declare module Sinon {6 interface SinonSpyCallApi {7 // Properties8 thisValue: any;9 args: any[];10 exception: any;11 returnValue: any;12 // Methods13 calledOn(obj: any): boolean;14 calledWith(...args: any[]): boolean;15 calledWithExactly(...args: any[]): boolean;16 calledWithMatch(...args: any[]): boolean;17 notCalledWith(...args: any[]): boolean;18 notCalledWithMatch(...args: any[]): boolean;19 returned(value: any): boolean;20 threw(): boolean;21 threw(type: string): boolean;22 threw(obj: any): boolean;23 callArg(pos: number): void;24 callArgOn(pos: number, obj: any, ...args: any[]): void;25 callArgWith(pos: number, ...args: any[]): void;26 callArgOnWith(pos: number, obj: any, ...args: any[]): void;27 yield(...args: any[]): void;28 yieldOn(obj: any, ...args: any[]): void;29 yieldTo(property: string, ...args: any[]): void;30 yieldToOn(property: string, obj: any, ...args: any[]): void;31 }32 interface SinonSpyCall extends SinonSpyCallApi {33 calledBefore(call: SinonSpyCall): boolean;34 calledAfter(call: SinonSpyCall): boolean;35 calledWithNew(call: SinonSpyCall): boolean;36 }37 interface SinonSpy extends SinonSpyCallApi {38 // Properties39 callCount: number;40 called: boolean;41 notCalled: boolean;42 calledOnce: boolean;43 calledTwice: boolean;44 calledThrice: boolean;45 firstCall: SinonSpyCall;46 secondCall: SinonSpyCall;47 thirdCall: SinonSpyCall;48 lastCall: SinonSpyCall;49 thisValues: any[];50 args: any[][];51 exceptions: any[];52 returnValues: any[];53 // Methods54 (...args: any[]): any;55 calledBefore(anotherSpy: SinonSpy): boolean;56 calledAfter(anotherSpy: SinonSpy): boolean;57 calledWithNew(spy: SinonSpy): boolean;58 withArgs(...args: any[]): SinonSpy;59 alwaysCalledOn(obj: any): boolean;60 alwaysCalledWith(...args: any[]): boolean;61 alwaysCalledWithExactly(...args: any[]): boolean;62 alwaysCalledWithMatch(...args: any[]): boolean;63 neverCalledWith(...args: any[]): boolean;64 neverCalledWithMatch(...args: any[]): boolean;65 alwaysThrew(): boolean;66 alwaysThrew(type: string): boolean;67 alwaysThrew(obj: any): boolean;68 alwaysReturned(): boolean;69 invokeCallback(...args: any[]): void;70 getCall(n: number): SinonSpyCall;71 reset(): void;72 printf(format: string, ...args: any[]): string;73 restore(): void;74 }75 interface SinonSpyStatic {76 (): SinonSpy;77 (func: any): SinonSpy;78 (obj: any, method: string): SinonSpy;79 }80 interface SinonStatic {81 spy: SinonSpyStatic;82 }83 interface SinonStub extends SinonSpy {84 resetBehavior(): void;85 returns(obj: any): SinonStub;86 returnsArg(index: number): SinonStub;87 returnsThis(): SinonStub;88 throws(type?: string): SinonStub;89 throws(obj: any): SinonStub;90 callsArg(index: number): SinonStub;91 callsArgOn(index: number, context: any): SinonStub;92 callsArgWith(index: number, ...args: any[]): SinonStub;93 callsArgOnWith(index: number, context: any, ...args: any[]): SinonStub;94 callsArgAsync(index: number): SinonStub;95 callsArgOnAsync(index: number, context: any): SinonStub;96 callsArgWithAsync(index: number, ...args: any[]): SinonStub;97 callsArgOnWithAsync(index: number, context: any, ...args: any[]): SinonStub;98 onCall(n: number): SinonStub;99 onFirstCall(): SinonStub;100 onSecondCall(): SinonStub;101 onThirdCall(): SinonStub;102 yields(...args: any[]): SinonStub;103 yieldsOn(context: any, ...args: any[]): SinonStub;104 yieldsTo(property: string, ...args: any[]): SinonStub;105 yieldsToOn(property: string, context: any, ...args: any[]): SinonStub;106 yieldsAsync(...args: any[]): SinonStub;107 yieldsOnAsync(context: any, ...args: any[]): SinonStub;108 yieldsToAsync(property: string, ...args: any[]): SinonStub;109 yieldsToOnAsync(property: string, context: any, ...args: any[]): SinonStub;110 withArgs(...args: any[]): SinonStub;111 }112 interface SinonStubStatic {113 (): SinonStub;114 (obj: any): SinonStub;115 (obj: any, method: string): SinonStub;116 (obj: any, method: string, func: any): SinonStub;117 }118 interface SinonStatic {119 stub: SinonStubStatic;120 }121 interface SinonExpectation extends SinonStub {122 atLeast(n: number): SinonExpectation;123 atMost(n: number): SinonExpectation;124 never(): SinonExpectation;125 once(): SinonExpectation;126 twice(): SinonExpectation;127 thrice(): SinonExpectation;128 exactly(n: number): SinonExpectation;129 withArgs(...args: any[]): SinonExpectation;130 withExactArgs(...args: any[]): SinonExpectation;131 on(obj: any): SinonExpectation;132 verify(): SinonExpectation;133 restore(): void;134 }135 interface SinonExpectationStatic {136 create(methodName?: string): SinonExpectation;137 }138 interface SinonMock {139 expects(method: string): SinonExpectation;140 restore(): void;141 verify(): void;142 }143 interface SinonMockStatic {144 (): SinonExpectation;145 (obj: any): SinonMock;146 }147 interface SinonStatic {148 expectation: SinonExpectationStatic;149 mock: SinonMockStatic;150 }151 interface SinonFakeTimers {152 now: number;153 create(now: number): SinonFakeTimers;154 setTimeout(callback: (...args: any[]) => void, timeout: number, ...args: any[]): number;155 clearTimeout(id: number): void;156 setInterval(callback: (...args: any[]) => void, timeout: number, ...args: any[]): number;157 clearInterval(id: number): void;158 tick(ms: number): number;159 reset(): void;160 Date(): Date;161 Date(year: number): Date;162 Date(year: number, month: number): Date;163 Date(year: number, month: number, day: number): Date;164 Date(year: number, month: number, day: number, hour: number): Date;165 Date(year: number, month: number, day: number, hour: number, minute: number): Date;166 Date(year: number, month: number, day: number, hour: number, minute: number, second: number): Date;167 Date(year: number, month: number, day: number, hour: number, minute: number, second: number, ms: number): Date;168 restore(): void;169 /**170 * Simulate the user changing the system clock while your program is running. It changes the 'now' timestamp171 * without affecting timers, intervals or immediates.172 * @param now The new 'now' in unix milliseconds173 */174 setSystemTime(now: number): void;175 /**176 * Simulate the user changing the system clock while your program is running. It changes the 'now' timestamp177 * without affecting timers, intervals or immediates.178 * @param now The new 'now' as a JavaScript Date179 */180 setSystemTime(date: Date): void;181 }182 interface SinonFakeTimersStatic {183 (): SinonFakeTimers;184 (...timers: string[]): SinonFakeTimers;185 (now: number, ...timers: string[]): SinonFakeTimers;186 }187 interface SinonStatic {188 useFakeTimers: SinonFakeTimersStatic;189 clock: SinonFakeTimers;190 }191 interface SinonFakeUploadProgress {192 eventListeners: {193 progress: any[];194 load: any[];195 abort: any[];196 error: any[];197 };198 addEventListener(event: string, listener: (e: Event) => any): void;199 removeEventListener(event: string, listener: (e: Event) => any): void;200 dispatchEvent(event: Event): void;201 }202 interface SinonFakeXMLHttpRequest {203 // Properties204 onCreate: (xhr: SinonFakeXMLHttpRequest) => void;205 url: string;206 method: string;207 requestHeaders: any;208 requestBody: string;209 status: number;210 statusText: string;211 async: boolean;212 username: string;213 password: string;214 withCredentials: boolean;215 upload: SinonFakeUploadProgress;216 responseXML: Document;217 getResponseHeader(header: string): string;218 getAllResponseHeaders(): any;219 // Methods220 restore(): void;221 useFilters: boolean;222 addFilter(filter: (method: string, url: string, async: boolean, username: string, password: string) => boolean): void;223 setResponseHeaders(headers: any): void;224 setResponseBody(body: string): void;225 respond(status: number, headers: any, body: string): void;226 autoRespond(ms: number): void;227 }228 interface SinonFakeXMLHttpRequestStatic {229 (): SinonFakeXMLHttpRequest;230 }231 interface SinonStatic {232 useFakeXMLHttpRequest: SinonFakeXMLHttpRequestStatic;233 FakeXMLHttpRequest: SinonFakeXMLHttpRequest;234 }235 interface SinonFakeServer {236 // Properties237 autoRespond: boolean;238 autoRespondAfter: number;239 fakeHTTPMethods: boolean;240 getHTTPMethod: (request: SinonFakeXMLHttpRequest) => string;241 requests: SinonFakeXMLHttpRequest[];242 respondImmediately: boolean;243 // Methods244 respondWith(body: string): void;245 respondWith(response: any[]): void;246 respondWith(fn: (xhr: SinonFakeXMLHttpRequest) => void): void;247 respondWith(url: string, body: string): void;248 respondWith(url: string, response: any[]): void;249 respondWith(url: string, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;250 respondWith(method: string, url: string, body: string): void;251 respondWith(method: string, url: string, response: any[]): void;252 respondWith(method: string, url: string, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;253 respondWith(url: RegExp, body: string): void;254 respondWith(url: RegExp, response: any[]): void;255 respondWith(url: RegExp, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;256 respondWith(method: string, url: RegExp, body: string): void;257 respondWith(method: string, url: RegExp, response: any[]): void;258 respondWith(method: string, url: RegExp, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;259 respond(): void;260 restore(): void;261 }262 interface SinonFakeServerStatic {263 create(): SinonFakeServer;264 }265 interface SinonStatic {266 fakeServer: SinonFakeServerStatic;267 fakeServerWithClock: SinonFakeServerStatic;268 }269 interface SinonExposeOptions {270 prefix?: string;271 includeFail?: boolean;272 }273 interface SinonAssert {274 // Properties275 failException: string;276 fail: (message?: string) => void; // Overridable277 pass: (assertion: any) => void; // Overridable278 // Methods279 notCalled(spy: SinonSpy): void;280 called(spy: SinonSpy): void;281 calledOnce(spy: SinonSpy): void;282 calledTwice(spy: SinonSpy): void;283 calledThrice(spy: SinonSpy): void;284 callCount(spy: SinonSpy, count: number): void;285 callOrder(...spies: SinonSpy[]): void;286 calledOn(spy: SinonSpy, obj: any): void;287 alwaysCalledOn(spy: SinonSpy, obj: any): void;288 calledWith(spy: SinonSpy, ...args: any[]): void;289 alwaysCalledWith(spy: SinonSpy, ...args: any[]): void;290 neverCalledWith(spy: SinonSpy, ...args: any[]): void;291 calledWithExactly(spy: SinonSpy, ...args: any[]): void;292 alwaysCalledWithExactly(spy: SinonSpy, ...args: any[]): void;293 calledWithMatch(spy: SinonSpy, ...args: any[]): void;294 alwaysCalledWithMatch(spy: SinonSpy, ...args: any[]): void;295 neverCalledWithMatch(spy: SinonSpy, ...args: any[]): void;296 threw(spy: SinonSpy): void;297 threw(spy: SinonSpy, exception: string): void;298 threw(spy: SinonSpy, exception: any): void;299 alwaysThrew(spy: SinonSpy): void;300 alwaysThrew(spy: SinonSpy, exception: string): void;301 alwaysThrew(spy: SinonSpy, exception: any): void;302 expose(obj: any, options?: SinonExposeOptions): void;303 }304 interface SinonStatic {305 assert: SinonAssert;306 }307 interface SinonMatcher {308 and(expr: SinonMatcher): SinonMatcher;309 or(expr: SinonMatcher): SinonMatcher;310 }311 interface SinonMatch {312 (value: number): SinonMatcher;313 (value: string): SinonMatcher;314 (expr: RegExp): SinonMatcher;315 (obj: any): SinonMatcher;316 (callback: (value: any) => boolean): SinonMatcher;317 any: SinonMatcher;318 defined: SinonMatcher;319 truthy: SinonMatcher;320 falsy: SinonMatcher;321 bool: SinonMatcher;322 number: SinonMatcher;323 string: SinonMatcher;324 object: SinonMatcher;325 func: SinonMatcher;326 array: SinonMatcher;327 regexp: SinonMatcher;328 date: SinonMatcher;329 same(obj: any): SinonMatcher;330 typeOf(type: string): SinonMatcher;331 instanceOf(type: any): SinonMatcher;332 has(property: string, expect?: any): SinonMatcher;333 hasOwn(property: string, expect?: any): SinonMatcher;334 }335 interface SinonStatic {336 match: SinonMatch;337 }338 interface SinonSandboxConfig {339 injectInto?: any;340 properties?: string[];341 useFakeTimers?: any;342 useFakeServer?: any;343 }344 interface SinonSandbox {345 clock: SinonFakeTimers;346 requests: SinonFakeXMLHttpRequest;347 server: SinonFakeServer;348 spy: SinonSpyStatic;349 stub: SinonStubStatic;350 mock: SinonMockStatic;351 useFakeTimers: SinonFakeTimersStatic;352 useFakeXMLHttpRequest: SinonFakeXMLHttpRequestStatic;353 useFakeServer(): SinonFakeServer;354 restore(): void;355 }356 interface SinonSandboxStatic {357 create(): SinonSandbox;358 create(config: SinonSandboxConfig): SinonSandbox;359 }360 interface SinonStatic {361 sandbox: SinonSandboxStatic;362 }363 interface SinonTestConfig {364 injectIntoThis?: boolean;365 injectInto?: any;366 properties?: string[];367 useFakeTimers?: boolean;368 useFakeServer?: boolean;369 }370 interface SinonTestWrapper extends SinonSandbox {371 (...args: any[]): any;372 }373 interface SinonStatic {374 config: SinonTestConfig;375 test(fn: (...args: any[]) => any): SinonTestWrapper;376 testCase(tests: any): any;377 }378 // Utility overridables379 interface SinonStatic {380 createStubInstance(constructor: any): SinonStub;381 format(obj: any): string;382 log(message: string): void;383 restore(object: any): void;384 }385}386declare var sinon: Sinon.SinonStatic;387declare module "sinon" {388 export = sinon;...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const request = require('supertest');2const app = require('./app');3describe('GET /api', () => {4 it('should return 200 OK', () => {5 return request(app)6 .get('/api')7 .expect(200);8 });9});10const express = require('express');11const app = express();12app.get('/api', (req, res) => {13 res.send({ message: 'Hello World!' });14});15module.exports = app;16"scripts": {17}

Full Screen

Using AI Code Generation

copy

Full Screen

1const sinon = require('sinon');2const axios = require('axios');3const expect = require('chai').expect;4describe('api tests', function () {5 it('should return success', function () {6 sinon.stub(axios, 'get').resolves({7 data: {8 },9 });10 expect(response.data.success).to.equal(true);11 });12 axios.get.restore();13 });14});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run apimocker automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful