jest . mock ( './SoundPlayer' ) ; import { SoundPlayer } from './SoundPlayer' ; import { SoundPlayerConsumer } from './SoundPlayerConsumer' ; beforeEach ( ( ) => { mocked ( SoundPlayer ) . mockClear ( ) ; } ) ; it ( 'We can check if the consumer called the class constructor' , ( ) => { new SoundPlayerConsumer ( ) ; expect ( SoundPlayer ) . toHaveBeenCalledTimes ( 1 ) ; } ) ; it ( 'We can check if the consumer called a method on the class instance' , ( ) => { expect ( SoundPlayer ) . not . toHaveBeenCalled ( ) ; const soundPlayerConsumer = new SoundPlayerConsumer ( ) ; expect ( SoundPlayer ) . toHaveBeenCalledTimes ( 1 ) ; const coolSoundFileName = 'song.mp3' ; soundPlayerConsumer . playSomethingCool ( ) ; const mockSoundPlayerInstance = mocked ( SoundPlayer ) . mock . instances [ 0 ] ; const mockPlaySoundFile = mocked ( mockSoundPlayerInstance . playSoundFile ) ; expect ( mockPlaySoundFile . mock . calls [ 0 ] [ 0 ] ) . toEqual ( coolSoundFileName ) ; expect ( mockPlaySoundFile ) . toHaveBeenCalledWith ( coolSoundFileName ) ; expect ( mockPlaySoundFile ) . toHaveBeenCalledTimes ( 1 ) ; } ) ;