From ace764fe10fc3c83c2aed1856d80b6c02cad2753 Mon Sep 17 00:00:00 2001 From: Binoy Patel Date: Mon, 4 Nov 2024 11:47:59 -0500 Subject: [PATCH] test(codegen): migrate codegen tests to vitest --- packages/@sanity/codegen/jest.config.mjs | 6 -- packages/@sanity/codegen/package.json | 6 +- .../src/__tests__/safeParseQuery.test.ts | 2 +- .../__snapshots__/typeGenerator.test.ts.snap | 70 ++++++++--------- .../__tests__/findQueriesInPath.test.ts | 2 +- .../__tests__/findQueriesInSource.test.ts | 10 ++- .../__tests__/typeGenerator.test.ts | 78 +++++++++---------- packages/@sanity/codegen/vitest.config.mts | 7 ++ pnpm-lock.yaml | 6 +- vitest.workspace.ts | 1 + 10 files changed, 99 insertions(+), 89 deletions(-) delete mode 100644 packages/@sanity/codegen/jest.config.mjs create mode 100644 packages/@sanity/codegen/vitest.config.mts diff --git a/packages/@sanity/codegen/jest.config.mjs b/packages/@sanity/codegen/jest.config.mjs deleted file mode 100644 index fc29f9a54e1..00000000000 --- a/packages/@sanity/codegen/jest.config.mjs +++ /dev/null @@ -1,6 +0,0 @@ -import {createJestConfig, readPackageName} from '@repo/test-config/jest' - -export default createJestConfig({ - displayName: readPackageName(import.meta.url), - testEnvironment: 'node', -}) diff --git a/packages/@sanity/codegen/package.json b/packages/@sanity/codegen/package.json index 61323638e28..f892412a2ae 100644 --- a/packages/@sanity/codegen/package.json +++ b/packages/@sanity/codegen/package.json @@ -46,7 +46,7 @@ "clean": "rimraf lib coverage", "lint": "eslint .", "prepublishOnly": "turbo run build", - "test": "jest", + "test": "vitest", "watch": "pkg-utils watch" }, "dependencies": { @@ -67,7 +67,6 @@ "zod": "^3.22.4" }, "devDependencies": { - "@jest/globals": "^29.7.0", "@repo/package.config": "workspace:*", "@repo/test-config": "workspace:*", "@types/babel__core": "^7.20.5", @@ -75,7 +74,8 @@ "@types/babel__register": "^7.17.3", "@types/babel__traverse": "^7.20.5", "@types/debug": "^4.1.12", - "rimraf": "^3.0.2" + "rimraf": "^3.0.2", + "vitest": "^2.1.1" }, "engines": { "node": ">=18" diff --git a/packages/@sanity/codegen/src/__tests__/safeParseQuery.test.ts b/packages/@sanity/codegen/src/__tests__/safeParseQuery.test.ts index ab342182909..1861b1320fb 100644 --- a/packages/@sanity/codegen/src/__tests__/safeParseQuery.test.ts +++ b/packages/@sanity/codegen/src/__tests__/safeParseQuery.test.ts @@ -1,4 +1,4 @@ -import {describe, expect, test} from '@jest/globals' +import {describe, expect, test} from 'vitest' import {extractSliceParams, safeParseQuery} from '../safeParseQuery' diff --git a/packages/@sanity/codegen/src/typescript/__tests__/__snapshots__/typeGenerator.test.ts.snap b/packages/@sanity/codegen/src/typescript/__tests__/__snapshots__/typeGenerator.test.ts.snap index 6330d1ba6fe..012a840806f 100644 --- a/packages/@sanity/codegen/src/typescript/__tests__/__snapshots__/typeGenerator.test.ts.snap +++ b/packages/@sanity/codegen/src/typescript/__tests__/__snapshots__/typeGenerator.test.ts.snap @@ -1,26 +1,26 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`generateSchemaTypes Adds a comment when missing referenced inline type 1`] = ` -"export type MyObject = unknown // Unable to locate the referenced type \\"test\\" in schema +exports[`generateSchemaTypes > Adds a comment when missing referenced inline type 1`] = ` +"export type MyObject = unknown // Unable to locate the referenced type "test" in schema ;" `; -exports[`generateSchemaTypes can generate well known types 1`] = `"export declare const internalGroqTypeReferenceTo: unique symbol;"`; +exports[`generateSchemaTypes > can generate well known types 1`] = `"export declare const internalGroqTypeReferenceTo: unique symbol;"`; -exports[`generateSchemaTypes generateTypeNodeTypes should be able to generate types for type nodes: boolean 1`] = `"export type Test_2 = boolean;"`; +exports[`generateSchemaTypes > generateTypeNodeTypes > should be able to generate types for type nodes: boolean 1`] = `"export type Test_2 = boolean;"`; -exports[`generateSchemaTypes generateTypeNodeTypes should be able to generate types for type nodes: null 1`] = `"export type Test_5 = null;"`; +exports[`generateSchemaTypes > generateTypeNodeTypes > should be able to generate types for type nodes: null 1`] = `"export type Test_5 = null;"`; -exports[`generateSchemaTypes generateTypeNodeTypes should be able to generate types for type nodes: number 1`] = `"export type Test_3 = number;"`; +exports[`generateSchemaTypes > generateTypeNodeTypes > should be able to generate types for type nodes: number 1`] = `"export type Test_3 = number;"`; -exports[`generateSchemaTypes generateTypeNodeTypes should be able to generate types for type nodes: string 1`] = `"export type Test = string;"`; +exports[`generateSchemaTypes > generateTypeNodeTypes > should be able to generate types for type nodes: string 1`] = `"export type Test = string;"`; -exports[`generateSchemaTypes generateTypeNodeTypes should be able to generate types for type nodes: unknown 1`] = `"export type Test_4 = unknown;"`; +exports[`generateSchemaTypes > generateTypeNodeTypes > should be able to generate types for type nodes: unknown 1`] = `"export type Test_4 = unknown;"`; -exports[`generateSchemaTypes should generate TypeScript type declarations for a schema 1`] = ` +exports[`generateSchemaTypes > should generate TypeScript type declarations for a schema 1`] = ` "export type Author = { _id: string; - _type: \\"author\\"; + _type: "author"; _createdAt: string; _updatedAt: string; _rev: string; @@ -29,7 +29,7 @@ exports[`generateSchemaTypes should generate TypeScript type declarations for a export type Post = { _id: string; - _type: \\"post\\"; + _type: "post"; _createdAt: string; _updatedAt: string; _rev: string; @@ -37,32 +37,32 @@ export type Post = { author?: { _ref: string; _weak?: boolean; - [internalGroqTypeReferenceTo]?: \\"author\\"; + [internalGroqTypeReferenceTo]?: "author"; } | { _ref: string; _weak?: boolean; - [internalGroqTypeReferenceTo]?: \\"ghost\\"; + [internalGroqTypeReferenceTo]?: "ghost"; }; slug?: Slug; excerpt?: string; mainImage?: { - _type: \\"image\\"; + _type: "image"; asset: { _ref: string; _weak?: boolean; - [internalGroqTypeReferenceTo]?: \\"sanity.imageAsset\\"; + [internalGroqTypeReferenceTo]?: "sanity.imageAsset"; }; caption?: string; attribution?: string; hotspot?: { - _type: \\"sanity.imageHotspot\\"; + _type: "sanity.imageHotspot"; x: number; y: number; height: number; width: number; }; crop?: { - _type: \\"sanity.imageCrop\\"; + _type: "sanity.imageCrop"; top: number; bottom: number; left: number; @@ -74,7 +74,7 @@ export type Post = { export type Ghost = { _id: string; - _type: \\"ghost\\"; + _type: "ghost"; _createdAt: string; _updatedAt: string; _rev: string; @@ -84,12 +84,12 @@ export type Ghost = { export type BlockContent = Array<{ _key: string; level?: number; - style?: \\"normal\\" | \\"h1\\" | \\"h2\\" | \\"h3\\" | \\"h4\\" | \\"blockquote\\"; - listItem?: \\"bullet\\"; + style?: "normal" | "h1" | "h2" | "h3" | "h4" | "blockquote"; + listItem?: "bullet"; children: Array<{ _key: string; text: string; - marks: Array; + marks: Array; }>; markDefs: Array<{ href?: string; @@ -101,25 +101,25 @@ export type SanityAssetSourceData = { name?: string; id?: string; url?: string; - _type: \\"sanity.assetSourceData\\"; + _type: "sanity.assetSourceData"; }; export type Slug = { current?: string; source?: string; - _type: \\"slug\\"; + _type: "slug"; }; export type Geopoint = { lat?: number; lng?: number; alt?: number; - _type: \\"geopoint\\"; + _type: "geopoint"; }; export type SanityImageAsset = { _id: string; - _type: \\"sanity.imageAsset\\"; + _type: "sanity.imageAsset"; _createdAt: string; _updatedAt: string; _rev: string; @@ -142,7 +142,7 @@ export type SanityImageAsset = { export type SanityFileAsset = { _id: string; - _type: \\"sanity.fileAsset\\"; + _type: "sanity.fileAsset"; _createdAt: string; _updatedAt: string; _rev: string; @@ -167,7 +167,7 @@ export type SanityImageCrop = { bottom?: number; left?: number; right?: number; - _type: \\"sanity.imageCrop\\"; + _type: "sanity.imageCrop"; }; export type SanityImageHotspot = { @@ -175,7 +175,7 @@ export type SanityImageHotspot = { y?: number; height?: number; width?: number; - _type: \\"sanity.imageHotspot\\"; + _type: "sanity.imageHotspot"; }; export type SanityImageMetadata = { @@ -186,14 +186,14 @@ export type SanityImageMetadata = { blurHash?: string; hasAlpha?: boolean; isOpaque?: boolean; - _type: \\"sanity.imageMetadata\\"; + _type: "sanity.imageMetadata"; }; export type SanityImageDimensions = { height?: number; width?: number; aspectRatio?: number; - _type: \\"sanity.imageDimensions\\"; + _type: "sanity.imageDimensions"; }; export type SanityImagePalette = { @@ -204,7 +204,7 @@ export type SanityImagePalette = { dominant?: SanityImagePaletteSwatch; lightMuted?: SanityImagePaletteSwatch; muted?: SanityImagePaletteSwatch; - _type: \\"sanity.imagePalette\\"; + _type: "sanity.imagePalette"; }; export type SanityImagePaletteSwatch = { @@ -212,13 +212,13 @@ export type SanityImagePaletteSwatch = { foreground?: string; population?: number; title?: string; - _type: \\"sanity.imagePaletteSwatch\\"; + _type: "sanity.imagePaletteSwatch"; }; export type AllSanitySchemaTypes = Author | Post | Ghost | BlockContent | SanityAssetSourceData | Slug | Geopoint | SanityImageAsset | SanityFileAsset | SanityImageCrop | SanityImageHotspot | SanityImageMetadata | SanityImageDimensions | SanityImagePalette | SanityImagePaletteSwatch;" `; -exports[`generateSchemaTypes should generate correct types for document schema with inline fields 1`] = ` +exports[`generateSchemaTypes > should generate correct types for document schema with inline fields 1`] = ` "export type MyObject = { inlineField: { test: string; @@ -231,4 +231,4 @@ exports[`generateSchemaTypes should generate correct types for document schema w };" `; -exports[`generateSchemaTypes should generate correct types for document schema with inline fields 2`] = `"export type SomeOtherType = MyObject;"`; +exports[`generateSchemaTypes > should generate correct types for document schema with inline fields 2`] = `"export type SomeOtherType = MyObject;"`; diff --git a/packages/@sanity/codegen/src/typescript/__tests__/findQueriesInPath.test.ts b/packages/@sanity/codegen/src/typescript/__tests__/findQueriesInPath.test.ts index 3cf3d4512ef..1d5d4e6a14a 100644 --- a/packages/@sanity/codegen/src/typescript/__tests__/findQueriesInPath.test.ts +++ b/packages/@sanity/codegen/src/typescript/__tests__/findQueriesInPath.test.ts @@ -1,7 +1,7 @@ import assert from 'node:assert' import path from 'node:path' -import {describe, expect, test} from '@jest/globals' +import {describe, expect, test} from 'vitest' import {findQueriesInPath} from '../findQueriesInPath' diff --git a/packages/@sanity/codegen/src/typescript/__tests__/findQueriesInSource.test.ts b/packages/@sanity/codegen/src/typescript/__tests__/findQueriesInSource.test.ts index bd77c2f3047..d69fa668899 100644 --- a/packages/@sanity/codegen/src/typescript/__tests__/findQueriesInSource.test.ts +++ b/packages/@sanity/codegen/src/typescript/__tests__/findQueriesInSource.test.ts @@ -1,7 +1,15 @@ -import {describe, expect, test} from '@jest/globals' +import {describe, expect, test, vi} from 'vitest' import {findQueriesInSource} from '../findQueriesInSource' +// Mock require since it's not supported in vitest +vi.mock('node:module', () => ({ + createRequire: vi.fn().mockReturnValue({ + // Add the extension to the path + resolve: vi.fn((path) => `${path}.ts`), + }), +})) + describe('findQueries with the groq template', () => { describe('should find queries in source', () => { test('plain string', () => { diff --git a/packages/@sanity/codegen/src/typescript/__tests__/typeGenerator.test.ts b/packages/@sanity/codegen/src/typescript/__tests__/typeGenerator.test.ts index 6f312be88a9..fc9091b869c 100644 --- a/packages/@sanity/codegen/src/typescript/__tests__/typeGenerator.test.ts +++ b/packages/@sanity/codegen/src/typescript/__tests__/typeGenerator.test.ts @@ -1,6 +1,5 @@ import path from 'node:path' -import {describe, expect, test} from '@jest/globals' import { createReferenceTypeNode, type DocumentSchemaType, @@ -10,6 +9,7 @@ import { type StringTypeNode, type TypeNode, } from 'groq-js' +import {describe, expect, test} from 'vitest' import {readSchema} from '../../readSchema' import {TypeGenerator} from '../typeGenerator' @@ -262,21 +262,21 @@ export type AllSanitySchemaTypes = Impossible;" const actualOutput = typeGenerator.generateSchemaTypes() expect(actualOutput).toMatchInlineSnapshot(` -"export type BlogPost = { - author: { - _ref: string; - _type: \\"reference\\"; - _weak?: boolean; - [internalGroqTypeReferenceTo]?: \\"author\\"; - }; -}; - -export type Author = { - name: string; -}; - -export type AllSanitySchemaTypes = BlogPost | Author;" -`) + "export type BlogPost = { + author: { + _ref: string; + _type: "reference"; + _weak?: boolean; + [internalGroqTypeReferenceTo]?: "author"; + }; + }; + + export type Author = { + name: string; + }; + + export type AllSanitySchemaTypes = BlogPost | Author;" + `) }) test('should generate correct types for document schema with union fields', () => { @@ -478,14 +478,14 @@ describe('generateQueryMap', () => { const actualOutput = typeGenerator.generateQueryMap(queries) expect(actualOutput).toMatchInlineSnapshot(` -"import \\"@sanity/client\\"; -declare module \\"@sanity/client\\" { - interface SanityQueries { - \\"*[_type == \\\\\\"author\\\\\\"]\\": AuthorsResult; - \\"*[_type == \\\\\\"author\\\\\\"][0]\\": FirstAuthorResult; - } -}" -`) + "import "@sanity/client"; + declare module "@sanity/client" { + interface SanityQueries { + "*[_type == \\"author\\"]": AuthorsResult; + "*[_type == \\"author\\"][0]": FirstAuthorResult; + } + }" + `) }) test('should generate a map of query results with duplicate type names', () => { @@ -509,14 +509,14 @@ declare module \\"@sanity/client\\" { const actualOutput = typeGenerator.generateQueryMap(queries) expect(actualOutput).toMatchInlineSnapshot(` -"import \\"@sanity/client\\"; -declare module \\"@sanity/client\\" { - interface SanityQueries { - \\"*[_type == \\\\\\"foo\\\\\\"]\\": Foo; - \\"*[_type == \\\\\\"bar\\\\\\"]\\": Foo_2; - } -}" -`) + "import "@sanity/client"; + declare module "@sanity/client" { + interface SanityQueries { + "*[_type == \\"foo\\"]": Foo; + "*[_type == \\"bar\\"]": Foo_2; + } + }" + `) }) test('should generate a map of query results with duplicate query strings', () => { @@ -540,12 +540,12 @@ declare module \\"@sanity/client\\" { const actualOutput = typeGenerator.generateQueryMap(queries) expect(actualOutput).toMatchInlineSnapshot(` -"import \\"@sanity/client\\"; -declare module \\"@sanity/client\\" { - interface SanityQueries { - \\"*[_type == \\\\\\"foo\\\\\\"]\\": Foo | Bar; - } -}" -`) + "import "@sanity/client"; + declare module "@sanity/client" { + interface SanityQueries { + "*[_type == \\"foo\\"]": Foo | Bar; + } + }" + `) }) }) diff --git a/packages/@sanity/codegen/vitest.config.mts b/packages/@sanity/codegen/vitest.config.mts new file mode 100644 index 00000000000..ed51d314e07 --- /dev/null +++ b/packages/@sanity/codegen/vitest.config.mts @@ -0,0 +1,7 @@ +import {defineConfig} from '@repo/test-config/vitest' + +export default defineConfig({ + test: { + includeSource: ['./src/**/*.ts'], + }, +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 43bb406ab75..e1f08da3043 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1054,9 +1054,6 @@ importers: specifier: ^3.22.4 version: 3.23.8 devDependencies: - '@jest/globals': - specifier: ^29.7.0 - version: 29.7.0 '@repo/package.config': specifier: workspace:* version: link:../../@repo/package.config @@ -1081,6 +1078,9 @@ importers: rimraf: specifier: ^3.0.2 version: 3.0.2 + vitest: + specifier: ^2.1.1 + version: 2.1.1(@types/node@22.5.4)(jsdom@23.2.0)(terser@5.32.0) packages/@sanity/diff: dependencies: diff --git a/vitest.workspace.ts b/vitest.workspace.ts index 1ee39f741ce..8d6cf95ba9d 100644 --- a/vitest.workspace.ts +++ b/vitest.workspace.ts @@ -4,6 +4,7 @@ export default defineWorkspace([ 'packages/@sanity/migrate', 'packages/@sanity/block-tools', 'packages/@sanity/cli', + 'packages/@sanity/codegen', 'packages/@sanity/mutator', 'packages/@sanity/schema', 'packages/@sanity/types',