import { expect } from '@loopback/testlab' ; import { readFileSync } from 'fs' ; import path from 'path' ; import { format } from 'util' ; import { main } from '../..' ; describe ( 'context examples' , ( ) => { const logs = [ ] ; const errors = [ ] ; let originalConsoleLog = console . log ; let originalConsoleError = console . error ; before ( disableConsoleOutput ) ; it ( 'runs all examples' , async function ( this ) { this . timeout ( 5000 ) ; const expectedLogs = loadExpectedLogs ( ) ; await main ( ) ; expect ( errors ) . to . eql ( [ ] ) ; expect ( replaceDates ( logs ) ) . to . eql ( replaceDates ( expectedLogs ) ) ; } ) ; after ( restoreConsoleOutput ) ; /**
     * Load the expected logs from `fixtures/examples-output.txt`.
     *
     * Run `node . > fixtures/examples-output.txt` to update the logs if needed.
     */ function loadExpectedLogs ( ) { const output = readFileSync ( path . join ( __dirname , '../../../fixtures/examples-output.txt' ) , 'utf-8' ) ; const items = output . split ( '\n' ) ; items . pop ( ) ; return items ; } function disableConsoleOutput ( ) { originalConsoleLog = console . log ; console . log = ( fmt , ... params ) => { logs . push ( format ( fmt , ... params ) ) ; } ; originalConsoleError = console . error ; console . error = ( fmt , ... params ) => { errors . push ( format ( fmt , ... params ) ) ; } ; } function restoreConsoleOutput ( ) { console . log = originalConsoleLog ; console . error = originalConsoleError ; } function replaceDates ( items ) { return items . map ( str => str . replace ( /\[\d+[\w\d\-\.\:]+\]/g , '[DATE]' ) ) ; } } ) ;