Best JavaScript code snippet using wpt
use_request.test.ts
Source:use_request.test.ts
...32 describe('path, method, body', () => {33 it('is used to send the request', async () => {34 const { setupSuccessRequest, completeRequest, hookResult, getSuccessResponse } = helpers;35 setupSuccessRequest();36 await completeRequest();37 expect(hookResult.data).toBe(getSuccessResponse().data);38 });39 });40 describe('pollIntervalMs', () => {41 it('sends another request after the specified time has elapsed', async () => {42 const { setupSuccessRequest, advanceTime, getSendRequestSpy } = helpers;43 setupSuccessRequest({ pollIntervalMs: REQUEST_TIME });44 await advanceTime(REQUEST_TIME);45 expect(getSendRequestSpy().callCount).toBe(1);46 // We need to advance (1) the pollIntervalMs and (2) the request time.47 await advanceTime(REQUEST_TIME * 2);48 expect(getSendRequestSpy().callCount).toBe(2);49 // We need to advance (1) the pollIntervalMs and (2) the request time.50 await advanceTime(REQUEST_TIME * 2);51 expect(getSendRequestSpy().callCount).toBe(3);52 });53 });54 describe('initialData', () => {55 it('sets the initial data value', async () => {56 const { setupSuccessRequest, completeRequest, hookResult, getSuccessResponse } = helpers;57 setupSuccessRequest({ initialData: 'initialData' });58 expect(hookResult.data).toBe('initialData');59 // The initial data value will be overwritten once the request resolves.60 await completeRequest();61 expect(hookResult.data).toBe(getSuccessResponse().data);62 });63 });64 describe('deserializer', () => {65 it('is called with the response once the request resolves', async () => {66 const { setupSuccessRequest, completeRequest, getSuccessResponse } = helpers;67 const deserializer = sinon.stub();68 setupSuccessRequest({ deserializer });69 sinon.assert.notCalled(deserializer);70 await completeRequest();71 sinon.assert.calledOnce(deserializer);72 sinon.assert.calledWith(deserializer, getSuccessResponse().data);73 });74 it('provides the data return value', async () => {75 const { setupSuccessRequest, completeRequest, hookResult } = helpers;76 setupSuccessRequest({ deserializer: () => 'intercepted' });77 await completeRequest();78 expect(hookResult.data).toBe('intercepted');79 });80 });81 });82 describe('state', () => {83 describe('isInitialRequest', () => {84 it('is true for the first request and false for subsequent requests', async () => {85 const { setupSuccessRequest, completeRequest, hookResult } = helpers;86 setupSuccessRequest();87 expect(hookResult.isInitialRequest).toBe(true);88 act(() => {89 hookResult.resendRequest();90 });91 await completeRequest();92 expect(hookResult.isInitialRequest).toBe(false);93 });94 });95 describe('isLoading', () => {96 it('represents in-flight request status', async () => {97 const { setupSuccessRequest, completeRequest, hookResult } = helpers;98 setupSuccessRequest();99 expect(hookResult.isLoading).toBe(true);100 await completeRequest();101 expect(hookResult.isLoading).toBe(false);102 });103 });104 describe('error', () => {105 it('surfaces errors from requests', async () => {106 const { setupErrorRequest, completeRequest, hookResult, getErrorResponse } = helpers;107 setupErrorRequest();108 await completeRequest();109 expect(hookResult.error).toBe(getErrorResponse().error);110 });111 it('surfaces body-shaped errors from requests', async () => {112 const {113 setupErrorWithBodyRequest,114 completeRequest,115 hookResult,116 getErrorWithBodyResponse,117 } = helpers;118 setupErrorWithBodyRequest();119 await completeRequest();120 expect(hookResult.error).toBe(getErrorWithBodyResponse().error);121 });122 it('persists while a request is in-flight', async () => {123 const { setupErrorRequest, completeRequest, hookResult, getErrorResponse } = helpers;124 setupErrorRequest();125 await completeRequest();126 expect(hookResult.isLoading).toBe(false);127 expect(hookResult.error).toBe(getErrorResponse().error);128 act(() => {129 hookResult.resendRequest();130 });131 expect(hookResult.isLoading).toBe(true);132 expect(hookResult.error).toBe(getErrorResponse().error);133 });134 it('is null when the request is successful', async () => {135 const { setupSuccessRequest, completeRequest, hookResult } = helpers;136 setupSuccessRequest();137 expect(hookResult.error).toBeNull();138 await completeRequest();139 expect(hookResult.isLoading).toBe(false);140 expect(hookResult.error).toBeNull();141 });142 });143 describe('data', () => {144 it('surfaces payloads from requests', async () => {145 const { setupSuccessRequest, completeRequest, hookResult, getSuccessResponse } = helpers;146 setupSuccessRequest();147 expect(hookResult.data).toBeUndefined();148 await completeRequest();149 expect(hookResult.data).toBe(getSuccessResponse().data);150 });151 it('persists while a request is in-flight', async () => {152 const { setupSuccessRequest, completeRequest, hookResult, getSuccessResponse } = helpers;153 setupSuccessRequest();154 await completeRequest();155 expect(hookResult.isLoading).toBe(false);156 expect(hookResult.data).toBe(getSuccessResponse().data);157 act(() => {158 hookResult.resendRequest();159 });160 expect(hookResult.isLoading).toBe(true);161 expect(hookResult.data).toBe(getSuccessResponse().data);162 });163 it('persists from last successful request when the next request fails', async () => {164 const {165 setupSuccessRequest,166 completeRequest,167 hookResult,168 getErrorResponse,169 setErrorResponse,170 getSuccessResponse,171 } = helpers;172 setupSuccessRequest();173 await completeRequest();174 expect(hookResult.isLoading).toBe(false);175 expect(hookResult.error).toBeNull();176 expect(hookResult.data).toBe(getSuccessResponse().data);177 setErrorResponse();178 await completeRequest();179 expect(hookResult.isLoading).toBe(false);180 expect(hookResult.error).toBe(getErrorResponse().error);181 expect(hookResult.data).toBe(getSuccessResponse().data);182 });183 });184 });185 describe('callbacks', () => {186 describe('resendRequest', () => {187 it('sends the request', async () => {188 const { setupSuccessRequest, completeRequest, hookResult, getSendRequestSpy } = helpers;189 setupSuccessRequest();190 await completeRequest();191 expect(getSendRequestSpy().callCount).toBe(1);192 await act(async () => {193 hookResult.resendRequest();194 await completeRequest();195 });196 expect(getSendRequestSpy().callCount).toBe(2);197 });198 it('resets the pollIntervalMs', async () => {199 const { setupSuccessRequest, advanceTime, hookResult, getSendRequestSpy } = helpers;200 const DOUBLE_REQUEST_TIME = REQUEST_TIME * 2;201 setupSuccessRequest({ pollIntervalMs: DOUBLE_REQUEST_TIME });202 // The initial request resolves, and then we'll immediately send a new one manually...203 await advanceTime(REQUEST_TIME);204 expect(getSendRequestSpy().callCount).toBe(1);205 act(() => {206 hookResult.resendRequest();207 });208 // The manual request resolves, and we'll send yet another one...209 await advanceTime(REQUEST_TIME);210 expect(getSendRequestSpy().callCount).toBe(2);211 act(() => {212 hookResult.resendRequest();213 });214 // At this point, we've moved forward 3s. The poll is set at 2s. If resendRequest didn't215 // reset the poll, the request call count would be 4, not 3.216 await advanceTime(REQUEST_TIME);217 expect(getSendRequestSpy().callCount).toBe(3);218 });219 });220 });221 describe('request behavior', () => {222 it('outdated responses are ignored by poll requests', async () => {223 const {224 setupSuccessRequest,225 setErrorResponse,226 completeRequest,227 hookResult,228 getErrorResponse,229 getSendRequestSpy,230 } = helpers;231 const DOUBLE_REQUEST_TIME = REQUEST_TIME * 2;232 // Send initial request, which will have a longer round-trip time.233 setupSuccessRequest({}, [DOUBLE_REQUEST_TIME]);234 // Send a new request, which will have a shorter round-trip time.235 setErrorResponse();236 // Complete both requests.237 await completeRequest();238 // Two requests were sent...239 expect(getSendRequestSpy().callCount).toBe(2);240 // ...but the error response is the one that takes precedence because it was *sent* more241 // recently, despite the success response *returning* more recently.242 expect(hookResult.error).toBe(getErrorResponse().error);243 expect(hookResult.data).toBeUndefined();244 });245 it(`outdated responses are ignored if there's a more recently-sent manual request`, async () => {246 const { setupSuccessRequest, advanceTime, hookResult, getSendRequestSpy } = helpers;247 const HALF_REQUEST_TIME = REQUEST_TIME * 0.5;248 setupSuccessRequest({ pollIntervalMs: REQUEST_TIME });249 // Before the original request resolves, we make a manual resendRequest call.250 await advanceTime(HALF_REQUEST_TIME);251 expect(getSendRequestSpy().callCount).toBe(0);252 act(() => {253 hookResult.resendRequest();254 });255 // The original quest resolves but it's been marked as outdated by the the manual resendRequest256 // call "interrupts", so data is left undefined.257 await advanceTime(HALF_REQUEST_TIME);258 expect(getSendRequestSpy().callCount).toBe(1);259 expect(hookResult.data).toBeUndefined();260 });261 it(`changing pollIntervalMs doesn't trigger a new request`, async () => {262 const { setupErrorRequest, setErrorResponse, completeRequest, getSendRequestSpy } = helpers;263 const DOUBLE_REQUEST_TIME = REQUEST_TIME * 2;264 // Send initial request.265 setupErrorRequest({ pollIntervalMs: REQUEST_TIME });266 // Setting a new poll will schedule a second request, but not send one immediately.267 setErrorResponse({ pollIntervalMs: DOUBLE_REQUEST_TIME });268 // Complete initial request.269 await completeRequest();270 // Complete scheduled poll request.271 await completeRequest();272 expect(getSendRequestSpy().callCount).toBe(2);273 });274 it('when the path changes after a request is scheduled, the scheduled request is sent with that path', async () => {275 const {276 setupSuccessRequest,277 completeRequest,278 hookResult,279 getErrorResponse,280 setErrorResponse,281 getSendRequestSpy,282 } = helpers;283 const DOUBLE_REQUEST_TIME = REQUEST_TIME * 2;284 // Sned first request and schedule a request, both with the success path.285 setupSuccessRequest({ pollIntervalMs: DOUBLE_REQUEST_TIME });286 // Change the path to the error path, sending a second request. pollIntervalMs is the same287 // so the originally scheduled poll remains cheduled.288 setErrorResponse({ pollIntervalMs: DOUBLE_REQUEST_TIME });289 // Complete the initial request, the requests by the path change, and the scheduled poll request.290 await completeRequest();291 await completeRequest();292 // If the scheduled poll request was sent to the success path, we wouldn't have an error result.293 // But we do, because it was sent to the error path.294 expect(getSendRequestSpy().callCount).toBe(3);295 expect(hookResult.error).toBe(getErrorResponse().error);296 });297 });...
Using AI Code Generation
1var wpt = require('webpagetest');2var api = new wpt('www.webpagetest.org');3 if (err) {4 console.log('Error: ' + err);5 } else {6 console.log('Test status: ' + data.statusText);7 api.completeRequest(data.data.testId, function(err, data) {8 if (err) {9 console.log('Error: ' + err);10 } else {11 console.log('Test status: ' + data.statusText);12 }13 });14 }15});
Using AI Code Generation
1var wpt = require('webpagetest');2var test = new wpt('www.webpagetest.org');3 if (err) {4 console.log(err);5 } else {6 test.completeRequest(data.data.testId, function(err, data) {7 if (err) {8 console.log(err);9 } else {10 console.log(data);11 }12 });13 }14});15var wpt = require('webpagetest');16var test = new wpt('www.webpagetest.org');17test.getLocations(function(err, data) {18 if (err) {19 console.log(err);20 } else {21 console.log(data);22 }23});24var wpt = require('webpagetest');25var test = new wpt('www.webpagetest.org');26test.getTesters(function(err, data) {27 if (err) {28 console.log(err);29 } else {30 console.log(data);31 }32});33var wpt = require('webpagetest');34var test = new wpt('www.webpagetest.org');35test.getTestStatus('140806_2R_2Q', function(err, data) {36 if (err) {37 console.log(err);38 } else {39 console.log(data);40 }41});42var wpt = require('webpagetest');43var test = new wpt('www.webpagetest.org');44test.getTestResults('140806_2R_2Q', function(err, data) {45 if (err) {46 console.log(err);47 } else {48 console.log(data);49 }50});
Using AI Code Generation
1var WebPageTest = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t1u2v3w4x5y6z7a8b9c0d1e2f3');3wpt.runTest(url, function(err, data) {4 if (err) return console.error(err);5 console.log('Test submitted to WebPageTest: %s', data.data.testId);6 wpt.getTestResults(data.data.testId, function(err, data) {7 if (err) return console.error(err);8 console.log('Test completed: %s', data.data.summary);9 });10});
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!