<commit_before>/**
 * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 *
 * @emails oncall+jsinfra
 */
'use strict';

describe('moduleMocker', () => {
  let moduleMocker;

  beforeEach(() => {
    moduleMocker = require('../');
  });

  describe('getMetadata', () => {
    it('returns the function `name` property', () => {
      function x() {}
      const metadata = moduleMocker.getMetadata(x);
      expect(x.name).toBe('x');
      expect(metadata.name).toBe('x');
    });

    it('mocks constant values', () => {
      expect(moduleMocker.getMetadata('banana').value).toEqual('banana');
      expect(moduleMocker.getMetadata(27).value).toEqual(27);
      expect(moduleMocker.getMetadata(false).value).toEqual(false);
    });
  });

  describe('generateFromMetadata', () => {
    it('forwards the function name property', () => {
      function foo() {}
      const fooMock = moduleMocker.generateFromMetadata(
        moduleMocker.getMetadata(foo),
      );
      expect(fooMock.name).toBe('foo');
    });

    it('escapes illegal characters in function name property', () => {
      const foo = {
        'foo-bar': () => {},
      };

      const fooBarMock = moduleMocker.generateFromMetadata(
        moduleMocker.getMetadata(foo['foo-bar']),
      );
      expect(fooBarMock.name).toBe('foo$bar');
    });

    it('special cases the mockConstructor name', () => {
      function mockConstructor() {}
      const fooMock = moduleMocker.generateFromMetadata(
        moduleMocker.getMetadata(mockConstructor),
      );
      // Depends on node version
      expect(
        !fooMock.name ||
        fooMock.name === 'mockConstructor',
      ).toBeTruthy();
    });

    it('wont interfere with previous mocks on a shared prototype', () => {
      const ClassFoo = function() {};
      ClassFoo.prototype.x = () => {};
      const ClassFooMock = moduleMocker.generateFromMetadata(
        moduleMocker.getMetadata(ClassFoo),
      );
      const foo = new ClassFooMock();
      const bar = new ClassFooMock();

      foo.x.mockImplementation(() => {
        return 'Foo';
      });
      bar.x.mockImplementation(() => {
        return 'Bar';
      });

      expect(foo.x()).toBe('Foo');
      expect(bar.x()).toBe('Bar');
    });

    it('does not mock non-enumerable getters', () => {
      const foo = Object.defineProperties({}, {
        nonEnumMethod: {
          value: () => {},
        },
        nonEnumGetter: {
          get: () => { throw new Error(); },
        },
      });
      const fooMock = moduleMocker.generateFromMetadata(
        moduleMocker.getMetadata(foo),
      );

      expect(typeof foo.nonEnumMethod).toBe('function');

      expect(fooMock.nonEnumMethod.mock).not.toBeUndefined();
      expect(fooMock.nonEnumGetter).toBeUndefined();
    });

    it('mocks getters of ES modules', () => {
      const foo = Object.defineProperties({}, {
        __esModule: {
          value: true,
        },
        enumGetter: {
          enumerable: true,
          get: () => 10,
        },
      });
      const fooMock = moduleMocker.generateFromMetadata(
        moduleMocker.getMetadata(foo),
      );
      expect(fooMock.enumGetter).toBeDefined();
    });<commit_msg>Fix mock-test on node 4. (#1691)<commit_after>/**
 * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 *
 * @emails oncall+jsinfra
 */
'use strict';

describe('moduleMocker', () => {
  let moduleMocker;

  beforeEach(() => {
    moduleMocker = require('../');
  });

  describe('getMetadata', () => {
    it('returns the function `name` property', () => {
      function x() {}
      const metadata = moduleMocker.getMetadata(x);
      expect(x.name).toBe('x');
      expect(metadata.name).toBe('x');
    });

    it('mocks constant values', () => {
      expect(moduleMocker.getMetadata('banana').value).toEqual('banana');
      expect(moduleMocker.getMetadata(27).value).toEqual(27);
      expect(moduleMocker.getMetadata(false).value).toEqual(false);
    });
  });

  describe('generateFromMetadata', () => {
    it('forwards the function name property', () => {
      function foo() {}
      const mock = moduleMocker.generateFromMetadata(
        moduleMocker.getMetadata(foo),
      );
      expect(mock.name).toBe('foo');
    });

    it('escapes illegal characters in function name property', () => {
      const foo = {
        'foo-bar': () => {},
      };

      const mock = moduleMocker.generateFromMetadata(
        moduleMocker.getMetadata(foo['foo-bar']),
      );
      expect(
        !mock.name ||
        mock.name === 'foo$bar'
      ).toBeTruthy();
    });

    it('special cases the mockConstructor name', () => {
      function mockConstructor() {}
      const mock = moduleMocker.generateFromMetadata(
        moduleMocker.getMetadata(mockConstructor),
      );
      // Depends on node version
      expect(
        !mock.name ||
        mock.name === 'mockConstructor',
      ).toBeTruthy();
    });

    it('wont interfere with previous mocks on a shared prototype', () => {
      const ClassFoo = function() {};
      ClassFoo.prototype.x = () => {};
      const ClassFooMock = moduleMocker.generateFromMetadata(
        moduleMocker.getMetadata(ClassFoo),
      );
      const foo = new ClassFooMock();
      const bar = new ClassFooMock();

      foo.x.mockImplementation(() => {
        return 'Foo';
      });
      bar.x.mockImplementation(() => {
        return 'Bar';
      });

      expect(foo.x()).toBe('Foo');
      expect(bar.x()).toBe('Bar');
    });

    it('does not mock non-enumerable getters', () => {
      const foo = Object.defineProperties({}, {
        nonEnumMethod: {
          value: () => {},
        },
        nonEnumGetter: {
          get: () => { throw new Error(); },
        },
      });
      const mock = moduleMocker.generateFromMetadata(
        moduleMocker.getMetadata(foo),
      );

      expect(typeof foo.nonEnumMethod).toBe('function');

      expect(mock.nonEnumMethod.mock).not.toBeUndefined();
      expect(mock.nonEnumGetter).toBeUndefined();
    });

    it('mocks getters of ES modules', () => {
      const foo = Object.defineProperties({}, {
        __esModule: {
          value: true,
        },
        enumGetter: {
          enumerable: true,
          get: () => 10,
        },
      });
      const mock = moduleMocker.generateFromMetadata(
        moduleMocker.getMetadata(foo),
      );
      expect(mock.enumGetter).toBeDefined();
    });
{'fraction_non_alphanumeric': 0.1547790932717297, 'fraction_numerical': 0.0036095870632399654, 'mean_word_length': 2.5073417721518987, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5389727', 'n_tokens_mistral': 2024, 'n_tokens_neox': 1956, 'n_words': 413}