From a412967e4a3f2bdcea9c5d04df0cedfb48088b2e Mon Sep 17 00:00:00 2001 From: Evan Hahn Date: Wed, 4 Sep 2024 10:21:41 -0500 Subject: [PATCH] test: properly test more `validate` errors (#249) --- test/index.test.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index 7418bac..b28f4f6 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -21,6 +21,10 @@ import { } from './fixtures/index.js' import { cachedValues } from './fixtures/cached.js' +/** @import { SchemaName } from '../dist/types.js' */ + +const schemaNames = /** @type {SchemaName[]} */ (Object.keys(dataTypeIds)) + test('Bad docs throw when encoding', () => { for (const { text, doc } of badDocs) { assert.throws(() => { @@ -30,19 +34,29 @@ test('Bad docs throw when encoding', () => { } }) -test(`Bad docs won't validate`, () => { +test('Bad docs throw when validating if bad schema name', () => { for (const { text, doc } of badDocs) { + const { schemaName } = doc + if (isSchemaName(schemaName)) continue + assert.throws(() => { - // @ts-expect-error - validate(doc) + validate(/** @type {any} */ (schemaName), doc) }, text) } }) -test('validate bad docs', () => { - for (const schemaName of Object.keys(currentSchemaVersions)) { +test(`Bad docs won't validate`, () => { + for (const { text, doc } of badDocs) { + const { schemaName } = doc + if (!isSchemaName(schemaName)) continue + + assert(!validate(schemaName, doc), text) + } +}) + +test('validate empty docs', () => { + for (const schemaName of schemaNames) { assert( - // @ts-ignore !validate(schemaName, {}), `${schemaName} with missing properties should not validate` ) @@ -295,6 +309,14 @@ test(`decoding observation with missing position provider props`, async () => { ) }) +/** + * @param {unknown} value + * @returns {value is SchemaName} + */ +function isSchemaName(value) { + return schemaNames.includes(/** @type {any} */ (value)) +} + /** * Remove undefined properties (deeply) from an object, by round-tripping to * JSON. Also handles Buffers via JSON.parse reviver