Skip to content

Commit

Permalink
fixed cli-exec
Browse files Browse the repository at this point in the history
  • Loading branch information
amandeepsingh333 committed Jan 13, 2025
1 parent 5b8ba6b commit cc9be3e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 34 deletions.
4 changes: 2 additions & 2 deletions packages/cli-exec/test/start.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ describe('percy exec:start', () => {
});

it('can start on an alternate port', async () => {
start(['--quiet', '--port=4567']);
let response = await request('http://localhost:4567/percy/healthcheck');
start(['--quiet', '--port=4568']);
let response = await request('http://localhost:4568/percy/healthcheck');
expect(response).toHaveProperty('success', true);
});

Expand Down
81 changes: 49 additions & 32 deletions packages/core/test/snapshot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1373,46 +1373,63 @@ describe('Snapshot', () => {
});

it('can execute scripts at various states', async () => {
let domtest = (pre, fn) => `
let p = document.createElement('p');
p.innerHTML = ['${pre}', (${fn})()].join(' - ');
document.body.appendChild(p);
`;

await percy.snapshot({
name: 'foo snapshot',
url: 'http://localhost:8000',
execute: async () => {
// Simulate the initial state
const p1 = document.createElement('p');
p1.textContent = 'beforeResize - 400';
document.body.appendChild(p1);

// Simulate a resize event
await new Promise(resolve => setTimeout(resolve, 100)); // Allow time for changes
const p2 = document.createElement('p');
p2.textContent = 'afterResize - 800';
document.body.appendChild(p2);

// Simulate another resize event
await new Promise(resolve => setTimeout(resolve, 100)); // Allow time for changes
const p3 = document.createElement('p');
p3.textContent = 'beforeResize - 800';
document.body.appendChild(p3);

// Simulate a final resize event
await new Promise(resolve => setTimeout(resolve, 100)); // Allow time for changes
const p4 = document.createElement('p');
p4.textContent = 'afterResize - 1200';
document.body.appendChild(p4);
widths: [400, 800, 1200],
execute: {
afterNavigation: domtest('afterNavigation', () => window.location.href),
beforeSnapshot: domtest('beforeSnapshot', () => 'done!')
},
domTransformation: `
(documentElement) => {
let p = document.createElement('p');
p.innerText = 'added using domTransformation';
documentElement.querySelector('body').append(p);
}`
});

await percy.snapshot({
name: 'bar snapshot',
url: 'http://localhost:8000',
widths: [400, 800, 1200],
execute: {
beforeResize: domtest('beforeResize', () => window.innerWidth),
afterResize: domtest('afterResize', () => window.innerWidth)
}
});

await percy.idle();
const snapshotContent = Buffer.from((

expect(logger.stdout).toEqual(jasmine.arrayContaining([
'[percy] Snapshot taken: foo snapshot',
'[percy] Snapshot taken: bar snapshot'
]));

expect(Buffer.from((
api.requests['/builds/123/resources'][0]
.body.data.attributes['base64-content']
), 'base64').toString();
// Log the full snapshot content for inspection
console.log('Snapshot Content:', snapshotContent);
// More flexible matching
expect(snapshotContent).toMatch(/<p>beforeResize - 400<\/p>/);
expect(snapshotContent).toMatch(/<p>afterResize - 800<\/p>/);
expect(snapshotContent).toMatch(/<p>beforeResize - 800<\/p>/);
expect(snapshotContent).toMatch(/<p>afterResize - 1200<\/p>/);
), 'base64').toString()).toMatch([
'<p>afterNavigation - http://localhost:8000/</p>',
'<p>beforeSnapshot - done!</p>',
'<p>added using domTransformation</p>'
].join(''));

expect(Buffer.from((
api.requests['/builds/123/resources'][3]
.body.data.attributes['base64-content']
), 'base64').toString()).toMatch([
'<p>beforeResize - 400</p>',
'<p>afterResize - 800</p>',
'<p>beforeResize - 800</p>',
'<p>afterResize - 1200</p>'
].join(''));
});

it('can execute scripts with built-in helpers', async () => {
Expand Down

0 comments on commit cc9be3e

Please sign in to comment.