Skip to content

Commit

Permalink
test(codegen): migrate codegen tests to vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
binoy14 committed Nov 4, 2024
1 parent 51db20a commit ace764f
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 89 deletions.
6 changes: 0 additions & 6 deletions packages/@sanity/codegen/jest.config.mjs

This file was deleted.

6 changes: 3 additions & 3 deletions packages/@sanity/codegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"clean": "rimraf lib coverage",
"lint": "eslint .",
"prepublishOnly": "turbo run build",
"test": "jest",
"test": "vitest",
"watch": "pkg-utils watch"
},
"dependencies": {
Expand All @@ -67,15 +67,15 @@
"zod": "^3.22.4"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@repo/package.config": "workspace:*",
"@repo/test-config": "workspace:*",
"@types/babel__core": "^7.20.5",
"@types/babel__generator": "^7.6.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"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {describe, expect, test} from '@jest/globals'
import {describe, expect, test} from 'vitest'

import {extractSliceParams, safeParseQuery} from '../safeParseQuery'

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -29,40 +29,40 @@ exports[`generateSchemaTypes should generate TypeScript type declarations for a
export type Post = {
_id: string;
_type: \\"post\\";
_type: "post";
_createdAt: string;
_updatedAt: string;
_rev: string;
title?: string;
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;
Expand All @@ -74,7 +74,7 @@ export type Post = {
export type Ghost = {
_id: string;
_type: \\"ghost\\";
_type: "ghost";
_createdAt: string;
_updatedAt: string;
_rev: string;
Expand All @@ -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<string | \\"strong\\" | \\"em\\">;
marks: Array<string | "strong" | "em">;
}>;
markDefs: Array<{
href?: string;
Expand All @@ -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;
Expand All @@ -142,7 +142,7 @@ export type SanityImageAsset = {
export type SanityFileAsset = {
_id: string;
_type: \\"sanity.fileAsset\\";
_type: "sanity.fileAsset";
_createdAt: string;
_updatedAt: string;
_rev: string;
Expand All @@ -167,15 +167,15 @@ export type SanityImageCrop = {
bottom?: number;
left?: number;
right?: number;
_type: \\"sanity.imageCrop\\";
_type: "sanity.imageCrop";
};
export type SanityImageHotspot = {
x?: number;
y?: number;
height?: number;
width?: number;
_type: \\"sanity.imageHotspot\\";
_type: "sanity.imageHotspot";
};
export type SanityImageMetadata = {
Expand All @@ -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 = {
Expand All @@ -204,21 +204,21 @@ export type SanityImagePalette = {
dominant?: SanityImagePaletteSwatch;
lightMuted?: SanityImagePaletteSwatch;
muted?: SanityImagePaletteSwatch;
_type: \\"sanity.imagePalette\\";
_type: "sanity.imagePalette";
};
export type SanityImagePaletteSwatch = {
background?: string;
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;
Expand All @@ -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;"`;
Original file line number Diff line number Diff line change
@@ -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'

Expand Down
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import path from 'node:path'

import {describe, expect, test} from '@jest/globals'
import {
createReferenceTypeNode,
type DocumentSchemaType,
Expand All @@ -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'
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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;
}
}"
`)
})
})
Loading

0 comments on commit ace764f

Please sign in to comment.