uvu#test JavaScript Examples

The following examples show how to use uvu#test. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.spec.js    From kit with MIT License 7 votes vote down vote up
test('serialize_error', () => {
	class FancyError extends Error {
		name = 'FancyError';
		fancy = true;

		/**
		 * @param {string} message
		 * @param {{
		 *   cause?: Error
		 * }} [options]
		 */
		constructor(message, options) {
			// @ts-expect-error go home typescript ur drunk
			super(message, options);
		}
	}

	const error = new FancyError('something went wrong', {
		cause: new Error('sorry')
	});

	const serialized = serialize_error(error, (error) => error.stack);

	assert.equal(
		serialized,
		JSON.stringify({
			name: 'FancyError',
			message: 'something went wrong',
			stack: error.stack,
			cause: {
				name: 'Error',
				message: 'sorry',
				// @ts-expect-error
				stack: error.cause.stack
			},
			fancy: true
		})
	);
});
Example #2
Source File: index.js    From nestie with MIT License 6 votes vote down vote up
test('wrong inputs', () => {
	run(1, undefined);
	run('', undefined);
	run(0, undefined);

	run(null, undefined);
	run(undefined, undefined);
	run(NaN, undefined);
});
Example #3
Source File: index.spec.js    From kit with MIT License 6 votes vote down vote up
test('creates routes with layout', () => {
	const { components, routes } = create('samples/basic-layout');

	const layout = 'samples/basic-layout/__layout.svelte';
	const index = 'samples/basic-layout/index.svelte';
	const foo___layout = 'samples/basic-layout/foo/__layout.svelte';
	const foo = 'samples/basic-layout/foo/index.svelte';

	assert.equal(components, [layout, default_error, foo___layout, foo, index]);

	assert.equal(routes, [
		{
			type: 'page',
			id: '',
			pattern: /^\/$/,
			path: '/',
			shadow: null,
			a: [layout, index],
			b: [default_error]
		},

		{
			type: 'page',
			id: 'foo',
			pattern: /^\/foo\/?$/,
			path: '/foo',
			shadow: null,
			a: [layout, foo___layout, foo],
			b: [default_error]
		}
	]);
});
Example #4
Source File: mount.test.js    From mount-vue-component with MIT License 6 votes vote down vote up
test('it can create create a component instance with unmount hooks', () => {
  let called = false
  const unmounted = () => called = true
  const comp = createComponent({ unmounted })
  const { destroy } = createComponentInstance(comp)
  assert.not(called)
  destroy()
  assert.ok(called)
})
Example #5
Source File: index.spec.js    From kit with MIT License 6 votes vote down vote up
test('errors on recursive name layout', () => {
	assert.throws(
		() => create('samples/named-layout-recursive-1'),
		/Recursive layout detected: samples\/named-layout-recursive-1\/__layout-a@b\.svelte -> samples\/named-layout-recursive-1\/__layout-b@a\.svelte -> samples\/named-layout-recursive-1\/__layout-a@b\.svelte/
	);
	assert.throws(
		() => create('samples/named-layout-recursive-2'),
		/Recursive layout detected: samples\/named-layout-recursive-2\/__layout-a@a\.svelte -> samples\/named-layout-recursive-2\/__layout-a@a\.svelte/
	);

	assert.throws(
		() => create('samples/named-layout-recursive-3'),
		/Recursive layout detected: samples\/named-layout-recursive-3\/__layout@a\.svelte -> samples\/named-layout-recursive-3\/__layout-a@default\.svelte -> samples\/named-layout-recursive-3\/__layout@a\.svelte/
	);
});
Example #6
Source File: Count.js    From uvu with MIT License 6 votes vote down vote up
test('should decrement count after `button#decr` click', async () => {
	const { container } = ENV.render(Count);

	assert.snapshot(
		container.innerHTML,
		`<button id="decr">--</button> <span>5</span> <button id="incr">++</button>`
	);

	await ENV.fire(
		container.querySelector('#decr'),
		'click'
	);

	assert.snapshot(
		container.innerHTML,
		`<button id="decr">--</button> <span>4</span> <button id="incr">++</button>`
	);
});
Example #7
Source File: endpoint.spec.js    From kit with MIT License 6 votes vote down vote up
test('is_text', () => {
	assert.equal(is_text(undefined), true);
	assert.equal(is_text(null), true);
	assert.equal(is_text(''), true);
	assert.equal(is_text('TEXT/PLAIN'), true);
	assert.equal(is_text('text/html'), true);
	assert.equal(is_text('text/javascript'), true);
	assert.equal(is_text('application/xml'), true);
	assert.equal(is_text('image/svg+xml'), true);
	assert.equal(is_text('application/json'), true);
	assert.equal(is_text('text/plain; charset="us-ascii"'), true);
	assert.equal(is_text('multipart/form-data; boundary=aBoundaryString'), true);

	assert.equal(is_text('multipart/byteranges; boundary=3d6b6a416f9b5'), false);
	assert.equal(is_text('image/apng'), false);
	assert.equal(is_text('IMAGE/webp'), false);
	assert.equal(is_text('application/octet-stream'), false);
});
Example #8
Source File: Count.js    From uvu with MIT License 6 votes vote down vote up
test('should decrement count after `button#decr` click', async () => {
	const { container } = ENV.render(Count);

	assert.snapshot(
		container.innerHTML,
		`<button id="decr">--</button><span>5</span><button id="incr">++</button>`
	);

	await ENV.fire(
		container.querySelector('#decr'),
		'click'
	);

	assert.snapshot(
		container.innerHTML,
		`<button id="decr">--</button><span>4</span><button id="incr">++</button>`
	);
});
Example #9
Source File: crypto.spec.js    From kit with MIT License 6 votes vote down vote up
inputs.forEach((input) => {
	test(input, async () => {
		// @ts-expect-error typescript what are you doing you lunatic
		const expected_bytes = await webcrypto.subtle.digest(
			'SHA-256',
			new TextEncoder().encode(input)
		);
		const expected = Buffer.from(expected_bytes).toString('base64');

		const actual = sha256(input);
		assert.equal(actual, expected);
	});
});
Example #10
Source File: index.js    From nestie with MIT License 6 votes vote down vote up
test('proto pollution :: __proto__ :: midlevel', () => {
	let output = nestie({
		'aaa.__proto__.foobar': 123
	});

	let tmp = {};
	assert.equal(output, { aaa: {} });
	assert.is(tmp.foobar, undefined);
});
Example #11
Source File: index.js    From rosetta with MIT License 6 votes vote down vote up
test('nested', () => {
	let ctx = rosetta({
		en: {
			fruits: {
				apple: 'apple',
				orange: 'orange',
				grape: 'grape',
			}
		},
		es: {
			fruits: {
				apple: 'manzana',
				orange: 'naranja',
				grape: 'uva',
			}
		}
	});

	ctx.locale('en');
	assert.is(ctx.t('fruits.apple'), 'apple', '(en) fruits.apple');
	assert.is(ctx.t('fruits.orange'), 'orange', '(en) fruits.orange');
	assert.is(ctx.t(['fruits', 'grape']), 'grape', '(en) ["fruits","grape"]');
	assert.is(ctx.t('fruits.404'), "", '(en) fruits.404 ~> ""');
	assert.is(ctx.t('error.404'), "", '(en) error.404 ~> ""');

	ctx.locale('es');
	assert.is(ctx.t('fruits.apple'), 'manzana', '(es) fruits.apple');
	assert.is(ctx.t('fruits.orange'), 'naranja', '(es) fruits.orange');
	assert.is(ctx.t(['fruits', 'grape']), 'uva', '(es) ["fruits","grape"]');
	assert.is(ctx.t('fruits.404'), "", '(es) fruits.404 ~> ""');
	assert.is(ctx.t('error.404'), "", '(es) error.404 ~> ""');
});
Example #12
Source File: queue.spec.js    From kit with MIT License 6 votes vote down vote up
test('q.add rejects if task rejects', async () => {
	const q = queue(1);

	try {
		await q.add(async () => {
			await sleep(1);
			throw new Error('nope');
		});

		assert.ok(false);
	} catch (e) {
		assert.equal(/** @type {Error} */ (e).message, 'nope');
	}
});
Example #13
Source File: debug.js    From rosetta with MIT License 6 votes vote down vote up
test('invalid value', () => {
	let ctx = rosetta({
		en: {
			foo: ['bar'],
		}
	});

	assert.equal(
		ctx.t('foo', null, 'en'),
		['bar']
	);
});
Example #14
Source File: queue.spec.js    From kit with MIT License 6 votes vote down vote up
test('q.done() resolves if nothing was added to the queue', async () => {
	const q = queue(100);
	await Promise.race([
		q.done(),
		sleep(1).then(() => {
			throw new Error('Timed out');
		})
	]);
});
Example #15
Source File: index.js    From nestie with MIT License 6 votes vote down vote up
test('proto pollution :: constructor :: nested', () => {
	let output = nestie({
		'constructor.prototype.hello': 'world',
		'foo': 123,
	});

	assert.equal(output, {
		foo: 123
	});

	assert.is(output.hasOwnProperty('constructor'), false);
	assert.is(output.hasOwnProperty('hello'), false);

	let tmp = {};
	assert.is(tmp.hasOwnProperty('constructor'), false);
	assert.is(tmp.hasOwnProperty('hello'), false);
});
Example #16
Source File: index.js    From uid with MIT License 6 votes vote down vote up
test('length :: 4', () => {
	let i=0, tmp;
	for (; i < 1e3; i++) {
		tmp = uid(4);
		assert.is(tmp.length, 4, `"${tmp}" is not 4 characters!`);
	}

	assert.ok('~> produced 1000 IDs w/ 4 chars each');
});
Example #17
Source File: index.js    From nestie with MIT License 6 votes vote down vote up
test('proto pollution :: constructor :: direct', () => {
	function Custom() {
		//
	}

	let output = nestie({
		'a.constructor': Custom,
		'foo.bar': 123,
	});

	assert.equal(output, {
		a: {
			// stopped
		},
		foo: {
			bar: 123,
		}
	});

	// Check existing object
	assert.is.not(output.a.constructor, Custom);
	assert.not.instance(output.a, Custom);
	assert.instance(output.a.constructor, Object, '~> 123 -> {}');
	assert.is(output.a.hasOwnProperty('constructor'), false);

	let tmp = {}; // Check new object
	assert.is.not(tmp.constructor, Custom);
	assert.not.instance(tmp, Custom);

	assert.instance(tmp.constructor, Object, '~> 123 -> {}');
	assert.is(tmp.hasOwnProperty('constructor'), false);
});
Example #18
Source File: index.js    From uid with MIT License 6 votes vote down vote up
test('length :: 6', () => {
	let i=0, tmp;
	for (; i < 1e3; i++) {
		tmp = uid(6);
		assert.is(tmp.length, 6, `"${tmp}" is not 6 characters!`);
	}

	assert.ok('~> produced 1000 IDs w/ 6 chars each');
});
Example #19
Source File: utils.spec.js    From kit with MIT License 6 votes vote down vote up
test('lowercase_keys', () => {
	assert.equal(lowercase_keys({ KEY: 'value' }), { key: 'value' });
	assert.equal(lowercase_keys({ Key: 'value' }), { key: 'value' });
	assert.equal(lowercase_keys({ UNDERSCORE_KEY: 'value' }), { underscore_key: 'value' });
	assert.equal(lowercase_keys({ 1: 'Hello World' }), { 1: 'Hello World' });
});
Example #20
Source File: single.js    From uid with MIT License 6 votes vote down vote up
test('length :: 6', () => {
	let i=0, tmp;
	for (; i < 1e3; i++) {
		tmp = uid(6);
		assert.is(tmp.length, 6, `"${tmp}" is not 6 characters!`);
	}

	assert.ok('~> produced 1000 IDs w/ 6 chars each');
});
Example #21
Source File: utils.spec.js    From kit with MIT License 6 votes vote down vote up
test.run();
Example #22
Source File: secure.js    From uid with MIT License 6 votes vote down vote up
test('length :: 6', () => {
	let i=0, tmp;
	for (; i < 1e3; i++) {
		tmp = uid(6);
		assert.is(tmp.length, 6, `"${tmp}" is not 6 characters!`);
	}

	assert.ok('~> produced 1000 IDs w/ 6 chars each');
});
Example #23
Source File: queue.spec.js    From kit with MIT License 6 votes vote down vote up
test('q.done() rejects if task rejects', async () => {
	const q = queue(1);

	q.add(async () => {
		await sleep(1);
		throw new Error('nope');
	}).catch((e) => {
		assert.equal(e.message, 'nope');
	});

	try {
		await q.done();
		assert.ok(false);
	} catch (e) {
		assert.equal(/** @type {Error} */ (e).message, 'nope');
	}
});
Example #24
Source File: secure.js    From uid with MIT License 6 votes vote down vote up
test('length :: 4', () => {
	let i=0, tmp;
	for (; i < 1e3; i++) {
		tmp = uid(4);
		assert.is(tmp.length, 4, `"${tmp}" is not 4 characters!`);
	}

	assert.ok('~> produced 1000 IDs w/ 4 chars each');
});
Example #25
Source File: index.js    From nestie with MIT License 6 votes vote down vote up
test('custom glue', () => {
	const input = {
		'foo.bar': 123,
		'bar.baz': 456,
		'baz_bat': 789,
	};

	const input_string = JSON.stringify(input);

	assert.equal(
		nestie(input, '_'),
		{
			'foo.bar': 123,
			'bar.baz': 456,
			'baz': { bat: 789 },
		}
	);

	assert.is(
		input_string,
		JSON.stringify(input),
		'does not mutate original'
	);

	assert.equal(
		nestie(input, '~'),
		{
			'foo.bar': 123,
			'bar.baz': 456,
			'baz_bat': 789,
		}
	);

	assert.is(
		input_string,
		JSON.stringify(input),
		'does not mutate original'
	);
});
Example #26
Source File: queue.spec.js    From kit with MIT License 5 votes vote down vote up
test('q.add fails if queue is already finished', async () => {
	const q = queue(1);
	q.add(() => {});

	await q.done();
	assert.throws(() => q.add(() => {}), /Cannot add tasks to a queue that has ended/);
});