diff --git a/package.json b/package.json index e25f3f0..e06e052 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "lint:fix": "eslint --fix", "test": "npm run test:unit && npm run test:typescript", "test:unit": "c8 --100 node --test", - "test:typescript": "tsd", + "test:typescript": "tstyche", "doc": "jsdoc2md ./src/*.js > docs/API.md" }, "devDependencies": { @@ -53,7 +53,7 @@ "jsdoc-to-markdown": "^9.0.0", "lodash.merge": "^4.6.2", "neostandard": "^0.13.0", - "tsd": "^0.33.0" + "tstyche": "^7.1.0" }, "dependencies": { "@fastify/deepmerge": "^3.0.0" diff --git a/types/FluentJSONSchema.test-d.ts b/types/FluentJSONSchema.tst.ts similarity index 52% rename from types/FluentJSONSchema.test-d.ts rename to types/FluentJSONSchema.tst.ts index e105b88..4994d73 100644 --- a/types/FluentJSONSchema.test-d.ts +++ b/types/FluentJSONSchema.tst.ts @@ -1,8 +1,11 @@ -// This file will be passed to the TypeScript CLI to verify our typings compile +import { expect } from 'tstyche' import S, { FluentSchemaError } from '..' -console.log('isFluentSchema:', S.object().isFluentJSONSchema) +// isFluentJSONSchema and isFluentSchema properties +expect(S.object().isFluentJSONSchema).type.toBe() +expect(S.object().isFluentSchema).type.toBe() + const schema = S.object() .id('http://foo.com/user') .title('A User') @@ -46,23 +49,49 @@ const schema = S.object() .ifThen(S.object().prop('age', S.string()), S.required(['age'])) .readOnly() .writeOnly(true) - .valueOf() -console.log('example:\n', JSON.stringify(schema)) -console.log('isFluentSchema:', S.object().isFluentSchema) +// Assertions for schema methods BEFORE valueOf +expect(schema).type.toHaveProperty('id') +expect(schema).type.toHaveProperty('title') +expect(schema).type.toHaveProperty('description') +expect(schema).type.toHaveProperty('definition') +expect(schema).type.toHaveProperty('prop') +expect(schema).type.toHaveProperty('required') +expect(schema).type.toHaveProperty('isFluentSchema') +expect(schema).type.toHaveProperty('readOnly') +expect(schema).type.toHaveProperty('writeOnly') + +// String method chain assertions +expect(S.string().pattern(/[a-z]*/g)).type.toHaveProperty('pattern') +expect(S.string().format('email')).type.toHaveProperty('format') +expect(S.string().format(S.FORMATS.EMAIL)).type.toHaveProperty('format') +expect(S.string().contentEncoding('base64')).type.toHaveProperty('contentEncoding') +expect(S.string().contentMediaType('image/png')).type.toHaveProperty('contentMediaType') +expect(S.string().minLength(6)).type.toHaveProperty('minLength') +expect(S.string().maxLength(12)).type.toHaveProperty('maxLength') +expect(S.string().default('123456')).type.toHaveProperty('default') + +expect(schema.valueOf()).type.toBe() +expect(S.object().isFluentSchema).type.toBe() const userBaseSchema = S.object() .additionalProperties(false) .prop('username', S.string()) .prop('password', S.string()) +// ObjectSchema methods assertions +expect(userBaseSchema).type.toHaveProperty('additionalProperties') +expect(userBaseSchema).type.toHaveProperty('prop') + const userSchema = S.object() .prop('id', S.string().format('uuid')) .prop('createdAt', S.string().format('time')) .prop('updatedAt', S.string().format('time')) .extend(userBaseSchema) -console.log('user:\n', JSON.stringify(userSchema.valueOf())) +expect(userSchema).type.toHaveProperty('extend') + +expect(userSchema.valueOf()).type.toBe() const largeUserSchema = S.object() .prop('id', S.string().format('uuid')) @@ -73,7 +102,8 @@ const largeUserSchema = S.object() const userSubsetSchema = largeUserSchema.only(['username', 'password']) -console.log('user subset:', JSON.stringify(userSubsetSchema.valueOf())) +expect(userSubsetSchema.valueOf()).type.toBe() +expect(userSubsetSchema).type.toHaveProperty('only') const personSchema = S.object() .prop('name', S.string()) @@ -84,7 +114,8 @@ const personSchema = S.object() const bodySchema = personSchema.without(['createdAt', 'updatedAt']) -console.log('person subset:', JSON.stringify(bodySchema.valueOf())) +expect(bodySchema.valueOf()).type.toBe() +expect(bodySchema).type.toHaveProperty('without') const personSchemaAllowsUnix = S.object() .prop('name', S.string()) @@ -93,23 +124,27 @@ const personSchemaAllowsUnix = S.object() .prop('createdAt', S.mixed(['string', 'integer']).format('time')) .prop('updatedAt', S.mixed(['string', 'integer']).minimum(0)) -console.log('person schema allows unix:', JSON.stringify(personSchemaAllowsUnix.valueOf())) +expect(personSchemaAllowsUnix.valueOf()).type.toBe() +expect(personSchemaAllowsUnix).type.toHaveProperty('prop') try { S.object().prop('foo', 'boom!' as any) } catch (e) { if (e instanceof FluentSchemaError) { - console.log(e.message) + expect(e.message).type.toBe() + expect(e).type.toBe() } } -const arrayExtendedSchema = S.array().items(userSchema).valueOf() +const arrayExtendedSchema = S.array().items(userSchema) -console.log('array of user\n', JSON.stringify(arrayExtendedSchema)) +expect(arrayExtendedSchema.valueOf()).type.toBe() +expect(arrayExtendedSchema).type.toHaveProperty('items') const extendExtendedSchema = S.object().extend(userSchema) -console.log('extend of user\n', JSON.stringify(extendExtendedSchema)) +expect(extendExtendedSchema.valueOf()).type.toBe() +expect(extendExtendedSchema).type.toHaveProperty('extend') const rawNullableSchema = S.object() .raw({ nullable: true }) @@ -117,7 +152,9 @@ const rawNullableSchema = S.object() .prop('foo', S.string()) .prop('hello', S.string()) -console.log('raw schema with nullable props\n', JSON.stringify(rawNullableSchema)) +expect(rawNullableSchema.valueOf()).type.toBe() +expect(rawNullableSchema).type.toHaveProperty('raw') +expect(rawNullableSchema).type.toHaveProperty('required') const dependentRequired = S.object() .dependentRequired({ @@ -125,25 +162,25 @@ const dependentRequired = S.object() }) .prop('foo') .prop('bar') - .valueOf() -console.log('dependentRequired:\n', JSON.stringify(dependentRequired)) +expect(dependentRequired.valueOf()).type.toBe() +expect(dependentRequired).type.toHaveProperty('dependentRequired') const dependentSchemas = S.object() .dependentSchemas({ foo: S.object().prop('bar'), }) .prop('bar', S.object().prop('bar')) - .valueOf() -console.log('dependentRequired:\n', JSON.stringify(dependentSchemas)) +expect(dependentSchemas.valueOf()).type.toBe() +expect(dependentSchemas).type.toHaveProperty('dependentSchemas') const deprecatedSchema = S.object() .deprecated() .prop('foo', S.string().deprecated()) - .valueOf() -console.log('deprecatedSchema:\n', JSON.stringify(deprecatedSchema)) +expect(deprecatedSchema.valueOf()).type.toBe() +expect(deprecatedSchema).type.toHaveProperty('deprecated') type Foo = { foo: string @@ -156,25 +193,26 @@ const dependentRequiredWithType = S.object() }) .prop('foo') .prop('bar') - .valueOf() -console.log('dependentRequired:\n', JSON.stringify(dependentRequiredWithType)) +expect(dependentRequiredWithType.valueOf()).type.toBe() +expect(dependentRequiredWithType).type.toHaveProperty('dependentRequired') +expect(dependentRequiredWithType).type.toHaveProperty('prop') const dependentSchemasWithType = S.object() .dependentSchemas({ foo: S.object().prop('bar'), }) .prop('bar', S.object().prop('bar')) - .valueOf() -console.log('dependentSchemasWithType:\n', JSON.stringify(dependentSchemasWithType)) +expect(dependentSchemasWithType.valueOf()).type.toBe() +expect(dependentSchemasWithType).type.toHaveProperty('dependentSchemas') const deprecatedSchemaWithType = S.object() .deprecated() .prop('foo', S.string().deprecated()) - .valueOf() -console.log('deprecatedSchemaWithType:\n', JSON.stringify(deprecatedSchemaWithType)) +expect(deprecatedSchemaWithType.valueOf()).type.toBe() +expect(deprecatedSchemaWithType).type.toHaveProperty('deprecated') type ReallyLongType = { foo: string @@ -194,13 +232,16 @@ const deepTestOnTypes = S.object() // you can provide any string, to avoid breaking changes .prop('aaaa', S.anyOf([S.string()])) .definition('abcd', S.number()) - .valueOf() -console.log('deepTestOnTypes:\n', JSON.stringify(deepTestOnTypes)) +expect(deepTestOnTypes.valueOf()).type.toBe() +expect(deepTestOnTypes).type.toHaveProperty('prop') +expect(deepTestOnTypes).type.toHaveProperty('definition') const tsIsoSchema = S.object() .prop('createdAt', S.string().format('iso-time')) .prop('updatedAt', S.string().format('iso-date-time')) - .valueOf() -console.log('ISO schema OK:', JSON.stringify(tsIsoSchema)) +expect(tsIsoSchema.valueOf()).type.toBe() +expect(tsIsoSchema).type.toHaveProperty('prop') +expect(S.string().format('iso-time')).type.toHaveProperty('format') +expect(S.string().format('iso-date-time')).type.toHaveProperty('format')