lighthouse-core/test/gather/mock-commands.js
/**
 * Creates a jest mock function whose implementation consumes mocked protocol responses matching the
 * requested command in the order they were mocked.
 *
 * It is decorated with two methods:
 *    - `mockResponse` which pushes protocol message responses for consumption
 *    - `findInvocation` which asserts that `sendCommand` was invoked with the given command and
 *      returns the protocol message argument.
 */
Creates a jest mock function whose implementation consumes mocked protocol responses matching the
requested command in the order they were mocked.


Creates a jest mock function whose implementation consumes mocked protocol responses matching the requested command in the order they were mocked .
function createMockSendCommandFn() {
  const mockResponses = [];
  const mockFn = jest.fn().mockImplementation((command, sessionId, ...args) => {
    const indexOfResponse = mockResponses
      .findIndex(entry => entry.command === command && entry.sessionId === sessionId);
    if (indexOfResponse === -1) throw new Error(`${command} unimplemented`);
    const {response, delay} = mockResponses[indexOfResponse];
    mockResponses.splice(indexOfResponse, 1);
    const returnValue = typeof response === 'function' ? response(...args) : response;
    if (delay) return new Promise(resolve => setTimeout(() => resolve(returnValue), delay));
    return Promise.resolve(returnValue);
  });
  mockFn.mockResponse = (command, response, delay) => {
    mockResponses.push({command, response, delay});
    return mockFn;
  };
  mockFn.mockResponseToSession = (command, sessionId, response, delay) => {
    mockResponses.push({command, sessionId, response, delay});
    return mockFn;
  };
  mockFn.findInvocation = (command, sessionId) => {
    expect(mockFn).toHaveBeenCalledWith(command, sessionId, expect.anything());
    return mockFn.mock.calls.find(call => call[0] === command && call[1] === sessionId)[2];
  };
  return mockFn;
}
function createMockSendCommandFn ( ) { const mockResponses = [ ] ; const mockFn = jest . fn ( ) . mockImplementation ( ( command , sessionId , ... args ) => { const indexOfResponse = mockResponses . findIndex ( entry => entry . command === command && entry . sessionId === sessionId ) ; if ( indexOfResponse === - 1 ) throw new Error ( ` ${ command } ` ) ; const { response , delay } = mockResponses [ indexOfResponse ] ; mockResponses . splice ( indexOfResponse , 1 ) ; const returnValue = typeof response === 'function' ? response ( ... args ) : response ; if ( delay ) return new Promise ( resolve => setTimeout ( ( ) => resolve ( returnValue ) , delay ) ) ; return Promise . resolve ( returnValue ) ; } ) ; mockFn . mockResponse = ( command , response , delay ) => { mockResponses . push ( { command , response , delay } ) ; return mockFn ; } ; mockFn . mockResponseToSession = ( command , sessionId , response , delay ) => { mockResponses . push ( { command , sessionId , response , delay } ) ; return mockFn ; } ; mockFn . findInvocation = ( command , sessionId ) => { expect ( mockFn ) . toHaveBeenCalledWith ( command , sessionId , expect . anything ( ) ) ; return mockFn . mock . calls . find ( call => call [ 0 ] === command && call [ 1 ] === sessionId ) [ 2 ] ; } ; return mockFn ; }
Creates a jest mock function whose implementation consumes mocked protocol responses matching the
 requested command in the order they were mocked.
Creates a jest mock function whose implementation consumes mocked protocol responses matching the requested command in the order they were mocked .