Esempio n. 1
0
const mockClient = () => {
  const client = {
    init: getMockFunction(),
    elements: getMockFunction(),
  };

  client.elements.mockReturnValue(
    Promise.resolve({ value: [] })
  );

  return client;
};
Esempio n. 2
0
 fn: function() {
   const fn = moduleMocker.getMockFunction();
   if (arguments.length > 0) {
     return fn.mockImplementation(arguments[0]);
   }
   return fn;
 },
Esempio n. 3
0
File: index.js Progetto: merxer/kata
  _createRuntimeFor(from) {
    const disableAutomock = () => {
      this._shouldAutoMock = false;
      return runtime;};

    const enableAutomock = () => {
      this._shouldAutoMock = true;
      return runtime;};

    const unmock = moduleName => {
      const moduleID = this._normalizeID(from, moduleName);
      this._explicitShouldMock[moduleID] = false;
      return runtime;};

    const deepUnmock = moduleName => {
      const moduleID = this._normalizeID(from, moduleName);
      this._explicitShouldMock[moduleID] = false;
      this._transitiveShouldMock[moduleID] = false;
      return runtime;};

    const mock = (
    moduleName, 
    mockFactory, 
    options) => 
    {
      if (mockFactory !== undefined) {
        return setMockFactory(moduleName, mockFactory, options);}


      const moduleID = this._normalizeID(from, moduleName);
      this._explicitShouldMock[moduleID] = true;
      return runtime;};

    const setMockFactory = (moduleName, mockFactory, options) => {
      this.setMock(from, moduleName, mockFactory, options);
      return runtime;};

    const useFakeTimers = () => {
      this._environment.fakeTimers.useFakeTimers();
      return runtime;};

    const useRealTimers = () => {
      this._environment.fakeTimers.useRealTimers();
      return runtime;};


    const runtime = { 
      addMatchers: matchers => {
        const jasmine = this._environment.global.jasmine;
        const addMatchers = 
        jasmine.addMatchers || jasmine.getEnv().currentSpec.addMatchers;
        addMatchers(matchers);}, 


      autoMockOff: disableAutomock, 
      disableAutomock, 

      autoMockOn: enableAutomock, 
      enableAutomock, 

      clearAllTimers: () => this._environment.fakeTimers.clearAllTimers(), 
      currentTestPath: () => this._environment.testFilePath, 

      dontMock: unmock, 
      unmock, 

      getTestEnvData: () => {
        const frozenCopy = {};
        // Make a shallow copy only because a deep copy seems like
        // overkill..
        Object.keys(this._config.testEnvData).forEach(
        key => frozenCopy[key] = this._config.testEnvData[key]);

        Object.freeze(frozenCopy);
        return frozenCopy;}, 


      genMockFromModule: moduleName => {
        return this._generateMock(from, moduleName);}, 

      genMockFunction: moduleMocker.getMockFunction, 
      genMockFn: moduleMocker.getMockFunction, 
      fn() {
        const fn = moduleMocker.getMockFunction();
        if (arguments.length > 0) {
          return fn.mockImplementation(arguments[0]);}

        return fn;}, 

      isMockFunction: moduleMocker.isMockFunction, 

      doMock: mock, 
      mock, 

      resetModuleRegistry: () => {
        this.resetModuleRegistry();
        return runtime;}, 


      runAllTicks: () => this._environment.fakeTimers.runAllTicks(), 
      runAllImmediates: () => this._environment.fakeTimers.runAllImmediates(), 
      runAllTimers: () => this._environment.fakeTimers.runAllTimers(), 
      runOnlyPendingTimers: () => 
      this._environment.fakeTimers.runOnlyPendingTimers(), 

      setMock: (moduleName, mock) => 
      setMockFactory(moduleName, () => mock), 

      deepUnmock, 

      useFakeTimers, 
      useRealTimers };

    return runtime;}}
Esempio n. 4
0
  _createRuntimeFor(from: Path) {
    const disableAutomock = () => {
      this._shouldAutoMock = false;
      return runtime;
    };
    const enableAutomock = () => {
      this._shouldAutoMock = true;
      return runtime;
    };
    const unmock = (moduleName: string) => {
      const moduleID = this._normalizeID(from, moduleName);
      if (
        !this._shouldAutoMock &&
        this._config.automock === false &&
        this._explicitShouldMock[moduleID] !== true
      ) {
        this._environment.global.console.warn(
          `jest.unmock('${moduleName}') was called but automocking is ` +
          `disabled. Remove the unnecessary call to \`jest.unmock\` or ` +
          `enable automocking for this test via \`jest.enableAutomock();\`. ` +
          `This warning is likely a result of a default configuration change ` +
          `in Jest 15.\n\n` +
          `Release Blog Post: https://facebook.github.io/jest/blog/2016/09/01/jest-15.html`,
        );
      }
      this._explicitShouldMock[moduleID] = false;
      return runtime;
    };
    const deepUnmock = (moduleName: string) => {
      const moduleID = this._normalizeID(from, moduleName);
      this._explicitShouldMock[moduleID] = false;
      this._transitiveShouldMock[moduleID] = false;
      return runtime;
    };
    const mock = (
      moduleName: string,
      mockFactory: Object,
      options: {virtual: boolean},
    ) => {
      if (mockFactory !== undefined) {
        return setMockFactory(moduleName, mockFactory, options);
      }

      const moduleID = this._normalizeID(from, moduleName);
      this._explicitShouldMock[moduleID] = true;
      return runtime;
    };
    const setMockFactory = (moduleName, mockFactory, options) => {
      this.setMock(from, moduleName, mockFactory, options);
      return runtime;
    };
    const useFakeTimers = () => {
      this._environment.fakeTimers.useFakeTimers();
      return runtime;
    };
    const useRealTimers = () => {
      this._environment.fakeTimers.useRealTimers();
      return runtime;
    };
    const resetModules = () => {
      this.resetModules();
      return runtime;
    };

    const runtime = {
      addMatchers: (matchers: Object) => {
        const jasmine = this._environment.global.jasmine;
        const addMatchers =
          jasmine.addMatchers || jasmine.getEnv().currentSpec.addMatchers;
        addMatchers(matchers);
      },

      autoMockOff: disableAutomock,
      disableAutomock,

      autoMockOn: enableAutomock,
      enableAutomock,

      clearAllTimers: () => this._environment.fakeTimers.clearAllTimers(),

      dontMock: unmock,
      unmock,

      genMockFromModule: (moduleName: string) => {
        return this._generateMock(from, moduleName);
      },
      genMockFunction: moduleMocker.getMockFunction,
      genMockFn: moduleMocker.getMockFunction,
      fn() {
        const fn = moduleMocker.getMockFunction();
        if (arguments.length > 0) {
          return fn.mockImplementation(arguments[0]);
        }
        return fn;
      },
      isMockFunction: moduleMocker.isMockFunction,

      doMock: mock,
      mock,

      resetModules,
      resetModuleRegistry: resetModules,

      runAllTicks: () => this._environment.fakeTimers.runAllTicks(),
      runAllImmediates: () => this._environment.fakeTimers.runAllImmediates(),
      runAllTimers: () => this._environment.fakeTimers.runAllTimers(),
      runOnlyPendingTimers: () =>
        this._environment.fakeTimers.runOnlyPendingTimers(),

      setMock: (moduleName: string, mock: Object) =>
        setMockFactory(moduleName, () => mock),

      deepUnmock,

      useFakeTimers,
      useRealTimers,
    };
    return runtime;
  }