Skip to content

Commit

Permalink
fix some flaky tests (#8361)
Browse files Browse the repository at this point in the history
* fix some flaky tests

* undo deletion
  • Loading branch information
Rich-Harris authored Jan 6, 2023
1 parent 4ff91b0 commit 8633ebd
Showing 1 changed file with 37 additions and 45 deletions.
82 changes: 37 additions & 45 deletions packages/kit/test/apps/basics/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ test.describe('Scrolling', () => {

test('scroll is restored after hitting the back button', async ({ baseURL, clicknav, page }) => {
await page.goto('/anchor');
await page.click('#scroll-anchor');
await page.locator('#scroll-anchor').click();
const originalScrollY = /** @type {number} */ (await page.evaluate(() => scrollY));
await clicknav('#routing-page');
await page.goBack();
Expand Down Expand Up @@ -266,7 +266,7 @@ test.describe('Scrolling', () => {
const target_scroll_y = rect.y + rect.height - height;
await page.evaluate((y) => scrollTo(0, y), target_scroll_y);

await page.click('[href="/scroll/cross-document/b"]');
await page.locator('[href="/scroll/cross-document/b"]').click();
expect(await page.textContent('h1')).toBe('b');
await page.waitForSelector('body.started');

Expand Down Expand Up @@ -394,7 +394,7 @@ test.describe('CSS', () => {
test.describe('Endpoints', () => {
test('calls a delete handler', async ({ page }) => {
await page.goto('/delete-route');
await page.click('.del');
await page.locator('.del').click();
expect(await page.innerHTML('h1')).toBe('deleted 42');
});
});
Expand Down Expand Up @@ -556,43 +556,35 @@ test.describe('Load', () => {
test('load does not call fetch if max-age allows it', async ({ page, request }) => {
await request.get('/load/cache-control/reset');

page.addInitScript(`
window.now = 0;
window.performance.now = () => now;
`);

await page.goto('/load/cache-control');
expect(await page.textContent('p')).toBe('Count is 0');
await page.waitForTimeout(500);
await page.click('button.default');
await page.waitForTimeout(500);
expect(await page.textContent('p')).toBe('Count is 0');

await page.evaluate(() => (window.now = 2500));
await page.click('button.default');
await expect(page.locator('p')).toHaveText('Count is 2');
await expect(page.getByText('Count is 0')).toBeVisible();
await page.locator('button.default').click();
await expect(page.getByText('Count is 0')).toBeVisible();

// use a cache expiration value high enough that we're confident the earlier
// portions of the test will complete before the cache expires
await page.waitForTimeout(4000);

await page.locator('button.default').click();
await expect(page.getByText('Count is 2')).toBeVisible();
});

test('load does ignore ttl if fetch cache options says so', async ({ page, request }) => {
await request.get('/load/cache-control/reset');

await page.goto('/load/cache-control');
expect(await page.textContent('p')).toBe('Count is 0');
await page.waitForTimeout(500);
await page.click('button.force');
await page.waitForTimeout(500);
expect(await page.textContent('p')).toBe('Count is 1');
await expect(page.getByText('Count is 0')).toBeVisible();
await page.locator('button.force').click();
await expect(page.getByText('Count is 1')).toBeVisible();
});

test('load busts cache if non-GET request to resource is made', async ({ page, request }) => {
await request.get('/load/cache-control/reset');

await page.goto('/load/cache-control');
expect(await page.textContent('p')).toBe('Count is 0');
await page.waitForTimeout(500);
await page.click('button.bust');
await page.waitForTimeout(500);
expect(await page.textContent('p')).toBe('Count is 1');
await expect(page.getByText('Count is 0')).toBeVisible();
await page.locator('button.bust').click();
await expect(page.getByText('Count is 1')).toBeVisible();
});
});

Expand Down Expand Up @@ -792,10 +784,10 @@ test.describe('Routing', () => {
await page.goto('/routing/hashes/pagestore');
expect(await page.textContent('#window-hash')).toBe('');
expect(await page.textContent('#page-url-hash')).toBe('');
await page.click('[href="#target"]');
await page.locator('[href="#target"]').click();
expect(await page.textContent('#window-hash')).toBe('#target');
expect(await page.textContent('#page-url-hash')).toBe('#target');
await page.click('[href="/routing/hashes/pagestore"]');
await page.locator('[href="/routing/hashes/pagestore"]').click();
await expect(page.locator('#window-hash')).toHaveText('#target'); // hashchange doesn't fire for these
await expect(page.locator('#page-url-hash')).toHaveText('');
});
Expand All @@ -811,7 +803,7 @@ test.describe('Routing', () => {

try {
await page.goto(`/routing/slashes?port=${port}`);
await page.click(`a[href="http://localhost:${port}/with-slash/"]`);
await page.locator(`a[href="http://localhost:${port}/with-slash/"]`).click();

expect(urls).toEqual(['/with-slash/']);
} finally {
Expand All @@ -823,7 +815,7 @@ test.describe('Routing', () => {
await page.goto('/routing/external-popstate');
expect(await page.textContent('h1')).toBe('hello');

await page.click('button');
await page.locator('button').click();
expect(await page.textContent('h1')).toBe('hello');

await page.goBack();
Expand All @@ -836,7 +828,7 @@ test.describe('Routing', () => {
test('recognizes clicks outside the app target', async ({ page }) => {
await page.goto('/routing/link-outside-app-target/source');

await page.click('[href="/routing/link-outside-app-target/target"]');
await page.locator('[href="/routing/link-outside-app-target/target"]').click();
await expect(page.locator('h1')).toHaveText('target: 1');
});

Expand All @@ -850,7 +842,7 @@ test.describe('Routing', () => {
page.on('request', (request) => requests.push(request.url()));

await page.locator('input').fill('updated');
await page.click('button');
await page.locator('button').click();

expect(requests).toEqual([]);
expect(await page.textContent('h1')).toBe('updated');
Expand Down Expand Up @@ -921,7 +913,7 @@ test.describe('$app/stores', () => {
test('can use $app/stores from anywhere on client', async ({ page }) => {
await page.goto('/store/client-access');
await expect(page.locator('h1')).toHaveText('undefined');
await page.click('button');
await page.locator('button').click();
await expect(page.locator('h1')).toHaveText('/store/client-access');
});

Expand Down Expand Up @@ -1014,7 +1006,7 @@ test.describe.serial('Invalidation', () => {

test('multiple invalidations run concurrently', async ({ page, request }) => {
await page.goto('/load/invalidation/multiple');
expect(await page.textContent('p')).toBe('layout: 0, page: 0');
await expect(page.getByText('layout: 0, page: 0')).toBeVisible();

await page.click('button.layout');
await page.click('button.layout');
Expand All @@ -1023,12 +1015,12 @@ test.describe.serial('Invalidation', () => {
await page.click('button.layout');
await page.click('button.page');
await page.click('button.all');
await expect(page.locator('p')).toHaveText('layout: 4, page: 4');
await expect(page.getByText('layout: 4, page: 4')).toBeVisible();
});

test('invalidateAll persists through redirects', async ({ page }) => {
await page.goto('/load/invalidation/multiple/redirect');
await page.click('button.redirect');
await page.locator('button.redirect').click();
await expect(page.locator('p.redirect-state')).toHaveText('Redirect state: done');
});

Expand Down Expand Up @@ -1096,10 +1088,10 @@ test.describe.serial('Invalidation', () => {

test('$page.url can safely be mutated', async ({ page }) => {
await page.goto('/load/mutated-url?q=initial');
expect(await page.textContent('h1')).toBe('initial');
await expect(page.getByText('initial')).toBeVisible();

await page.click('button');
expect(await page.textContent('h1')).toBe('updated');
await page.locator('button').click();
await expect(page.getByText('updated')).toBeVisible();
});
});

Expand Down Expand Up @@ -1146,12 +1138,12 @@ test.describe('data-sveltekit attributes', () => {
page.on('request', (r) => requests.push(r.url()));

await page.goto('/data-sveltekit/reload');
await page.click('#one');
await page.locator('#one').click();
expect(requests).toContain(`${baseURL}/data-sveltekit/reload/target`);

requests.length = 0;
await page.goto('/data-sveltekit/reload');
await page.click('#two');
await page.locator('#two').click();
expect(requests).toContain(`${baseURL}/data-sveltekit/reload/target`);

requests.length = 0;
Expand Down Expand Up @@ -1207,7 +1199,7 @@ test.describe('cookies', () => {
test('etag forwards cookies', async ({ page }) => {
await page.goto('/cookies/forwarded-in-etag');
await expect(page.locator('p')).toHaveText('foo=bar');
await Promise.all([page.waitForNavigation(), page.click('button')]);
await page.locator('button').click();
await expect(page.locator('p')).toHaveText('foo=bar');
});
});
Expand All @@ -1224,11 +1216,11 @@ test.describe('Interactivity', () => {
await page.goto('/interactivity/toggle-element');
expect(await page.textContent('button')).toBe('remove');

await page.click('button');
await page.locator('button').click();
expect(await page.textContent('button')).toBe('add');
expect(await page.textContent('a')).toBe('add');

await page.click('a');
await page.locator('a').filter({ hasText: 'add' }).click();
expect(await page.textContent('a')).toBe('remove');

expect(errored).toBe(false);
Expand Down

0 comments on commit 8633ebd

Please sign in to comment.