import { mocked } from 'ts-jest/utils' ; import { readFixtureJSON } from './helpers' ; const iconSize = 96 ; describe ( 'jpeg-library' , ( ) => { beforeEach ( ( ) => { jest . resetModules ( ) ; } ) ; test ( 'ensure falls back to jpeg-js when jpeg-turbo exceptions' , async ( ) => { const img = readFixtureJSON ( 'fillImage-sample-icon-96.json' ) ; jest . mock ( '@julusian/jpeg-turbo' ) ; const jpegTurbo = require ( '@julusian/jpeg-turbo' ) ; mocked ( jpegTurbo . bufferSize ) . mockImplementation ( ( ) => 1000 ) ; mocked ( jpegTurbo . compressSync ) . mockImplementation ( ( ) => { throw new Error ( 'something failed' ) ; } ) ; jest . doMock ( 'jpeg-js' ) ; const jpegJS = require ( 'jpeg-js' ) ; mocked ( jpegJS . encode ) . mockImplementation ( ( src ) => ( { ... src , data : Buffer . alloc ( 100 ) } ) ) ; const { encodeJPEG } = require ( '../jpeg' ) as typeof import ( '../jpeg' ) ; const encoded = await encodeJPEG ( img , iconSize , iconSize , undefined ) ; expect ( encoded ) . toBeTruthy ( ) ; expect ( encoded ) . toHaveLength ( 100 ) ; } ) ; test ( 'ensure falls back to jpeg-js when jpeg-turbo is not available' , async ( ) => { const img = readFixtureJSON ( 'fillImage-sample-icon-96.json' ) ; jest . doMock ( '@julusian/jpeg-turbo' , undefined ) ; jest . doMock ( 'jpeg-js' ) ; const jpegJS = require ( 'jpeg-js' ) ; mocked ( jpegJS . encode ) . mockImplementation ( ( src ) => ( { ... src , data : Buffer . alloc ( 100 ) } ) ) ; const { encodeJPEG } = require ( '../jpeg' ) as typeof import ( '../jpeg' ) ; const encoded = await encodeJPEG ( img , iconSize , iconSize , undefined ) ; expect ( encoded ) . toBeTruthy ( ) ; expect ( encoded ) . toHaveLength ( 100 ) ; } ) ; } ) ;