From cffcc9a523c8917e5f8db564aa3f8111e42d0633 Mon Sep 17 00:00:00 2001 From: Shodai Suzuki Date: Sat, 4 May 2024 21:03:38 +0900 Subject: [PATCH] chore: add `mock` tests (#1355) * chore: add `mock` tests * fix: import variable type in custom mock builder test --- tests/configs/mock.config.ts | 107 +++++++++++++++++++++++++++++++++++ tests/package.json | 1 + 2 files changed, 108 insertions(+) create mode 100644 tests/configs/mock.config.ts diff --git a/tests/configs/mock.config.ts b/tests/configs/mock.config.ts new file mode 100644 index 000000000..0a2786c2c --- /dev/null +++ b/tests/configs/mock.config.ts @@ -0,0 +1,107 @@ +import { faker } from '@faker-js/faker'; +import { defineConfig } from 'orval'; + +export default defineConfig({ + petstore: { + output: { + target: '../generated/mock/petstore/endpoints.ts', + schemas: '../generated/mock/petstore/model', + client: 'axios', + mock: true, + override: { + mock: { + properties: { + name: 'jon', + }, + format: { + email: () => faker.internet.email(), + }, + required: true, + delay: 500, + arrayMin: 3, + arrayMax: 5, + useExamples: true, + baseUrl: 'https://petstore.swagger.io/v1', + }, + }, + }, + input: { + target: '../specifications/petstore.yaml', + }, + }, + petstoreEachHttpStatus: { + output: { + target: '../generated/mock/petstore-each-http-status/endpoints.ts', + schemas: '../generated/mock/petstore-each-http-status/model', + client: 'axios', + mock: true, + override: { + mock: { + generateEachHttpStatus: true, + }, + }, + }, + input: { + target: '../specifications/default-status.yaml', + }, + }, + petstoreCustomMockBuilder: { + output: { + target: '../generated/mock/petstore-custom-mock-builder/endpoints.ts', + schemas: '../generated/mock/petstore-custom-mock-builder/model', + client: 'axios', + mock: (verbOptions, _) => { + const imports = []; + const handlerName = `${verbOptions.operationId}MockHandler`; + + return { + imports: imports, + implementation: { + function: '', + handlerName: handlerName, + handler: `const ${handlerName} = () => { return { data: { id: 1, name: "myName" } } }\n`, + }, + }; + }, + }, + input: { + target: '../specifications/petstore.yaml', + }, + }, + petstoreTagsSplit: { + output: { + target: '../generated/mock/petstore-tags-split/endpoints.ts', + schemas: '../generated/mock/petstore-tags-split/model', + mock: true, + mode: 'tags-split', + client: 'axios', + }, + input: { + target: '../specifications/petstore.yaml', + }, + }, + petstoreSplit: { + output: { + target: '../generated/mock/split/endpoints.ts', + schemas: '../generated/mock/split/model', + mock: true, + mode: 'split', + client: 'axios', + }, + input: { + target: '../specifications/petstore.yaml', + }, + }, + petstoreTags: { + output: { + target: '../generated/mock/tags/endpoints.ts', + schemas: '../generated/mock/tags/model', + mock: true, + mode: 'tags', + client: 'axios', + }, + input: { + target: '../specifications/petstore.yaml', + }, + }, +}); diff --git a/tests/package.json b/tests/package.json index 534a28e60..455b9508f 100644 --- a/tests/package.json +++ b/tests/package.json @@ -16,6 +16,7 @@ "generate:swr": "yarn orval --config ./configs/swr.config.ts", "generate:multi-file": "yarn orval --config ./configs/multi-file.config.ts", "generate:zod": "yarn orval --config ./configs/zod.config.ts", + "generate:mock": "yarn orval --config ./configs/mock.config.ts", "build": "tsc" }, "author": "Victor Bury",