From 666ff02cd8b446fdae845d4e3550a85e748b3dbd Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Wed, 9 Nov 2022 23:56:56 +0100 Subject: test: use test snapshots for scripts/apidoc/signature.spec.ts (#1488) --- test/scripts/apidoc/.gitignore | 1 - .../apidoc/__snapshots__/signature.spec.ts.snap | 506 +++++++++++++++++++++ test/scripts/apidoc/signature.expected.json | 331 -------------- test/scripts/apidoc/signature.spec.ts | 28 +- 4 files changed, 510 insertions(+), 356 deletions(-) create mode 100644 test/scripts/apidoc/__snapshots__/signature.spec.ts.snap delete mode 100644 test/scripts/apidoc/signature.expected.json (limited to 'test/scripts') diff --git a/test/scripts/apidoc/.gitignore b/test/scripts/apidoc/.gitignore index 4d07e30e..a6d7ecd9 100644 --- a/test/scripts/apidoc/.gitignore +++ b/test/scripts/apidoc/.gitignore @@ -1,2 +1 @@ -*.actuals.json temp/ diff --git a/test/scripts/apidoc/__snapshots__/signature.spec.ts.snap b/test/scripts/apidoc/__snapshots__/signature.spec.ts.snap new file mode 100644 index 00000000..096e4b72 --- /dev/null +++ b/test/scripts/apidoc/__snapshots__/signature.spec.ts.snap @@ -0,0 +1,506 @@ +// Vitest Snapshot v1 + +exports[`signature > analyzeSignature() > defaultBooleanParamMethod 1`] = ` +{ + "deprecated": false, + "description": "

Test with a default parameter.

+", + "examples": "
ts
faker.defaultBooleanParamMethod(c: boolean = true): number
+
+
", + "name": "defaultBooleanParamMethod", + "parameters": [ + { + "default": "true", + "description": "

The boolean parameter.

+", + "name": "c", + "type": "boolean", + }, + ], + "returns": "number", + "seeAlsos": [], + "since": "", + "title": "Default Boolean Param Method", +} +`; + +exports[`signature > analyzeSignature() > expected and actual methods are equal 1`] = ` +[ + "defaultBooleanParamMethod", + "functionParamMethod", + "literalUnionParamMethod", + "methodWithDeprecated", + "methodWithExample", + "methodWithMultipleSeeMarkers", + "methodWithSinceMarker", + "multiParamMethod", + "noParamMethod", + "optionalStringParamMethod", + "optionsInlineParamMethodWithDefaults", + "optionsInterfaceParamMethodWithDefaults", + "optionsParamMethod", + "optionsTypeParamMethodWithDefaults", + "requiredNumberParamMethod", + "stringUnionParamMethod", +] +`; + +exports[`signature > analyzeSignature() > functionParamMethod 1`] = ` +{ + "deprecated": false, + "description": "

Test with a function parameters.

+", + "examples": "
ts
faker.functionParamMethod(fn: (a: string) => number): number
+
+
", + "name": "functionParamMethod", + "parameters": [ + { + "default": undefined, + "description": "

The function parameter.

+", + "name": "fn", + "type": "(a: string) => number", + }, + ], + "returns": "number", + "seeAlsos": [], + "since": "", + "title": "Function Param Method", +} +`; + +exports[`signature > analyzeSignature() > literalUnionParamMethod 1`] = ` +{ + "deprecated": false, + "description": "

Test with LiteralUnion.

+", + "examples": "
ts
faker.literalUnionParamMethod(value: 'a' | 'b' | string): string
+
+
", + "name": "literalUnionParamMethod", + "parameters": [ + { + "default": undefined, + "description": "

'a' or 'b'.

+", + "name": "value", + "type": "'a' | 'b' | string", + }, + ], + "returns": "string", + "seeAlsos": [], + "since": "", + "title": "Literal Union Param Method", +} +`; + +exports[`signature > analyzeSignature() > methodWithDeprecated 1`] = ` +{ + "deprecated": true, + "description": "

Test with deprecated and see marker.

+", + "examples": "
ts
faker.methodWithDeprecated(): number
+
+
", + "name": "methodWithDeprecated", + "parameters": [], + "returns": "number", + "seeAlsos": [ + "test.apidoc.methodWithExample()", + ], + "since": "", + "title": "Method With Deprecated", +} +`; + +exports[`signature > analyzeSignature() > methodWithExample 1`] = ` +{ + "deprecated": false, + "description": "

Test with example marker.

+", + "examples": "
ts
faker.methodWithExample(): number
+test.apidoc.methodWithExample() // 0
+
+
", + "name": "methodWithExample", + "parameters": [], + "returns": "number", + "seeAlsos": [], + "since": "", + "title": "Method With Example", +} +`; + +exports[`signature > analyzeSignature() > methodWithMultipleSeeMarkers 1`] = ` +{ + "deprecated": false, + "description": "

Test with multiple see markers.

+", + "examples": "
ts
faker.methodWithMultipleSeeMarkers(): number
+
+
", + "name": "methodWithMultipleSeeMarkers", + "parameters": [], + "returns": "number", + "seeAlsos": [ + "test.apidoc.methodWithExample()", + "test.apidoc.methodWithDeprecated()", + ], + "since": "", + "title": "Method With Multiple See Markers", +} +`; + +exports[`signature > analyzeSignature() > methodWithSinceMarker 1`] = ` +{ + "deprecated": false, + "description": "

Test with since marker.

+", + "examples": "
ts
faker.methodWithSinceMarker(): number
+
+
", + "name": "methodWithSinceMarker", + "parameters": [], + "returns": "number", + "seeAlsos": [], + "since": "1.0.0", + "title": "Method With Since Marker", +} +`; + +exports[`signature > analyzeSignature() > multiParamMethod 1`] = ` +{ + "deprecated": false, + "description": "

Test with multiple parameters.

+", + "examples": "
ts
faker.multiParamMethod(a: number, b?: string, c: boolean = true): number
+
+
", + "name": "multiParamMethod", + "parameters": [ + { + "default": undefined, + "description": "

The number parameter.

+", + "name": "a", + "type": "number", + }, + { + "default": undefined, + "description": "

The string parameter.

+", + "name": "b?", + "type": "string", + }, + { + "default": "true", + "description": "

The boolean parameter.

+", + "name": "c", + "type": "boolean", + }, + ], + "returns": "number", + "seeAlsos": [], + "since": "", + "title": "Multi Param Method", +} +`; + +exports[`signature > analyzeSignature() > noParamMethod 1`] = ` +{ + "deprecated": false, + "description": "

Test with no parameters.

+", + "examples": "
ts
faker.noParamMethod(): number
+
+
", + "name": "noParamMethod", + "parameters": [], + "returns": "number", + "seeAlsos": [], + "since": "", + "title": "No Param Method", +} +`; + +exports[`signature > analyzeSignature() > optionalStringParamMethod 1`] = ` +{ + "deprecated": false, + "description": "

Test with an optional parameter.

+", + "examples": "
ts
faker.optionalStringParamMethod(b?: string): number
+
+
", + "name": "optionalStringParamMethod", + "parameters": [ + { + "default": undefined, + "description": "

The string parameter.

+", + "name": "b?", + "type": "string", + }, + ], + "returns": "number", + "seeAlsos": [], + "since": "", + "title": "Optional String Param Method", +} +`; + +exports[`signature > analyzeSignature() > optionsInlineParamMethodWithDefaults 1`] = ` +{ + "deprecated": false, + "description": "

Test with a function parameters (inline types) with defaults.

+", + "examples": "
ts
faker.optionsInlineParamMethodWithDefaults(a: {
+  value: number
+} = { value: 1 }, b: {
+  value: number
+} = { value: 1 }, c: {
+  value: number
+}): number
+
+
", + "name": "optionsInlineParamMethodWithDefaults", + "parameters": [ + { + "default": "{ value: 1 }", + "description": "

Parameter with signature default. +It also has a more complex description.

+", + "name": "a", + "type": "{ ... }", + }, + { + "default": undefined, + "description": "

The number parameter.

+", + "name": "a.value?", + "type": "number", + }, + { + "default": "{ value: 1 }", + "description": "

Parameter with jsdocs default.

+

It also has a more complex description.

+", + "name": "b", + "type": "{ ... }", + }, + { + "default": undefined, + "description": "

The number parameter.

+", + "name": "b.value?", + "type": "number", + }, + { + "default": undefined, + "description": "

Parameter with inner jsdocs default.

+", + "name": "c", + "type": "{ ... }", + }, + { + "default": "2", + "description": "

The number parameter. It also has a more complex description.

+", + "name": "c.value?", + "type": "number", + }, + ], + "returns": "number", + "seeAlsos": [], + "since": "", + "title": "Options Inline Param Method With Defaults", +} +`; + +exports[`signature > analyzeSignature() > optionsInterfaceParamMethodWithDefaults 1`] = ` +{ + "deprecated": false, + "description": "

Test with a function parameters with defaults.

+", + "examples": "
ts
faker.optionsInterfaceParamMethodWithDefaults(a: ParameterOptionsInterfaceA = { value: 1 }, b: ParameterOptionsInterfaceB = { value: 1 }, c: ParameterOptionsInterfaceC): number
+
+
", + "name": "optionsInterfaceParamMethodWithDefaults", + "parameters": [ + { + "default": "{ value: 1 }", + "description": "

Parameter with signature default.

+", + "name": "a", + "type": "ParameterOptionsInterfaceA", + }, + { + "default": "{ value: 1 }", + "description": "

Parameter with jsdocs default.

+", + "name": "b", + "type": "ParameterOptionsInterfaceB", + }, + { + "default": undefined, + "description": "

Parameter with inner jsdocs default.

+", + "name": "c", + "type": "ParameterOptionsInterfaceC", + }, + ], + "returns": "number", + "seeAlsos": [], + "since": "", + "title": "Options Interface Param Method With Defaults", +} +`; + +exports[`signature > analyzeSignature() > optionsParamMethod 1`] = ` +{ + "deprecated": false, + "description": "

Test with a function parameters.

+", + "examples": "
ts
faker.optionsParamMethod(options: {
+  a: number,
+  b: string,
+  c: boolean,
+  d: () => string
+}): number
+
+
", + "name": "optionsParamMethod", + "parameters": [ + { + "default": undefined, + "description": "

The function parameter.

+", + "name": "options", + "type": "{ ... }", + }, + { + "default": undefined, + "description": "

The number parameter.

+", + "name": "options.a", + "type": "number", + }, + { + "default": undefined, + "description": "

The string parameter.

+", + "name": "options.b?", + "type": "string", + }, + { + "default": undefined, + "description": "

The boolean parameter.

+", + "name": "options.c", + "type": "boolean", + }, + { + "default": undefined, + "description": "

The method parameter.

+", + "name": "options.d", + "type": "() => string", + }, + ], + "returns": "number", + "seeAlsos": [], + "since": "", + "title": "Options Param Method", +} +`; + +exports[`signature > analyzeSignature() > optionsTypeParamMethodWithDefaults 1`] = ` +{ + "deprecated": false, + "description": "

Test with a function parameters with defaults.

+", + "examples": "
ts
faker.optionsTypeParamMethodWithDefaults(a: ParameterOptionsTypeA = { value: 1 }, b: ParameterOptionsTypeB = { value: 1 }, c: ParameterOptionsTypeC): number
+
+
", + "name": "optionsTypeParamMethodWithDefaults", + "parameters": [ + { + "default": "{ value: 1 }", + "description": "

Parameter with signature default.

+", + "name": "a", + "type": "ParameterOptionsTypeA", + }, + { + "default": "{ value: 1 }", + "description": "

Parameter with jsdocs default.

+", + "name": "b", + "type": "ParameterOptionsTypeB", + }, + { + "default": undefined, + "description": "

Parameter with inner jsdocs default.

+", + "name": "c", + "type": "ParameterOptionsTypeC", + }, + ], + "returns": "number", + "seeAlsos": [], + "since": "", + "title": "Options Type Param Method With Defaults", +} +`; + +exports[`signature > analyzeSignature() > requiredNumberParamMethod 1`] = ` +{ + "deprecated": false, + "description": "

Test with a required parameter.

+", + "examples": "
ts
faker.requiredNumberParamMethod(a: number): number
+
+
", + "name": "requiredNumberParamMethod", + "parameters": [ + { + "default": undefined, + "description": "

The number parameter.

+", + "name": "a", + "type": "number", + }, + ], + "returns": "number", + "seeAlsos": [], + "since": "", + "title": "Required Number Param Method", +} +`; + +exports[`signature > analyzeSignature() > stringUnionParamMethod 1`] = ` +{ + "deprecated": false, + "description": "

Test with string union.

+", + "examples": "
ts
faker.stringUnionParamMethod(value: 'a' | 'b'): string
+
+
", + "name": "stringUnionParamMethod", + "parameters": [ + { + "default": undefined, + "description": "

'a' or 'b'.

+", + "name": "value", + "type": "'a' | 'b'", + }, + ], + "returns": "string", + "seeAlsos": [], + "since": "", + "title": "String Union Param Method", +} +`; diff --git a/test/scripts/apidoc/signature.expected.json b/test/scripts/apidoc/signature.expected.json deleted file mode 100644 index 05809105..00000000 --- a/test/scripts/apidoc/signature.expected.json +++ /dev/null @@ -1,331 +0,0 @@ -{ - "defaultBooleanParamMethod": { - "name": "defaultBooleanParamMethod", - "title": "Default Boolean Param Method", - "description": "

Test with a default parameter.

\n", - "parameters": [ - { - "name": "c", - "type": "boolean", - "default": "true", - "description": "

The boolean parameter.

\n" - } - ], - "since": "", - "returns": "number", - "examples": "
ts
faker.defaultBooleanParamMethod(c: boolean = true): number\n
\n
", - "deprecated": false, - "seeAlsos": [] - }, - "functionParamMethod": { - "name": "functionParamMethod", - "title": "Function Param Method", - "description": "

Test with a function parameters.

\n", - "parameters": [ - { - "name": "fn", - "type": "(a: string) => number", - "description": "

The function parameter.

\n" - } - ], - "since": "", - "returns": "number", - "examples": "
ts
faker.functionParamMethod(fn: (a: string) => number): number\n
\n
", - "deprecated": false, - "seeAlsos": [] - }, - "literalUnionParamMethod": { - "name": "literalUnionParamMethod", - "title": "Literal Union Param Method", - "description": "

Test with LiteralUnion.

\n", - "parameters": [ - { - "name": "value", - "type": "'a' | 'b' | string", - "description": "

'a' or 'b'.

\n" - } - ], - "since": "", - "returns": "string", - "examples": "
ts
faker.literalUnionParamMethod(value: 'a' | 'b' | string): string\n
\n
", - "deprecated": false, - "seeAlsos": [] - }, - "methodWithDeprecated": { - "name": "methodWithDeprecated", - "title": "Method With Deprecated", - "description": "

Test with deprecated and see marker.

\n", - "parameters": [], - "since": "", - "returns": "number", - "examples": "
ts
faker.methodWithDeprecated(): number\n
\n
", - "deprecated": true, - "seeAlsos": ["test.apidoc.methodWithExample()"] - }, - "methodWithExample": { - "name": "methodWithExample", - "title": "Method With Example", - "description": "

Test with example marker.

\n", - "parameters": [], - "since": "", - "returns": "number", - "examples": "
ts
faker.methodWithExample(): number\ntest.apidoc.methodWithExample() // 0\n
\n
", - "deprecated": false, - "seeAlsos": [] - }, - "methodWithMultipleSeeMarkers": { - "name": "methodWithMultipleSeeMarkers", - "title": "Method With Multiple See Markers", - "description": "

Test with multiple see markers.

\n", - "parameters": [], - "since": "", - "returns": "number", - "examples": "
ts
faker.methodWithMultipleSeeMarkers(): number\n
\n
", - "deprecated": false, - "seeAlsos": [ - "test.apidoc.methodWithExample()", - "test.apidoc.methodWithDeprecated()" - ] - }, - "methodWithSinceMarker": { - "name": "methodWithSinceMarker", - "title": "Method With Since Marker", - "description": "

Test with since marker.

\n", - "parameters": [], - "since": "1.0.0", - "returns": "number", - "examples": "
ts
faker.methodWithSinceMarker(): number\n
\n
", - "deprecated": false, - "seeAlsos": [] - }, - "multiParamMethod": { - "name": "multiParamMethod", - "title": "Multi Param Method", - "description": "

Test with multiple parameters.

\n", - "parameters": [ - { - "name": "a", - "type": "number", - "description": "

The number parameter.

\n" - }, - { - "name": "b?", - "type": "string", - "description": "

The string parameter.

\n" - }, - { - "name": "c", - "type": "boolean", - "default": "true", - "description": "

The boolean parameter.

\n" - } - ], - "since": "", - "returns": "number", - "examples": "
ts
faker.multiParamMethod(a: number, b?: string, c: boolean = true): number\n
\n
", - "deprecated": false, - "seeAlsos": [] - }, - "noParamMethod": { - "name": "noParamMethod", - "title": "No Param Method", - "description": "

Test with no parameters.

\n", - "parameters": [], - "since": "", - "returns": "number", - "examples": "
ts
faker.noParamMethod(): number\n
\n
", - "deprecated": false, - "seeAlsos": [] - }, - "optionalStringParamMethod": { - "name": "optionalStringParamMethod", - "title": "Optional String Param Method", - "description": "

Test with an optional parameter.

\n", - "parameters": [ - { - "name": "b?", - "type": "string", - "description": "

The string parameter.

\n" - } - ], - "since": "", - "returns": "number", - "examples": "
ts
faker.optionalStringParamMethod(b?: string): number\n
\n
", - "deprecated": false, - "seeAlsos": [] - }, - "optionsInlineParamMethodWithDefaults": { - "name": "optionsInlineParamMethodWithDefaults", - "title": "Options Inline Param Method With Defaults", - "description": "

Test with a function parameters (inline types) with defaults.

\n", - "parameters": [ - { - "name": "a", - "type": "{ ... }", - "default": "{ value: 1 }", - "description": "

Parameter with signature default.\nIt also has a more complex description.

\n" - }, - { - "name": "a.value?", - "type": "number", - "description": "

The number parameter.

\n" - }, - { - "name": "b", - "type": "{ ... }", - "default": "{ value: 1 }", - "description": "

Parameter with jsdocs default.

\n

It also has a more complex description.

\n" - }, - { - "name": "b.value?", - "type": "number", - "description": "

The number parameter.

\n" - }, - { - "name": "c", - "type": "{ ... }", - "description": "

Parameter with inner jsdocs default.

\n" - }, - { - "name": "c.value?", - "type": "number", - "default": "2", - "description": "

The number parameter. It also has a more complex description.

\n" - } - ], - "since": "", - "returns": "number", - "examples": "
ts
faker.optionsInlineParamMethodWithDefaults(a: {\n  value: number\n} = { value: 1 }, b: {\n  value: number\n} = { value: 1 }, c: {\n  value: number\n}): number\n
\n
", - "deprecated": false, - "seeAlsos": [] - }, - "optionsInterfaceParamMethodWithDefaults": { - "name": "optionsInterfaceParamMethodWithDefaults", - "title": "Options Interface Param Method With Defaults", - "description": "

Test with a function parameters with defaults.

\n", - "parameters": [ - { - "name": "a", - "type": "ParameterOptionsInterfaceA", - "default": "{ value: 1 }", - "description": "

Parameter with signature default.

\n" - }, - { - "name": "b", - "type": "ParameterOptionsInterfaceB", - "default": "{ value: 1 }", - "description": "

Parameter with jsdocs default.

\n" - }, - { - "name": "c", - "type": "ParameterOptionsInterfaceC", - "description": "

Parameter with inner jsdocs default.

\n" - } - ], - "since": "", - "returns": "number", - "examples": "
ts
faker.optionsInterfaceParamMethodWithDefaults(a: ParameterOptionsInterfaceA = { value: 1 }, b: ParameterOptionsInterfaceB = { value: 1 }, c: ParameterOptionsInterfaceC): number\n
\n
", - "deprecated": false, - "seeAlsos": [] - }, - "optionsParamMethod": { - "name": "optionsParamMethod", - "title": "Options Param Method", - "description": "

Test with a function parameters.

\n", - "parameters": [ - { - "name": "options", - "type": "{ ... }", - "description": "

The function parameter.

\n" - }, - { - "name": "options.a", - "type": "number", - "description": "

The number parameter.

\n" - }, - { - "name": "options.b?", - "type": "string", - "description": "

The string parameter.

\n" - }, - { - "name": "options.c", - "type": "boolean", - "description": "

The boolean parameter.

\n" - }, - { - "name": "options.d", - "type": "() => string", - "description": "

The method parameter.

\n" - } - ], - "since": "", - "returns": "number", - "examples": "
ts
faker.optionsParamMethod(options: {\n  a: number,\n  b: string,\n  c: boolean,\n  d: () => string\n}): number\n
\n
", - "deprecated": false, - "seeAlsos": [] - }, - "optionsTypeParamMethodWithDefaults": { - "name": "optionsTypeParamMethodWithDefaults", - "title": "Options Type Param Method With Defaults", - "description": "

Test with a function parameters with defaults.

\n", - "parameters": [ - { - "name": "a", - "type": "ParameterOptionsTypeA", - "default": "{ value: 1 }", - "description": "

Parameter with signature default.

\n" - }, - { - "name": "b", - "type": "ParameterOptionsTypeB", - "default": "{ value: 1 }", - "description": "

Parameter with jsdocs default.

\n" - }, - { - "name": "c", - "type": "ParameterOptionsTypeC", - "description": "

Parameter with inner jsdocs default.

\n" - } - ], - "since": "", - "returns": "number", - "examples": "
ts
faker.optionsTypeParamMethodWithDefaults(a: ParameterOptionsTypeA = { value: 1 }, b: ParameterOptionsTypeB = { value: 1 }, c: ParameterOptionsTypeC): number\n
\n
", - "deprecated": false, - "seeAlsos": [] - }, - "requiredNumberParamMethod": { - "name": "requiredNumberParamMethod", - "title": "Required Number Param Method", - "description": "

Test with a required parameter.

\n", - "parameters": [ - { - "name": "a", - "type": "number", - "description": "

The number parameter.

\n" - } - ], - "since": "", - "returns": "number", - "examples": "
ts
faker.requiredNumberParamMethod(a: number): number\n
\n
", - "deprecated": false, - "seeAlsos": [] - }, - "stringUnionParamMethod": { - "name": "stringUnionParamMethod", - "title": "String Union Param Method", - "description": "

Test with string union.

\n", - "parameters": [ - { - "name": "value", - "type": "'a' | 'b'", - "description": "

'a' or 'b'.

\n" - } - ], - "since": "", - "returns": "string", - "examples": "
ts
faker.stringUnionParamMethod(value: 'a' | 'b'): string\n
\n
", - "deprecated": false, - "seeAlsos": [] - } -} diff --git a/test/scripts/apidoc/signature.spec.ts b/test/scripts/apidoc/signature.spec.ts index f55c10f8..43ab00e7 100644 --- a/test/scripts/apidoc/signature.spec.ts +++ b/test/scripts/apidoc/signature.spec.ts @@ -1,24 +1,13 @@ -import { writeFileSync } from 'fs'; -import { resolve } from 'path'; -import { afterAll, beforeAll, describe, expect, it } from 'vitest'; -import type { Method } from '../../../docs/.vitepress/components/api-docs/method'; +import { beforeAll, describe, expect, it } from 'vitest'; import { analyzeSignature, initMarkdownRenderer, } from '../../../scripts/apidoc/signature'; import { SignatureTest } from './signature.example'; -import expected_ from './signature.expected.json'; import { loadExampleMethods } from './utils'; -const expected: Record = expected_; - -function prettyJson(object): string { - return JSON.stringify(object, null, 2); -} - describe('signature', () => { describe('analyzeSignature()', () => { - const actuals = {}; const methods = loadExampleMethods(); beforeAll(async () => { @@ -30,24 +19,15 @@ describe('signature', () => { }); it('expected and actual methods are equal', () => { - expect(Object.keys(methods).sort()).toEqual(Object.keys(expected).sort()); + expect(Object.keys(methods).sort()).toMatchSnapshot(); }); - it.each(Object.keys(expected).sort())('%s', (name) => { + it.each(Object.keys(methods).sort())('%s', (name) => { const method = methods[name]; expect(method, `Method ${name} to be defined`).toBeDefined(); const actual = analyzeSignature(method.signatures[0], null, method.name); - actuals[name] = actual; - - expect(prettyJson(actual)).toBe(prettyJson(expected[name])); - }); - afterAll(() => { - // Write to file for easier comparison - writeFileSync( - resolve('test', 'scripts', 'apidoc', 'signature.actuals.json'), - prettyJson(actuals) - ); + expect(actual).toMatchSnapshot(); }); }); }); -- cgit v1.2.3