From 3798318d5c37905e711b381ee7ba1bd7cd14c8f9 Mon Sep 17 00:00:00 2001 From: Andrea Carraro Date: Mon, 18 Sep 2023 19:11:26 +0200 Subject: [PATCH] test: generate schemas in different paths (#78) * test: import fixture path * test: remove importFresh utility --- README.md | 2 +- test/circularReference.test.ts | 19 +++++------- test/dereferencing.test.ts | 16 +++++----- test/externalRefs.test.ts | 13 ++++---- test/idExport.test.ts | 13 ++++---- test/index.test.ts | 30 +++++++++---------- test/jsonOpenAPI.test.ts | 9 +++--- test/metaData.test.ts | 4 +-- test/outputPath.test.ts | 28 +++++++++++++++-- test/parameters.test.ts | 9 +++--- .../plugins/fastifyTypeProviderPlugin.test.ts | 25 +++++++--------- test/refHandling-import.test.ts | 18 +++-------- test/refHandling-inline.test.ts | 4 +-- test/refHandling-keep.test.ts | 17 +++++------ test/schemaPatcher.test.ts | 13 ++++---- test/serialization.test.ts | 9 +++--- test/silent.test.ts | 4 +-- test/test-utils/fixtures.ts | 3 ++ test/test-utils/importFresh.ts | 8 ----- test/test-utils/index.ts | 3 +- test/test-utils/makeTestOutputPath.ts | 10 +++++++ 21 files changed, 128 insertions(+), 129 deletions(-) create mode 100644 test/test-utils/fixtures.ts delete mode 100644 test/test-utils/importFresh.ts create mode 100644 test/test-utils/makeTestOutputPath.ts diff --git a/README.md b/README.md index cb6c85f..4a3c988 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ type MyModel = FromSchema; Generated JSON schema path names get escaped in order to be valid file system names. -Circular `$ref`s can be technically resolved with "import" `refHandling` option. But TS will stop the type recursion and type the schema as `any`. See [relevant tests](https://github.com/toomuchdesign/openapi-ts-json-schema/blob/master/test/circularReference.test.ts). +Circular `$ref`s can be technically resolved with "import" `refHandling` option. But TS will stop the type recursion and type the schema as `any` (error `ts(7022)`). See [relevant tests](https://github.com/toomuchdesign/openapi-ts-json-schema/blob/master/test/circularReference.test.ts). Take a look at the [Developer's notes](./docs/developer-notes.md) for a few more in-depth explanations. diff --git a/test/circularReference.test.ts b/test/circularReference.test.ts index b71244b..021faec 100644 --- a/test/circularReference.test.ts +++ b/test/circularReference.test.ts @@ -1,21 +1,20 @@ import path from 'path'; import { describe, it, expect } from 'vitest'; -import { importFresh } from './test-utils'; +import { fixtures, makeTestOutputPath } from './test-utils'; import { openapiToTsJsonSchema } from '../src'; -const fixtures = path.resolve(__dirname, 'fixtures'); - describe('Circular reference', () => { - it('Works', async () => { + it("Doesn't break", async () => { const { outputPath } = await openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'circular-reference/specs.yaml'), + outputPath: makeTestOutputPath('circular'), definitionPathsToGenerateFrom: ['components.schemas'], refHandling: 'import', silent: true, }); - const januarySchema = await importFresh( - path.resolve(outputPath, 'components/schemas/January'), + const januarySchema = await import( + path.resolve(outputPath, 'components/schemas/January') ); expect(januarySchema.default).toEqual({ @@ -26,12 +25,8 @@ describe('Circular reference', () => { description: 'February description', type: 'object', properties: { - previousMonth: { - description: 'January description', - type: 'object', - // @NOTE JS engine seems to stop recursion - properties: { nextMonth: undefined }, - }, + // Node stops recursion + previousMonth: undefined, }, }, }, diff --git a/test/dereferencing.test.ts b/test/dereferencing.test.ts index 17c2e4c..b0a1533 100644 --- a/test/dereferencing.test.ts +++ b/test/dereferencing.test.ts @@ -1,20 +1,19 @@ import path from 'path'; import { describe, it, expect } from 'vitest'; -import { importFresh } from './test-utils'; +import { fixtures, makeTestOutputPath } from './test-utils'; import { openapiToTsJsonSchema } from '../src'; -const fixtures = path.resolve(__dirname, 'fixtures'); - -describe('Deferencing', () => { +describe('Dereferencing', () => { it('Dereferences and transforms even from paths not marked for generation', async () => { const { outputPath } = await openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'mini-referenced/specs.yaml'), + outputPath: makeTestOutputPath('dereferencing'), definitionPathsToGenerateFrom: ['components.months'], silent: true, }); - const januarySchema = await importFresh( - path.resolve(outputPath, 'components/months/January'), + const januarySchema = await import( + path.resolve(outputPath, 'components/months/January') ); expect(januarySchema.default).toEqual({ @@ -30,12 +29,13 @@ describe('Deferencing', () => { it('Transforms deeply nested schemas', async () => { const { outputPath } = await openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'complex/specs.yaml'), + outputPath: makeTestOutputPath('dereferencing'), definitionPathsToGenerateFrom: ['paths'], silent: true, }); - const pathsSchema = await importFresh( - path.resolve(outputPath, 'paths/v1|path-1'), + const pathsSchema = await import( + path.resolve(outputPath, 'paths/v1|path-1') ); expect( diff --git a/test/externalRefs.test.ts b/test/externalRefs.test.ts index 4de46f0..3f2bbe3 100644 --- a/test/externalRefs.test.ts +++ b/test/externalRefs.test.ts @@ -1,20 +1,19 @@ import path from 'path'; import { describe, it, expect } from 'vitest'; -import { importFresh } from './test-utils'; +import { fixtures, makeTestOutputPath } from './test-utils'; import { openapiToTsJsonSchema } from '../src'; -const fixtures = path.resolve(__dirname, 'fixtures'); - describe('External $ref', () => { it('Resolve external refs', async () => { const { outputPath } = await openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'external-ref/specs.yaml'), + outputPath: makeTestOutputPath('external-refs'), definitionPathsToGenerateFrom: ['components.schemas'], silent: true, }); - const externalDefinitionSchema = await importFresh( - path.resolve(outputPath, 'components/schemas/ExternalDefinition'), + const externalDefinitionSchema = await import( + path.resolve(outputPath, 'components/schemas/ExternalDefinition') ); expect(externalDefinitionSchema.default).toEqual({ @@ -23,11 +22,11 @@ describe('External $ref', () => { enum: ['yes', 'no', null], }); - const localDefinitionReferencingExternalSchema = await importFresh( + const localDefinitionReferencingExternalSchema = await import( path.resolve( outputPath, 'components/schemas/LocalDefinitionReferencingExternal', - ), + ) ); expect(localDefinitionReferencingExternalSchema.default).toEqual({ diff --git a/test/idExport.test.ts b/test/idExport.test.ts index 17be872..6d5ee99 100644 --- a/test/idExport.test.ts +++ b/test/idExport.test.ts @@ -1,23 +1,22 @@ import path from 'path'; import { describe, it, expect } from 'vitest'; import { openapiToTsJsonSchema } from '../src'; -import { importFresh } from './test-utils'; - -const fixtures = path.resolve(__dirname, 'fixtures'); +import { fixtures, makeTestOutputPath } from './test-utils'; describe('$id export', async () => { it('exposes schema id as $id named export', async () => { const { outputPath } = await openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'complex/specs.yaml'), + outputPath: makeTestOutputPath('id-export'), definitionPathsToGenerateFrom: ['components.months'], silent: true, }); - const januarySchema = await importFresh( - path.resolve(outputPath, 'components/months/January'), + const januarySchema = await import( + path.resolve(outputPath, 'components/months/January') ); - const februarySchema = await importFresh( - path.resolve(outputPath, 'components/months/February'), + const februarySchema = await import( + path.resolve(outputPath, 'components/months/February') ); expect(januarySchema['$id']).toBe('#/components/months/January'); diff --git a/test/index.test.ts b/test/index.test.ts index 7716aff..d9da74c 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,34 +1,27 @@ import path from 'path'; import { existsSync } from 'fs'; import { describe, it, expect, beforeEach, vi } from 'vitest'; -import { importFresh } from './test-utils'; +import { fixtures, makeTestOutputPath } from './test-utils'; import { openapiToTsJsonSchema } from '../src'; -const fixtures = path.resolve(__dirname, 'fixtures'); - describe('openapiToTsJsonSchema', () => { it('Generates expected JSON schemas', async () => { const { outputPath } = await openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'complex/specs.yaml'), + outputPath: makeTestOutputPath('index'), definitionPathsToGenerateFrom: ['paths', 'components.months'], silent: true, }); - expect(outputPath).toBe( - path.resolve(fixtures, 'complex/schemas-autogenerated'), - ); - - const januarySchema = await importFresh( - path.resolve(outputPath, 'components/months/January'), + const januarySchema = await import( + path.resolve(outputPath, 'components/months/January') ); - const februarySchema = await importFresh( - path.resolve(outputPath, 'components/months/February'), + const februarySchema = await import( + path.resolve(outputPath, 'components/months/February') ); // definition paths get escaped - const path1 = await importFresh( - path.resolve(outputPath, 'paths/v1|path-1'), - ); + const path1 = await import(path.resolve(outputPath, 'paths/v1|path-1')); expect(januarySchema.default).toEqual({ description: 'January description', @@ -90,8 +83,11 @@ describe('openapiToTsJsonSchema', () => { }); it('deletes previously generated schemas', async () => { - const { outputPath } = await openapiToTsJsonSchema({ + const outputPath = makeTestOutputPath('index'); + + await openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'mini-referenced/specs.yaml'), + outputPath, definitionPathsToGenerateFrom: ['components.schemas'], silent: true, }); @@ -106,6 +102,7 @@ describe('openapiToTsJsonSchema', () => { await openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'mini-referenced/specs.yaml'), + outputPath, definitionPathsToGenerateFrom: ['components.months'], silent: true, }); @@ -116,6 +113,7 @@ describe('openapiToTsJsonSchema', () => { await expect(() => openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'does-not-exist.yaml'), + outputPath: makeTestOutputPath('index'), definitionPathsToGenerateFrom: ['components'], silent: true, }), @@ -134,6 +132,7 @@ describe('openapiToTsJsonSchema', () => { it('logs expected message', async () => { await openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'mini-referenced/specs.yaml'), + outputPath: makeTestOutputPath('index'), definitionPathsToGenerateFrom: [], }); @@ -148,6 +147,7 @@ describe('openapiToTsJsonSchema', () => { await expect( openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'mini-referenced/specs.yaml'), + outputPath: makeTestOutputPath('index'), definitionPathsToGenerateFrom: ['paths', '/components.schema'], }), ).rejects.toThrow( diff --git a/test/jsonOpenAPI.test.ts b/test/jsonOpenAPI.test.ts index 3253279..b73bd95 100644 --- a/test/jsonOpenAPI.test.ts +++ b/test/jsonOpenAPI.test.ts @@ -1,20 +1,19 @@ import path from 'path'; import { describe, it, expect } from 'vitest'; -import { importFresh } from './test-utils'; +import { fixtures, makeTestOutputPath } from './test-utils'; import { openapiToTsJsonSchema } from '../src'; -const fixtures = path.resolve(__dirname, 'fixtures'); - describe('JSON OpenAPI input', async () => { it('generates expected schemas', async () => { const { outputPath } = await openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'json/specs.json'), + outputPath: makeTestOutputPath('json'), definitionPathsToGenerateFrom: ['components.months'], silent: true, }); - const januarySchema = await importFresh( - path.resolve(outputPath, 'components/months/January'), + const januarySchema = await import( + path.resolve(outputPath, 'components/months/January') ); expect(januarySchema.default).toEqual({ diff --git a/test/metaData.test.ts b/test/metaData.test.ts index 4ae1976..0829dc5 100644 --- a/test/metaData.test.ts +++ b/test/metaData.test.ts @@ -2,13 +2,13 @@ import path from 'path'; import fs from 'fs'; import { describe, it, expect } from 'vitest'; import { openapiToTsJsonSchema } from '../src'; - -const fixtures = path.resolve(__dirname, 'fixtures'); +import { fixtures, makeTestOutputPath } from './test-utils'; describe('Returned "metaData"', async () => { it('returns expected data', async () => { const { metaData } = await openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'mini-referenced/specs.yaml'), + outputPath: makeTestOutputPath('meta-data'), definitionPathsToGenerateFrom: ['components.months'], refHandling: 'import', silent: true, diff --git a/test/outputPath.test.ts b/test/outputPath.test.ts index 318baa1..625134b 100644 --- a/test/outputPath.test.ts +++ b/test/outputPath.test.ts @@ -1,11 +1,10 @@ import path from 'path'; import fs from 'fs'; import { describe, it, expect } from 'vitest'; +import { fixtures } from './test-utils'; import { openapiToTsJsonSchema } from '../src'; -const fixtures = path.resolve(__dirname, 'fixtures'); - -describe('"outputPath" option', async () => { +describe('"outputPath" option', () => { it('saves generated schemas to provided path', async () => { const customOutputPath = path.resolve( fixtures, @@ -29,4 +28,27 @@ describe('"outputPath" option', async () => { ); expect(fs.existsSync(expectedGeneratedSchemaPath)).toBe(true); }); + + describe('no value provided', () => { + it('saves generated schemas in a "schemas-autogenerated" folder next to open api schema', async () => { + const { outputPath } = await openapiToTsJsonSchema({ + openApiSchema: path.resolve(fixtures, 'mini-referenced/specs.yaml'), + definitionPathsToGenerateFrom: ['components.schemas'], + silent: true, + }); + + const expectedOutputPath = path.resolve( + fixtures, + 'mini-referenced/schemas-autogenerated', + ); + expect(outputPath).toBe(expectedOutputPath); + + const expectedGeneratedSchemaPath = path.resolve( + expectedOutputPath, + 'components/schemas', + 'Answer.ts', + ); + expect(fs.existsSync(expectedGeneratedSchemaPath)).toBe(true); + }); + }); }); diff --git a/test/parameters.test.ts b/test/parameters.test.ts index 2d17e35..6eec957 100644 --- a/test/parameters.test.ts +++ b/test/parameters.test.ts @@ -1,20 +1,19 @@ import path from 'path'; import { describe, it, expect } from 'vitest'; -import { importFresh } from './test-utils'; +import { fixtures, makeTestOutputPath } from './test-utils'; import { openapiToTsJsonSchema } from '../src'; -const fixtures = path.resolve(__dirname, 'fixtures'); - describe('OpenAPI parameters', () => { it('Transforms parameters array into a JSON schema record', async () => { const { outputPath } = await openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'parameters/specs.yaml'), + outputPath: makeTestOutputPath('parameters'), definitionPathsToGenerateFrom: ['paths'], silent: true, }); - const pathSchema = await importFresh( - path.resolve(outputPath, 'paths/v1|path-1'), + const pathSchema = await import( + path.resolve(outputPath, 'paths/v1|path-1') ); expect(pathSchema.default).toEqual({ diff --git a/test/plugins/fastifyTypeProviderPlugin.test.ts b/test/plugins/fastifyTypeProviderPlugin.test.ts index 446dfad..67aceec 100644 --- a/test/plugins/fastifyTypeProviderPlugin.test.ts +++ b/test/plugins/fastifyTypeProviderPlugin.test.ts @@ -3,19 +3,14 @@ import fs from 'fs/promises'; import { describe, it, expect } from 'vitest'; import { openapiToTsJsonSchema } from '../../src'; import { fastifyTypeProviderPlugin } from '../../src'; -import { importFresh } from '../test-utils'; +import { fixtures, makeTestOutputPath } from '../test-utils'; import { formatTypeScript } from '../../src/utils'; -const fixtures = path.resolve(__dirname, '../fixtures'); - describe('fastifyTypeProviderPlugin plugin', () => { it('generates expected file', async () => { const { outputPath } = await openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'complex/specs.yaml'), - outputPath: path.resolve( - fixtures, - 'complex/schemas-autogenerated-fastifyTypeProviderPlugin', - ), + outputPath: makeTestOutputPath('plugin-fastify'), definitionPathsToGenerateFrom: ['components.months', 'paths'], refHandling: 'keep', plugins: [fastifyTypeProviderPlugin()], @@ -63,17 +58,17 @@ describe('fastifyTypeProviderPlugin plugin', () => { expect(actualAsText).toBe(expectedAsText); // Ref schemas for fastify.addSchema - const answerSchema = await importFresh( - path.resolve(outputPath, 'components/schemas/Answer'), + const answerSchema = await import( + path.resolve(outputPath, 'components/schemas/Answer') ); - const januarySchema = await importFresh( - path.resolve(outputPath, 'components/months/January'), + const januarySchema = await import( + path.resolve(outputPath, 'components/months/January') ); - const februarySchema = await importFresh( - path.resolve(outputPath, 'components/months/February'), + const februarySchema = await import( + path.resolve(outputPath, 'components/months/February') ); - const actualParsed = await importFresh( - path.resolve(outputPath, 'fastifyTypeProvider'), + const actualParsed = await import( + path.resolve(outputPath, 'fastifyTypeProvider') ); expect(actualParsed.referenceSchemas).toEqual([ diff --git a/test/refHandling-import.test.ts b/test/refHandling-import.test.ts index 402ed02..65c99c1 100644 --- a/test/refHandling-import.test.ts +++ b/test/refHandling-import.test.ts @@ -1,12 +1,10 @@ import path from 'path'; import fs from 'fs/promises'; import { describe, it, expect } from 'vitest'; -import { importFresh } from './test-utils'; +import { fixtures, makeTestOutputPath } from './test-utils'; import { openapiToTsJsonSchema } from '../src'; import { formatTypeScript } from '../src/utils'; -const fixtures = path.resolve(__dirname, 'fixtures'); - describe('refHandling option === "import"', () => { describe.each([ { @@ -25,18 +23,13 @@ describe('refHandling option === "import"', () => { it('Generates expected schema', async () => { const { outputPath } = await openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'complex/specs.yaml'), - outputPath: path.resolve( - fixtures, - 'complex/schemas-autogenerated-refHandling-import', - ), + outputPath: makeTestOutputPath('refHandling-import'), definitionPathsToGenerateFrom, silent: true, refHandling: 'import', }); - const path1 = await importFresh( - path.resolve(outputPath, 'paths/v1|path-1'), - ); + const path1 = await import(path.resolve(outputPath, 'paths/v1|path-1')); // Expectations against parsed root schema expect(path1.default).toEqual({ @@ -115,10 +108,7 @@ describe('refHandling option === "import"', () => { it('Generates expected $ref schemas', async () => { const { outputPath } = await openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'complex/specs.yaml'), - outputPath: path.resolve( - fixtures, - 'complex/schemas-autogenerated-refHandling-import', - ), + outputPath: makeTestOutputPath('refHandling-import'), definitionPathsToGenerateFrom: ['paths'], silent: true, refHandling: 'import', diff --git a/test/refHandling-inline.test.ts b/test/refHandling-inline.test.ts index f616f5e..30be834 100644 --- a/test/refHandling-inline.test.ts +++ b/test/refHandling-inline.test.ts @@ -2,13 +2,13 @@ import path from 'path'; import fs from 'fs/promises'; import { describe, it, expect } from 'vitest'; import { openapiToTsJsonSchema } from '../src'; - -const fixtures = path.resolve(__dirname, 'fixtures'); +import { fixtures, makeTestOutputPath } from './test-utils'; describe('refHandling option === "inline"', () => { it('Preserves original "$ref" information as a commented prop', async () => { const { outputPath } = await openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'mini-referenced/specs.yaml'), + outputPath: makeTestOutputPath('refHandling-inline'), definitionPathsToGenerateFrom: ['components.months'], refHandling: 'inline', silent: true, diff --git a/test/refHandling-keep.test.ts b/test/refHandling-keep.test.ts index d36c9e5..7f0846b 100644 --- a/test/refHandling-keep.test.ts +++ b/test/refHandling-keep.test.ts @@ -1,22 +1,19 @@ import path from 'path'; import { describe, it, expect } from 'vitest'; -import { importFresh } from './test-utils'; +import { fixtures, makeTestOutputPath } from './test-utils'; import { openapiToTsJsonSchema } from '../src'; -const fixtures = path.resolve(__dirname, 'fixtures'); - describe('refHandling option === "keep"', () => { it('Generates expected schemas preserving $ref pointer', async () => { const { outputPath } = await openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'complex/specs.yaml'), + outputPath: makeTestOutputPath('refHandling-keep'), definitionPathsToGenerateFrom: ['paths'], silent: true, refHandling: 'keep', }); - const path1 = await importFresh( - path.resolve(outputPath, 'paths/v1|path-1'), - ); + const path1 = await import(path.resolve(outputPath, 'paths/v1|path-1')); // Expectations against parsed root schema expect(path1.default).toEqual({ @@ -48,8 +45,8 @@ describe('refHandling option === "keep"', () => { refHandling: 'keep', }); - const januarySchema = await importFresh( - path.resolve(outputPath, 'components/months/January'), + const januarySchema = await import( + path.resolve(outputPath, 'components/months/January') ); expect(januarySchema.default).toEqual({ @@ -61,8 +58,8 @@ describe('refHandling option === "keep"', () => { }, }); - const answerSchema = await importFresh( - path.resolve(outputPath, 'components/schemas/Answer'), + const answerSchema = await import( + path.resolve(outputPath, 'components/schemas/Answer') ); expect(answerSchema.default).toEqual({ diff --git a/test/schemaPatcher.test.ts b/test/schemaPatcher.test.ts index 21a141d..18597b1 100644 --- a/test/schemaPatcher.test.ts +++ b/test/schemaPatcher.test.ts @@ -1,14 +1,13 @@ import path from 'path'; import { describe, it, expect } from 'vitest'; -import { importFresh } from './test-utils'; +import { fixtures, makeTestOutputPath } from './test-utils'; import { openapiToTsJsonSchema } from '../src'; -const fixtures = path.resolve(__dirname, 'fixtures'); - describe('"schemaPatcher" option', () => { it('transforms generated JSON schemas', async () => { const { outputPath } = await openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'complex/specs.yaml'), + outputPath: makeTestOutputPath('schema-patcher'), definitionPathsToGenerateFrom: ['components.months', 'paths'], schemaPatcher: ({ schema }) => { if (schema.description === 'January description') { @@ -18,8 +17,8 @@ describe('"schemaPatcher" option', () => { silent: true, }); - const januarySchema = await importFresh( - path.resolve(outputPath, 'components/months/January'), + const januarySchema = await import( + path.resolve(outputPath, 'components/months/January') ); expect(januarySchema.default).toEqual({ @@ -32,8 +31,8 @@ describe('"schemaPatcher" option', () => { }); // Testing deep nested props being patched, too - const pathSchema = await importFresh( - path.resolve(outputPath, 'paths/v1|path-1'), + const pathSchema = await import( + path.resolve(outputPath, 'paths/v1|path-1') ); expect( diff --git a/test/serialization.test.ts b/test/serialization.test.ts index 3b07edd..a9900a4 100644 --- a/test/serialization.test.ts +++ b/test/serialization.test.ts @@ -1,20 +1,19 @@ import path from 'path'; import { describe, it, expect } from 'vitest'; import { openapiToTsJsonSchema } from '../src'; -import { importFresh } from './test-utils'; - -const fixtures = path.resolve(__dirname, 'fixtures'); +import { fixtures, makeTestOutputPath } from './test-utils'; describe('openapiToTsJsonSchema', async () => { it('serializes strings as expected', async () => { const { outputPath } = await openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'serialization/specs.yaml'), + outputPath: makeTestOutputPath('serialization'), definitionPathsToGenerateFrom: ['components.schemas'], silent: true, }); - const actual = await importFresh( - path.resolve(outputPath, 'components/schemas/Foo'), + const actual = await import( + path.resolve(outputPath, 'components/schemas/Foo') ); const expected = { diff --git a/test/silent.test.ts b/test/silent.test.ts index 736d27b..77d55a6 100644 --- a/test/silent.test.ts +++ b/test/silent.test.ts @@ -1,8 +1,7 @@ import path from 'path'; import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; import { openapiToTsJsonSchema } from '../src'; - -const fixtures = path.resolve(__dirname, 'fixtures'); +import { fixtures, makeTestOutputPath } from './test-utils'; beforeEach(() => { vi.spyOn(console, 'log').mockImplementationOnce(() => undefined); @@ -16,6 +15,7 @@ describe('"silent" option', async () => { it('console.log user messages', async () => { const { outputPath } = await openapiToTsJsonSchema({ openApiSchema: path.resolve(fixtures, 'mini-referenced/specs.yaml'), + outputPath: makeTestOutputPath('silent-option'), definitionPathsToGenerateFrom: ['paths'], silent: false, }); diff --git a/test/test-utils/fixtures.ts b/test/test-utils/fixtures.ts new file mode 100644 index 0000000..d0ab97c --- /dev/null +++ b/test/test-utils/fixtures.ts @@ -0,0 +1,3 @@ +import path from 'path'; + +export const fixtures = path.resolve(__dirname, '../fixtures'); diff --git a/test/test-utils/importFresh.ts b/test/test-utils/importFresh.ts deleted file mode 100644 index 752a4cb..0000000 --- a/test/test-utils/importFresh.ts +++ /dev/null @@ -1,8 +0,0 @@ -/** - * It doesn't work for sub-imports - * https://github.com/small-tech/import-fresh - */ -export async function importFresh(absolutePathToModule: string) { - const cacheBustingModulePath = `${absolutePathToModule}?update=${Date.now()}`; - return await import(cacheBustingModulePath); -} diff --git a/test/test-utils/index.ts b/test/test-utils/index.ts index e54f944..46729d9 100644 --- a/test/test-utils/index.ts +++ b/test/test-utils/index.ts @@ -1 +1,2 @@ -export { importFresh } from './importFresh'; +export { fixtures } from './fixtures'; +export { makeTestOutputPath } from './makeTestOutputPath'; diff --git a/test/test-utils/makeTestOutputPath.ts b/test/test-utils/makeTestOutputPath.ts new file mode 100644 index 0000000..0140f18 --- /dev/null +++ b/test/test-utils/makeTestOutputPath.ts @@ -0,0 +1,10 @@ +import path from 'path'; +import { fixtures } from './fixtures'; + +/** + * Generate output paths in the fixtures folder + * as a schemas-autogenerated-* folder + */ +export function makeTestOutputPath(id: string = 'no-id'): string { + return path.resolve(fixtures, `schemas-autogenerated-${id}-${Date.now()}`); +}