From 7cb19b0f341765adb0af374fdd9412c11ba63fa1 Mon Sep 17 00:00:00 2001 From: Caleb Cox Date: Wed, 30 Oct 2024 16:48:13 -0500 Subject: [PATCH] Make enum mocking deterministic --- src/__tests__/lib.test.ts | 9 ++++++++- src/mock.ts | 5 ++--- src/utils/getRandomElement.ts | 4 ---- src/utils/random.ts | 2 ++ 4 files changed, 12 insertions(+), 8 deletions(-) delete mode 100644 src/utils/getRandomElement.ts diff --git a/src/__tests__/lib.test.ts b/src/__tests__/lib.test.ts index fb0d525..bcb51bf 100644 --- a/src/__tests__/lib.test.ts +++ b/src/__tests__/lib.test.ts @@ -3,6 +3,8 @@ import { GraphQLError } from "graphql"; import schema from "./schema"; import schemaAST from "./schema-ast.json"; +const seed = 'seed'; + describe("Automocking", () => { describe("Guardrails", () => { test("it throws without a valid schema", () => { @@ -89,10 +91,15 @@ describe("Automocking", () => { returnEnum } `; - const resp: any = ergonomock(schema, testQuery); + const resp: any = ergonomock(schema, testQuery, { seed }); expect(resp.data).toMatchObject({ returnEnum: expect.toBeOneOf(["A", "B", "C"]), }); + + for (let i = 0; i < 10; ++i) { + const resp2 = ergonomock(schema, testQuery, { seed }); + expect(resp).toEqual(resp2); + } }); test("can automock unions", () => { diff --git a/src/mock.ts b/src/mock.ts index 2eb9913..6819710 100644 --- a/src/mock.ts +++ b/src/mock.ts @@ -19,7 +19,6 @@ import { buildASTSchema, } from "graphql"; import random from "./utils/random"; -import getRandomElement from "./utils/getRandomElement"; import forEachFieldInQuery from "./utils/forEachFieldInQuery"; const defaultMockMap: Map> = new Map(); @@ -117,7 +116,7 @@ export function ergonomock( if (fieldType instanceof GraphQLUnionType || fieldType instanceof GraphQLInterfaceType) { let implementationType; const possibleTypes = schema.getPossibleTypes(fieldType); - implementationType = getRandomElement(possibleTypes); + implementationType = random.item(possibleTypes); return Object.assign( { __typename: implementationType }, mockResolverFunction(implementationType)(root, args, context, info) @@ -130,7 +129,7 @@ export function ergonomock( // Default mock for enums if (fieldType instanceof GraphQLEnumType) { - return getRandomElement(fieldType.getValues()).value; + return random.item(fieldType.getValues()).value; } // Automock object types diff --git a/src/utils/getRandomElement.ts b/src/utils/getRandomElement.ts deleted file mode 100644 index 2252701..0000000 --- a/src/utils/getRandomElement.ts +++ /dev/null @@ -1,4 +0,0 @@ -export default function getRandomElement(ary: ReadonlyArray) { - const sample = Math.floor(Math.random() * ary.length); - return ary[sample]; -} diff --git a/src/utils/random.ts b/src/utils/random.ts index b3139a5..4b84f41 100644 --- a/src/utils/random.ts +++ b/src/utils/random.ts @@ -252,6 +252,8 @@ const random = { // max is exclusive, min is inclusive float: (max: number = 100, min: number = 1) => rng() * (max - min) + min, + item: (array: readonly T[]): T => array[random.integer(array.length, 0)], + // max and min are inclusive list: (maxLength: number = 4, minLength: number = 1) => [ ...Array(random.integer(maxLength + 1, minLength))