import { RollupLifecycleHooks } from "./rollup-lifecycle-hooks.class" ; import { join } from "path" ; import { Project } from "@sakuli/core" ; import { mockPartial } from "sneer" ; describe ( "RollupLifecycleHooks" , ( ) => { let project ; beforeEach ( ( ) => { const rootDir = join ( __dirname , ".." , "__mock__" ) ; project = mockPartial < Project > ( { rootDir , testFiles : [ ] , get ( key ) { return ( < Record < string , string > > { tsconfig : join ( rootDir , "tsconfig.spec.json" ) , } ) [ key ] ; } , } ) ; } ) ; it ( "should create a bundle from index.js file" , async ( ) => { const hooks = new RollupLifecycleHooks ( ) ; const expectedFileContent = "'use strict';\n\n" + "const pi = 3.14159265359;\n" + "\n" + "console.log(pi);\n" ; const fileContent = await hooks . readFileContent ( { path : "index.js" , } , project ) ; expect ( fileContent ) . toBe ( expectedFileContent ) ; } ) ; it ( "should throw on parser errors" , async ( ) => { const hooks = new RollupLifecycleHooks ( ) ; const fileName = "error.js" ; const o = hooks . readFileContent ( { path : fileName , } , project ) ; await expect ( o ) . rejects . toThrowError ( /^Syntax error in file .* on line 1, column 41\.\n1: console.log\("this should throw an error"\)\);\n.*$/ ) ; } ) ; it ( "should add typescript plugin if file extension is .ts" , async ( ) => { const hooks = new RollupLifecycleHooks ( ) ; const output = await hooks . readFileContent ( { path : "ts/index.ts" , } , project ) ; expect ( output ) . toContain ( `require('fs')` ) ; expect ( output ) . toContain ( `const server = ` ) ; } ) ; it ( "should error when file has typescript errors" , async ( ) => { const hooks = new RollupLifecycleHooks ( ) ; await expect ( hooks . readFileContent ( { path : "ts/error.ts" , } , project ) ) . rejects . toEqual ( expect . anything ( ) ) ; } ) ; } ) ;