Skip to content

Commit

Permalink
fixed test for discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
amandeepsingh333 committed Jan 10, 2025
1 parent 3ff670d commit 511e881
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
10 changes: 5 additions & 5 deletions packages/core/src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ export function chromium({

// default chromium revisions corresponds to v123.0.6312.58
chromium.revisions = {
linux: '1262506',
win64: '1262500',
win32: '1262500',
darwin: '1262506',
darwinArm: '1262509'
linux: '1368524',
win64: '1368503',
win32: '1368516',
darwin: '1368518',
darwinArm: '1368521'
};
// export the namespace by default
export * as default from './install.js';
21 changes: 19 additions & 2 deletions packages/core/src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class RequestLifeCycleHandler {
this.resolveResponseReceived = null;
this.requestWillBeSent = new Promise((resolve) => (this.resolveRequestWillBeSent = resolve));
this.responseReceived = new Promise((resolve) => (this.resolveResponseReceived = resolve));
this.isWorker = false;
}
}
// The Interceptor class creates common handlers for dealing with intercepting asset requests
Expand Down Expand Up @@ -216,10 +217,21 @@ export class Network {
// otherwise set it to be pending until it is paused.
_handleRequestWillBeSent = async event => {
let { requestId, request, type } = event;

// do not handle data urls
if (request.url.startsWith('data:')) return;

const isWorker = type === 'Worker' ||

Check failure on line 223 in packages/core/src/network.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 4 spaces but found 6

Check failure on line 223 in packages/core/src/network.js

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed
(request.url.endsWith('.js'));

// Mark in the lifecycle handler if this is a worker request

Check failure on line 226 in packages/core/src/network.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 4 spaces but found 6
this.#requestsLifeCycleHandler.get(requestId).isWorker = isWorker;

Check failure on line 227 in packages/core/src/network.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 4 spaces but found 6

// For worker requests, auto-resolve responseReceived since Chrome 131 won't fire it

Check failure on line 229 in packages/core/src/network.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 4 spaces but found 6
if (isWorker) {

Check failure on line 230 in packages/core/src/network.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 4 spaces but found 6
this.log.debug(`Auto-resolving responseReceived for worker request: ${request.url}`);

Check failure on line 231 in packages/core/src/network.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 6 spaces but found 8
this.#requestsLifeCycleHandler.get(requestId).resolveResponseReceived();

Check failure on line 232 in packages/core/src/network.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 6 spaces but found 8
}

Check failure on line 233 in packages/core/src/network.js

View workflow job for this annotation

GitHub Actions / Lint

Expected indentation of 4 spaces but found 6

// Browsers handle URL encoding leniently.
// This code checks for issues such as `%` and leading spaces and warns the user accordingly.
decodeAndEncodeURLWithLogging(request.url, this.log, {
Expand Down Expand Up @@ -302,7 +314,12 @@ export class Network {
_handleLoadingFinished = async (session, event) => {
let { requestId } = event;
// wait for upto 2 seconds or check if response has been sent
await this.#requestsLifeCycleHandler.get(requestId).responseReceived;
const handler = this.#requestsLifeCycleHandler.get(requestId);

// For worker requests, we don't need to wait for responseReceived
if (!handler.isWorker) {
await handler.responseReceived;
}
let request = this.#requests.get(requestId);
/* istanbul ignore if: race condition paranioa */
if (!request) return;
Expand Down
30 changes: 9 additions & 21 deletions packages/core/test/discovery.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sha256hash } from '@percy/client/utils';
import { sha256hash, waitForTimeout } from '@percy/client/utils';

Check failure on line 1 in packages/core/test/discovery.test.js

View workflow job for this annotation

GitHub Actions / Lint

'waitForTimeout' is defined but never used
import { logger, api, setupTest, createTestServer, dedent } from './helpers/index.js';
import Percy from '@percy/core';
import { RESOURCE_CACHE_KEY } from '../src/discovery.js';
Expand Down Expand Up @@ -42,6 +42,7 @@ describe('Discovery', () => {
delete process.env.PERCY_GZIP;

api.reply('/builds/123/snapshots', ({ body }) => {
console.log("dsdsdd")
// resource order is not important, stabilize it for testing
captured.push(body.data.relationships.resources.data.sort((a, b) => (
a.attributes['resource-url'].localeCompare(b.attributes['resource-url'])
Expand Down Expand Up @@ -791,7 +792,7 @@ describe('Discovery', () => {
});
});

it('captures requests from workers', async () => {
fit('captures requests from workers', async () => {
// Fetch and Network events are inherently racey because they come from different processes. The
// bug we are testing here happens specifically when the Network event comes after the Fetch
// event. Using a stub, we can cause Network events to happen a few milliseconds later than they
Expand All @@ -807,12 +808,8 @@ describe('Discovery', () => {

server.reply('/worker.js', () => [200, 'text/javascript', dedent`
self.addEventListener("message", async ({ data }) => {
try {
let response = await fetch(new Request(data));
self.postMessage("done");
} catch (error) {
self.postMessage("error");
}
let response = await fetch(new Request(data));
self.postMessage("done");
});
`]);

Expand All @@ -827,28 +824,23 @@ describe('Discovery', () => {
worker.addEventListener("message", ({ data }) => {
document.body.classList.add(data);
console.log('Worker message received:', data);
});
setTimeout(() => {
console.log('Sending message to worker');
worker.postMessage("http://localhost:8000/img.gif");
}, 100);
</script>
</body>
</html>
`]);

server.reply('/img.gif', () => [200, 'image/gif', 'GIF89a...']);

await percy.snapshot({
await percy.snapshot({
name: 'worker snapshot',
url: 'http://localhost:8000',
waitForSelector: '.done',
enableJavaScript: true,
waitForSelector: '.done',
waitForTimeout: 1000
});

await percy.idle();
let paths = server.requests.map(r => r[0]);
expect(paths).toContain('/img.gif');

Expand All @@ -861,10 +853,6 @@ describe('Discovery', () => {
})
])
);
if (!captured.length) {
console.log('No resources captured');
console.log('Server requests:', server.requests);
}
});

it('does not error on cancelled requests', async () => {
Expand Down Expand Up @@ -2208,14 +2196,15 @@ describe('Discovery', () => {
]));
});

it('should fail to launch if the devtools address is not logged', async () => {
fit('should fail to launch if the devtools address is not logged', async () => {
await expectAsync(
Percy.start({
token: 'PERCY_TOKEN',
snapshot: { widths: [1000] },
discovery: {
launchOptions: {
args: ['--remote-debugging-port=null'],
timeout: 5000
},
},
})
Expand All @@ -2229,7 +2218,6 @@ describe('Discovery', () => {
expect(lastLogMessage.includes('Failed to launch browser')).toEqual(true);
});



it('should fail to launch after the timeout', async () => {
await expectAsync(Percy.start({
Expand Down

0 comments on commit 511e881

Please sign in to comment.