Skip to content

chore: squelch test console output #15807

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 6 commits into from
Apr 19, 2025
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 @@ -36,8 +36,6 @@ export default test({
btn1.click();
});

console.warn(logs);

// the five components guarded by `count < 2` unmount and log
assert.deepEqual(logs, [1, true, 1, true, 1, true, 1, true, 1, true]);

Expand Down
10 changes: 5 additions & 5 deletions packages/svelte/tests/runtime-legacy/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ async function run_test_variant(
// @ts-expect-error
globalThis.__svelte.uid = 1;

if (manual_hydrate) {
if (manual_hydrate && variant === 'hydrate') {
hydrate_fn = () => {
instance = hydrate(mod.default, {
target,
Expand Down Expand Up @@ -469,10 +469,6 @@ async function run_test_variant(
throw err;
}
} finally {
console.log = console_log;
console.warn = console_warn;
console.error = console_error;

config.after_test?.();

// Free up the microtask queue
Expand All @@ -486,6 +482,10 @@ async function run_test_variant(
process.on('unhandledRejection', listener);
});
}

console.log = console_log;
console.warn = console_warn;
console.error = console_error;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,16 @@ export default test({
dev: true
},

test({ assert, target, warnings }) {
/** @type {any} */
let error;

test({ assert, target, warnings, errors }) {
const handler = (/** @type {any}} */ e) => {
error = e.error;
e.stopImmediatePropagation();
};

window.addEventListener('error', handler, true);

target.querySelector('button')?.click();

assert.throws(() => {
throw error;
}, /state_unsafe_mutation/);
assert.include(errors[0], 'state_unsafe_mutation');

window.removeEventListener('error', handler, true);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { assertType } from 'vitest';
import { test } from '../../test';

export default test({
Expand All @@ -8,12 +7,8 @@ export default test({
dev: true
},

test({ assert, target, warnings, logs }) {
/** @type {any} */
let error = null;

test({ assert, target, warnings, logs, errors }) {
const handler = (/** @type {any} */ e) => {
error = e.error;
e.stopImmediatePropagation();
};

Expand All @@ -23,25 +18,20 @@ export default test({

b1.click();
assert.deepEqual(logs, []);
assert.equal(error, null);

error = null;
logs.length = 0;
assert.deepEqual(errors, []);

b2.click();
assert.deepEqual(logs, ['clicked']);
assert.equal(error, null);
assert.deepEqual(errors, []);

error = null;
logs.length = 0;

b3.click();
assert.deepEqual(logs, []);
assert.deepEqual(warnings, [
'`click` handler at main.svelte:10:17 should be a function. Did you mean to add a leading `() =>`?'
]);
assert.isNotNull(error);
assert.match(error.message, /is not a function/);
assert.include(errors[0], 'is not a function');

window.removeEventListener('error', handler, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,16 @@ export default test({
dev: true
},

test({ assert, target, warnings }) {
/** @type {any} */
let error;

test({ assert, target, warnings, errors }) {
const handler = (/** @type {any} */ e) => {
error = e.error;
e.stopImmediatePropagation();
};

window.addEventListener('error', handler, true);

target.querySelector('button')?.click();

assert.throws(() => {
throw error;
}, /state_unsafe_mutation/);
assert.include(errors[0], 'state_unsafe_mutation');

window.removeEventListener('error', handler, true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ export default test({
dev: true
},

async test({ assert, target, logs }) {
async test({ assert, target, logs, errors }) {
const b1 = target.querySelector('button');
b1?.click();
flushSync();

assert.ok(errors.length > 0);
assert.deepEqual(logs, ['init', 'a', 'init', 'b']);
}
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { assert } from 'vitest';
import { test } from '../../test';

export default test({
compileOptions: {
dev: true
},
test() {}

test({ logs }) {
assert.ok(logs.length > 0);
}
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
const log = () => {
console.log(snip);
if (!snip) throw new Error('oops');
}
let x = $state(0);
</script>
Expand All @@ -9,4 +9,4 @@

{#snippet snip()}
snippet {x}
{/snippet}
{/snippet}
22 changes: 19 additions & 3 deletions packages/svelte/tests/signals/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { derived } from '../../src/internal/client/reactivity/deriveds';
import { snapshot } from '../../src/internal/shared/clone.js';
import { SvelteSet } from '../../src/reactivity/set';
import { DESTROYED } from '../../src/internal/client/constants';
import { noop } from 'svelte/internal/client';

/**
* @param runes runes mode
Expand Down Expand Up @@ -469,6 +470,9 @@ describe('signals', () => {
test('schedules rerun when writing to signal before reading it', (runes) => {
if (!runes) return () => {};

const error = console.error;
console.error = noop;

const value = state({ count: 0 });
user_effect(() => {
set(value, { count: 0 });
Expand All @@ -482,14 +486,19 @@ describe('signals', () => {
} catch (e: any) {
assert.include(e.message, 'effect_update_depth_exceeded');
errored = true;
} finally {
assert.equal(errored, true);
console.error = error;
}
assert.equal(errored, true);
};
});

test('schedules rerun when updating deeply nested value', (runes) => {
if (!runes) return () => {};

const error = console.error;
console.error = noop;

const value = proxy({ a: { b: { c: 0 } } });
user_effect(() => {
value.a.b.c += 1;
Expand All @@ -502,14 +511,19 @@ describe('signals', () => {
} catch (e: any) {
assert.include(e.message, 'effect_update_depth_exceeded');
errored = true;
} finally {
assert.equal(errored, true);
console.error = error;
}
assert.equal(errored, true);
};
});

test('schedules rerun when writing to signal before reading it', (runes) => {
if (!runes) return () => {};

const error = console.error;
console.error = noop;

const value = proxy({ arr: [] });
user_effect(() => {
value.arr = [];
Expand All @@ -523,8 +537,10 @@ describe('signals', () => {
} catch (e: any) {
assert.include(e.message, 'effect_update_depth_exceeded');
errored = true;
} finally {
assert.equal(errored, true);
console.error = error;
}
assert.equal(errored, true);
};
});

Expand Down