import createReadGeneratedFile , { ReadGeneratedFile , } from "../helpers/read-file" ; import { promises as fs } from "fs" ; import { generateCodeFromSchema } from "../helpers/generate-code" ; import generateArtifactsDirPath from "../helpers/artifacts-dir" ; import { getDirectoryStructureString } from "../helpers/structure" ; describe ( "emitOnly generator option" , ( ) => { let outputDirPath ; let readGeneratedFile ; let schema ; beforeEach ( async ( ) => { outputDirPath = generateArtifactsDirPath ( "emit-only-regression" ) ; await fs . mkdir ( outputDirPath , { recursive : true } ) ; readGeneratedFile = createReadGeneratedFile ( outputDirPath ) ; schema = `
      model User {
        id          Int     @id @default(autoincrement())
        email       String  @unique
        posts       Post[]  @relation("posts")
      }

      model Post {
        uuid      String    @id @default(uuid())
        createdAt DateTime  @default(now())
        updatedAt DateTime  @updatedAt
        published Boolean
        title     String
        content   String?
        author    User      @relation(fields: [authorId], references: [id], name: "posts")
        authorId  Int
        metadata  Json
      }
    ` ; } ) ; describe ( "when only 'enums' is set" , ( ) => { it ( "should generate proper files, enhance and index" , async ( ) => { await generateCodeFromSchema ( schema , { emitOnly : [ "enums" ] , outputDirPath , } ) ; const enhanceTSFile = await readGeneratedFile ( "/enhance.ts" ) ; const mainIndexTSFile = await readGeneratedFile ( "/index.ts" ) ; const directoryStructureString = getDirectoryStructureString ( outputDirPath ) ; expect ( enhanceTSFile ) . toMatchSnapshot ( "enhance" ) ; expect ( mainIndexTSFile ) . toMatchSnapshot ( "mainIndex" ) ; expect ( directoryStructureString ) . toMatchSnapshot ( "structure" ) ; } ) ; } ) ; describe ( "when only 'models' is set" , ( ) => { it ( "should generate model without _count, proper files, enhance and index" , async ( ) => { await generateCodeFromSchema ( schema , { emitOnly : [ "models" ] , outputDirPath , } ) ; const userModelTSFile = await readGeneratedFile ( "/models/User.ts" ) ; const enhanceTSFile = await readGeneratedFile ( "/enhance.ts" ) ; const mainIndexTSFile = await readGeneratedFile ( "/index.ts" ) ; const directoryStructureString = getDirectoryStructureString ( outputDirPath ) ; expect ( userModelTSFile ) . toMatchSnapshot ( "User" ) ; expect ( enhanceTSFile ) . toMatchSnapshot ( "enhance" ) ; expect ( mainIndexTSFile ) . toMatchSnapshot ( "mainIndex" ) ; expect ( directoryStructureString ) . toMatchSnapshot ( "structure" ) ; } ) ; } ) ; describe ( "when only 'crudResolvers' is set" , ( ) => { it ( "should generate proper files, enhance and index" , async ( ) => { await generateCodeFromSchema ( schema , { emitOnly : [ "crudResolvers" ] , outputDirPath , } ) ; const enhanceTSFile = await readGeneratedFile ( "/enhance.ts" ) ; const mainIndexTSFile = await readGeneratedFile ( "/index.ts" ) ; const directoryStructureString = getDirectoryStructureString ( outputDirPath ) ; expect ( enhanceTSFile ) . toMatchSnapshot ( "enhance" ) ; expect ( mainIndexTSFile ) . toMatchSnapshot ( "mainIndex" ) ; expect ( directoryStructureString ) . toMatchSnapshot ( "structure" ) ; } ) ; } ) ; describe ( "when only 'relationResolvers' is set" , ( ) => { it ( "should generate proper files, enhance and index" , async ( ) => { await generateCodeFromSchema ( schema , { emitOnly : [ "relationResolvers" ] , outputDirPath , } ) ; const enhanceTSFile = await readGeneratedFile ( "/enhance.ts" ) ; const mainIndexTSFile = await readGeneratedFile ( "/index.ts" ) ; const directoryStructureString = getDirectoryStructureString ( outputDirPath ) ; expect ( enhanceTSFile ) . toMatchSnapshot ( "enhance" ) ; expect ( mainIndexTSFile ) . toMatchSnapshot ( "mainIndex" ) ; expect ( directoryStructureString ) . toMatchSnapshot ( "structure" ) ; } ) ; } ) ; describe ( "when only 'inputs' is set" , ( ) => { it ( "should generate proper files, enhance and index" , async ( ) => { await generateCodeFromSchema ( schema , { emitOnly : [ "inputs" ] , outputDirPath , } ) ; const enhanceTSFile = await readGeneratedFile ( "/enhance.ts" ) ; const mainIndexTSFile = await readGeneratedFile ( "/index.ts" ) ; const directoryStructureString = getDirectoryStructureString ( outputDirPath ) ; expect ( enhanceTSFile ) . toMatchSnapshot ( "enhance" ) ; expect ( mainIndexTSFile ) . toMatchSnapshot ( "mainIndex" ) ; expect ( directoryStructureString ) . toMatchSnapshot ( "structure" ) ; } ) ; } ) ; describe ( "when only 'outputs' is set" , ( ) => { it ( "should generate proper files, enhance and index" , async ( ) => { await generateCodeFromSchema ( schema , { emitOnly : [ "outputs" ] , outputDirPath , } ) ; const enhanceTSFile = await readGeneratedFile ( "/enhance.ts" ) ; const mainIndexTSFile = await readGeneratedFile ( "/index.ts" ) ; const directoryStructureString = getDirectoryStructureString ( outputDirPath ) ; expect ( enhanceTSFile ) . toMatchSnapshot ( "enhance" ) ; expect ( mainIndexTSFile ) . toMatchSnapshot ( "mainIndex" ) ; expect ( directoryStructureString ) . toMatchSnapshot ( "structure" ) ; } ) ; } ) ; describe ( "when both 'crudResolvers' and `inputs` is set" , ( ) => { it ( "should not fail and generate proper files, enhance and index" , async ( ) => { await generateCodeFromSchema ( schema , { emitOnly : [ "crudResolvers" , "inputs" ] , outputDirPath , } ) ; const enhanceTSFile = await readGeneratedFile ( "/enhance.ts" ) ; const mainIndexTSFile = await readGeneratedFile ( "/index.ts" ) ; const directoryStructureString = getDirectoryStructureString ( outputDirPath ) ; expect ( enhanceTSFile ) . toMatchSnapshot ( "enhance" ) ; expect ( mainIndexTSFile ) . toMatchSnapshot ( "mainIndex" ) ; expect ( directoryStructureString ) . toMatchSnapshot ( "structure" ) ; } ) ; } ) ; } ) ;