import tempy from 'tempy' ; import path from 'path' ; import execa from 'execa' ; import fs from 'fs-extra' ; import createWebScriptsLibrary from '.' ; import getInstallCmd from './get-install-cmd' ; describe ( 'integration test' , ( ) => { let PKG_ROOT ; let LIBRARY_NAME ; let LIBRARY_ROOT ; let INSTALL_CMD ; describe ( 'succesful create' , ( ) => { beforeAll ( async ( ) => { PKG_ROOT = tempy . directory ( ) ; LIBRARY_NAME = 'my-cool-library' ; LIBRARY_ROOT = path . join ( PKG_ROOT , LIBRARY_NAME ) ; process . chdir ( PKG_ROOT ) ; INSTALL_CMD = getInstallCmd ( ) ; await createWebScriptsLibrary ( LIBRARY_NAME ) ; } , 60000 ) ; it ( 'replace the package.json name with the correct name' , async ( ) => { const pkgPath = path . join ( LIBRARY_ROOT , 'package.json' ) ; expect ( fs . existsSync ( pkgPath ) ) . toBe ( true ) ; expect ( require ( pkgPath ) . name ) . toBe ( LIBRARY_NAME ) ; } ) ; [ { cmd : 'lint' , asyncTimeout : 15000 } , { cmd : 'test' , asyncTimeout : 30000 } , { cmd : 'build' , asyncTimeout : 60000 } , ] . forEach ( ( { cmd , asyncTimeout = 10000 , } ) => { it ( `scaffolds such that "${ cmd }" command runs successfully` , async ( ) => { await expect ( execa ( INSTALL_CMD , [ 'run' , cmd ] , { cwd : LIBRARY_ROOT } ) ) . resolves . toBeDefined ( ) ; } , asyncTimeout ) ; } ) ; } ) ; describe ( 'failure cases' , ( ) => { beforeEach ( ( ) => { PKG_ROOT = tempy . directory ( ) ; LIBRARY_NAME = 'my-cool-library' ; LIBRARY_ROOT = path . join ( PKG_ROOT , LIBRARY_NAME ) ; process . chdir ( PKG_ROOT ) ; INSTALL_CMD = getInstallCmd ( ) ; } ) ; it ( 'fails when the library name is missing' , async ( ) => { await expect ( createWebScriptsLibrary ( ) ) . rejects . toEqual ( expect . any ( Error ) ) ; } ) ; it ( 'fails when a directory already exists' , async ( ) => { await fs . mkdir ( path . join ( PKG_ROOT , LIBRARY_NAME ) ) ; await expect ( createWebScriptsLibrary ( ) ) . rejects . toEqual ( expect . any ( Error ) ) ; } ) ; } ) ; } ) ;