Skip to content

monkey-patch console.log and console.warn for all tests #11265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ export default test({

test(assert, target) {
assert.equal(target.querySelector('a')?.getAttribute('href'), '/bar');
}
},

errors: [
'Detected a href attribute value change during hydration. This will not be repaired during hydration, the href value that came from the server will be used. Related element:'
]
});
11 changes: 10 additions & 1 deletion packages/svelte/tests/hydration/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ interface HydrationTest extends BaseTest {
) => void | Promise<void>;
before_test?: () => void;
after_test?: () => void;
errors?: any[];
}

function read(path: string): string | void {
Expand Down Expand Up @@ -65,6 +66,7 @@ const { test, run } = suite<HydrationTest>(async (config, cwd) => {
const snapshot = config.snapshot ? config.snapshot(target) : {};

const error = console.error;
const errors: any[] = [];
let got_hydration_error = false;
console.error = (message: any) => {
if (typeof message === 'string' && message.startsWith('ERR_SVELTE_HYDRATION_MISMATCH')) {
Expand All @@ -73,7 +75,7 @@ const { test, run } = suite<HydrationTest>(async (config, cwd) => {
error(message);
}
} else {
error(message);
errors.push(message);
}
};

Expand All @@ -85,12 +87,19 @@ const { test, run } = suite<HydrationTest>(async (config, cwd) => {
});

console.error = error;

if (config.expect_hydration_error) {
assert.ok(got_hydration_error, 'Expected hydration error');
} else {
assert.ok(!got_hydration_error, 'Unexpected hydration error');
}

if (config.errors) {
assert.deepEqual(errors, config.errors);
} else if (errors.length) {
throw new Error(`Unexpected errors: ${errors.join('\n')}`);
}

const expected = read(`${cwd}/_expected.html`) ?? rendered.html;
assert_html_equal(target.innerHTML, expected);

Expand Down
1 change: 0 additions & 1 deletion packages/svelte/tests/runtime-browser/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ let browser: import('@playwright/test').Browser;

beforeAll(async () => {
browser = await chromium.launch();
console.log('[runtime-browser] Launched browser');
}, 20000);

afterAll(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
test({ assert, component, target }) {
test({ assert, target, logs }) {
const [b1, b2, b3] = target.querySelectorAll('button');
const first_h1 = target.querySelector('h1');

assert.deepEqual(component.log, [undefined, first_h1]);
assert.deepEqual(logs, [undefined, first_h1]);

flushSync(() => {
b3.click();
});

const third_h1 = target.querySelector('h1');

assert.deepEqual(component.log, [undefined, first_h1, third_h1]);
assert.deepEqual(logs, [undefined, first_h1, third_h1]);

flushSync(() => {
b1.click();
});

assert.deepEqual(component.log, [undefined, first_h1, third_h1, target.querySelector('h1')]);
assert.deepEqual(logs, [undefined, first_h1, third_h1, target.querySelector('h1')]);
}
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<script>
let activeTab = 0;
let activeHeading;
export let log = [];

$: log.push(activeHeading);
$: console.log(activeHeading);
</script>

<div class="tabs">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ import { unmount } from 'svelte';
export default test({
test({ component }) {
unmount(component.l1);
}
},

warnings: ['Tried to unmount a component that was not mounted.']
});
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<script>
let keys = ['a', 'b'];
let items = ['c', 'd'];
export let log;

function setKey(key, value, item) {
log.push(`setKey(${key}, ${value}, ${item})`);
}
console.log(`setKey(${key}, ${value}, ${item})`);
}
</script>

{#each items as item (item)}
{#each keys as key (key)}
<slot {key} {item} set={(value) => setKey(key, value, item)} />
{/each}
{/each}
{/each}
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@ export default test({
<button type="button">Set a-d</button>
<button type="button">Set b-d</button>
`,
async test({ assert, target, window, component }) {

async test({ assert, target, window, logs }) {
const [btn1, btn2, btn3, btn4] = target.querySelectorAll('button');
const click = new window.MouseEvent('click', { bubbles: true });

await btn1.dispatchEvent(click);
assert.deepEqual(component.log, ['setKey(a, value-a-c, c)']);
assert.deepEqual(logs, ['setKey(a, value-a-c, c)']);

await btn2.dispatchEvent(click);
assert.deepEqual(component.log, ['setKey(a, value-a-c, c)', 'setKey(b, value-b-c, c)']);
assert.deepEqual(logs, ['setKey(a, value-a-c, c)', 'setKey(b, value-b-c, c)']);

await btn3.dispatchEvent(click);
assert.deepEqual(component.log, [
assert.deepEqual(logs, [
'setKey(a, value-a-c, c)',
'setKey(b, value-b-c, c)',
'setKey(a, value-a-d, d)'
]);

await btn4.dispatchEvent(click);
assert.deepEqual(component.log, [
assert.deepEqual(logs, [
'setKey(a, value-a-c, c)',
'setKey(b, value-b-c, c)',
'setKey(a, value-a-d, d)',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<script>
import Nested from './Nested.svelte';
export let log = [];
</script>

<Nested {log} let:set let:key let:item>
<Nested let:set let:key let:item>
<button type="button" on:click={() => set(`value-${key}-${item}`)}>Set {key}-{item}</button>
</Nested>
</Nested>
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<script>
let keys = ['a', 'b'];
export let log;

function setKey(key, value) {
log.push(`setKey(${key}, ${value})`);
}
console.log(`setKey(${key}, ${value})`);
}
</script>

{#each keys as key (key)}
<slot {key} set={(value) => setKey(key, value)} />
{/each}
{/each}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ export default test({
<button type="button">Set a</button>
<button type="button">Set b</button>
`,
async test({ assert, target, window, component }) {

async test({ assert, target, window, logs }) {
const [btn1, btn2] = target.querySelectorAll('button');
const click = new window.MouseEvent('click', { bubbles: true });

await btn1.dispatchEvent(click);
assert.deepEqual(component.log, ['setKey(a, value-a)']);
assert.deepEqual(logs, ['setKey(a, value-a)']);

await btn2.dispatchEvent(click);
assert.deepEqual(component.log, ['setKey(a, value-a)', 'setKey(b, value-b)']);
assert.deepEqual(logs, ['setKey(a, value-a)', 'setKey(b, value-b)']);
}
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<script>
import Nested from './Nested.svelte';
export let log = [];
</script>

<Nested {log} let:set let:key>
<Nested let:set let:key>
<button type="button" on:click={() => set(`value-${key}`)}>Set {key}</button>
</Nested>
</Nested>
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<script>
export let log;
function setKey(key, value) {
log.push(`setKey(${key}, ${value})`);
}
console.log(`setKey(${key}, ${value})`);
}
</script>

<slot key="a" set={setKey} />
<slot key="b" set={setKey} />
<slot key="b" set={setKey} />
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<script>
import Inner from './Inner.svelte';
export let log;
</script>

<Inner {log} let:key let:set>
<Inner let:key let:set>
<slot {key} set={(value) => set(key, value)} />
</Inner>
</Inner>
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ export default test({
<button type="button">Set a</button>
<button type="button">Set b</button>
`,
async test({ assert, target, window, component }) {

async test({ assert, target, window, logs }) {
const [btn1, btn2] = target.querySelectorAll('button');
const click = new window.MouseEvent('click', { bubbles: true });

await btn1.dispatchEvent(click);
assert.deepEqual(component.log, ['setKey(a, value-a)']);
assert.deepEqual(logs, ['setKey(a, value-a)']);

await btn2.dispatchEvent(click);
assert.deepEqual(component.log, ['setKey(a, value-a)', 'setKey(b, value-b)']);
assert.deepEqual(logs, ['setKey(a, value-a)', 'setKey(b, value-b)']);
}
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<script>
import Nested from './Nested.svelte';
export let log = [];
</script>

<Nested {log} let:set let:key>
<Nested let:set let:key>
<button type="button" on:click={() => set(`value-${key}`)}>Set {key}</button>
</Nested>
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@ export default test({
test({ component }) {
unmount(component);
unmount(component);
}
},

warnings: [
'Tried to unmount a component that was not mounted.',
'Tried to unmount a component that was not mounted.'
]
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

<script>
import { onDestroy } from 'svelte';
import { onDestroy } from 'svelte';

onDestroy(() => {
console.log('destroy');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
import { test } from '../../test';

/**
* @type {{ (...data: any[]): void; (message?: any, ...optionalParams: any[]): void; (...data: any[]): void; (message?: any, ...optionalParams: any[]): void; }}
*/
let log;

export default test({
html: `
<button>destroy component</button>
`,

before_test() {
log = console.log;
},
after_test() {
console.log = log;
},

async test({ assert, target, window }) {
async test({ assert, target, window, logs }) {
const button = target.querySelector('button');
const event = new window.MouseEvent('click');
/**
* @type {any[]}
*/
const messages = [];
console.log = (msg) => messages.push(msg);

// @ts-ignore
await button.dispatchEvent(event);
assert.htmlEqual(
Expand All @@ -33,6 +17,6 @@ export default test({
<button>destroy component</button>
`
);
assert.deepEqual(messages, ['destroy']);
assert.deepEqual(logs, ['destroy']);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
let active = true;
</script>

<button on:click='{() => active = false }'>destroy component</button>
<button on:click={() => active = false }>destroy component</button>

<svelte:component this={active ? Empty : null} />
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import { flushSync } from 'svelte';
import { ok, test } from '../../test';

export default test({
test({ assert, component, target, window }) {
test({ assert, logs, target }) {
const button = target.querySelector('button');
ok(button);

flushSync(() => {
button.click();
});

assert.deepEqual(component.log, ['1 - 1']);
assert.deepEqual(logs, ['1 - 1']);

flushSync(() => {
button.click();
});

assert.deepEqual(component.log, ['1 - 1', '2 - 2']);
assert.deepEqual(logs, ['1 - 1', '2 - 2']);
}
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script>
export let log = [];
let referenced_directly = 0;
let not_referenced_directly = 0;
let css_based_on_not_referenced = '';
Expand All @@ -8,7 +7,7 @@
referenced_directly += 1;
not_referenced_directly += 1;
css_based_on_not_referenced = not_referenced_directly % 2 == 1 ? 'background-color: red' : '';
log.push(referenced_directly + ' - ' + not_referenced_directly); //only referenced_directly is increasing
console.log(referenced_directly + ' - ' + not_referenced_directly); //only referenced_directly is increasing
}
</script>

Expand Down
Loading
Loading