import execa from 'execa' ; import type { ExecaChildProcess } from 'execa' ; import fs from 'fs-jetpack' ; import type { FSJetpack } from 'fs-jetpack/types' ; import path from 'path' ; import tempy from 'tempy' ; /**
 * Base test context.
 */ type BaseContext = { tmpDir : string ; fs : FSJetpack ; mocked : { cwd : string ; } ; /**
     * Setup the temporary directory based on the contents of some fixture.
     */ fixture : ( name : string ) => void ; remarks cli : ( ... input : string [ ] ) => ExecaChildProcess < string > ; } ; /**
 * Create test context to use in tests. Provides the following:
 *
 * - A temporary directory
 * - an fs-jetpack instance bound to the temporary directory
 * - Mocked process.cwd via Node process.chdir
 * - Fixture loader for boostrapping the temporary directory with content
 */ export const jestContext = { new : function ( ctx = { } as any ) { const c = ctx as BaseContext ; beforeEach ( ( ) => { const originalCwd = process . cwd ( ) ; c . tmpDir = tempy . directory ( ) ; c . fs = fs . cwd ( c . tmpDir ) ; c . fixture = ( name ) => { c . fs . copy ( path . join ( originalCwd , 'src' , '__tests__' , 'fixtures' , name ) , '.' , { overwrite : true , } ) ; c . fs . symlink ( path . join ( originalCwd , '..' , 'client' ) , path . join ( c . fs . cwd ( ) , 'node_modules' , '@prisma' , 'client' ) ) ; } ; c . mocked = c . mocked ?? { cwd : process . cwd ( ) , } ; c . cli = ( ... input ) => { return execa . node ( path . join ( originalCwd , '../cli/build/index.js' ) , input , { cwd : c . fs . cwd ( ) , stdio : 'pipe' , all : true , } ) ; } ; process . chdir ( c . tmpDir ) ; } ) ; afterEach ( ( ) => { process . chdir ( c . mocked . cwd ) ; } ) ; return factory ( ctx ) ; } , } ; /**
 * Factory for creating a context contributor possibly configured in some special way.
 */ type ContextContributorFactory < Settings , Context , NewContext > = Settings extends { } ? ( ) => ContextContributor < Context , NewContext > : ( settings : Settings ) => ContextContributor < Context , NewContext > ; /**
 * A function that provides additonal test context.
 */ type ContextContributor < Context , NewContext > = ( ctx : Context ) => NewContext ; /**
 * Main context builder API that permits recursively building up context.
 */ function factory < Context > ( ctx ) { return { add < NewContext > ( contextContributor ) { contextContributor ( ctx ) ; return factory < Context & NewContext > ( ctx as any ) ; } , assemble ( ) { return ctx ; } , } ; } /**
 * Test context contributor. Mocks console.error with a Jest spy before each test.
 */ export const jestConsoleContext = ( ) => ( ctx ) => { beforeEach ( ( ) => { ctx . mocked [ 'console.error' ] = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ; ctx . mocked [ 'console.log' ] = jest . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } ) ; ctx . mocked [ 'console.info' ] = jest . spyOn ( console , 'info' ) . mockImplementation ( ( ) => { } ) ; ctx . mocked [ 'console.warn' ] = jest . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => { } ) ; } ) ; afterEach ( ( ) => { ctx . mocked [ 'console.error' ] . mockRestore ( ) ; ctx . mocked [ 'console.log' ] . mockRestore ( ) ; ctx . mocked [ 'console.info' ] . mockRestore ( ) ; ctx . mocked [ 'console.warn' ] . mockRestore ( ) ; } ) ; return null as any ; } ;