<commit_before>'use strict';
/**
 * Unit tests for index.
 */

const _ = require('lodash');
const BbPromise = require('bluebird');
const chai = require('chai');
const sinon = require('sinon');
const mockery = require('mockery');
const Serverless = require('serverless');
const Module = require('module');

chai.use(require('chai-as-promised'));
chai.use(require('sinon-chai'));

const expect = chai.expect;

describe('ServerlessWebpack', () => {
  let sandbox;
  let serverless;
  let ServerlessWebpack;
  let moduleStub;

  before(function () {
    // Mockery might take some time to clear the cache. So add 3 seconds to the default timeout.
    this.timeout(5000);

    sandbox = sinon.createSandbox();

    mockery.enable({ useCleanCache: true, warnOnUnregistered: false });
    mockery.registerMock('ts-node/register', {});
    mockery.registerMock('webpack', {});

    ServerlessWebpack = require('./index');
    moduleStub = sandbox.stub(Module, '_load');
  });

  beforeEach(() => {
    serverless = new Serverless();
    serverless.cli = {
      log: sandbox.stub(),
      consoleLog: sandbox.stub()
    };

    sandbox.stub(serverless.pluginManager, 'spawn').returns(BbPromise.resolve());
  });

  afterEach(() => {
    sandbox.resetHistory();
  });

  after(() => {
    sandbox.restore();
    mockery.disable();
    mockery.deregisterAll();
  });

  it('should expose a lib object', () => {
    const lib = ServerlessWebpack.lib;
    expect(lib).to.be.an('object');
    expect(lib).to.have.a.property('entries').that.is.an('object').that.is.empty;
    expect(lib).to.have.a.property('webpack').that.is.an('object').that.deep.equals({
      isLocal: false
    });
  });

  describe('with a TS webpack configuration', () => {
    it('should support old config and register ts-node', () => {
      _.set(serverless, 'service.custom.webpack', 'webpack.config.ts');
      new ServerlessWebpack(serverless, {});
      expect(Module._load).to.have.been.calledOnce;
      expect(Module._load).to.have.been.calledWith('ts-node/register');
    });

    it('should support new config and register ts-node', () => {
      _.set(serverless, 'service.custom.webpack.webpackConfig', 'webpack.config.ts');
      new ServerlessWebpack(serverless, {});
      expect(Module._load).to.have.been.calledOnce;
      expect(Module._load).to.have.been.calledWith('ts-node/register');
    });

    it('should throw an error if config use TS but ts-node was not added as dependency', () => {
      moduleStub.throws();

      _.set(serverless, 'service.custom.webpack.webpackConfig', 'webpack.config.ts');

      const badDeps = function () {
        new ServerlessWebpack(serverless, {});
      };

      expect(badDeps).to.throw(
        'If you want to use TypeScript with serverless-webpack, please add "ts-node" as dependency.'
      );

      expect(Module._load).to.have.been.calledOnce;
      expect(Module._load).to.have.been.calledWith('ts-node/register');<commit_msg>Formatting<commit_after>'use strict';
/**
 * Unit tests for index.
 */

const _ = require('lodash');
const BbPromise = require('bluebird');
const chai = require('chai');
const sinon = require('sinon');
const mockery = require('mockery');
const Serverless = require('serverless');
const Module = require('module');

chai.use(require('chai-as-promised'));
chai.use(require('sinon-chai'));

const expect = chai.expect;

describe('ServerlessWebpack', () => {
  let sandbox;
  let serverless;
  let ServerlessWebpack;
  let moduleStub;

  before(function () {
    // Mockery might take some time to clear the cache. So add 3 seconds to the default timeout.
    this.timeout(5000);

    sandbox = sinon.createSandbox();

    mockery.enable({ useCleanCache: true, warnOnUnregistered: false });
    mockery.registerMock('ts-node/register', {});
    mockery.registerMock('webpack', {});

    ServerlessWebpack = require('./index');
    moduleStub = sandbox.stub(Module, '_load');
  });

  beforeEach(() => {
    serverless = new Serverless();
    serverless.cli = {
      log: sandbox.stub(),
      consoleLog: sandbox.stub()
    };

    sandbox.stub(serverless.pluginManager, 'spawn').returns(BbPromise.resolve());
  });

  afterEach(() => {
    sandbox.resetHistory();
  });

  after(() => {
    sandbox.restore();
    mockery.disable();
    mockery.deregisterAll();
  });

  it('should expose a lib object', () => {
    const lib = ServerlessWebpack.lib;
    expect(lib).to.be.an('object');
    expect(lib).to.have.a.property('entries').that.is.an('object').that.is.empty;
    expect(lib).to.have.a.property('webpack').that.is.an('object').that.deep.equals({
      isLocal: false
    });
  });

  describe('with a TS webpack configuration', () => {
    it('should support old config and register ts-node', () => {
      _.set(serverless, 'service.custom.webpack', 'webpack.config.ts');
      new ServerlessWebpack(serverless, {});
      expect(Module._load).to.have.been.calledOnce;
      expect(Module._load).to.have.been.calledWith('ts-node/register');
    });

    it('should support new config and register ts-node', () => {
      _.set(serverless, 'service.custom.webpack.webpackConfig', 'webpack.config.ts');
      new ServerlessWebpack(serverless, {});
      expect(Module._load).to.have.been.calledOnce;
      expect(Module._load).to.have.been.calledWith('ts-node/register');
    });

    it('should throw an error if config use TS but ts-node was not added as dependency', () => {
      moduleStub.throws();

      _.set(serverless, 'service.custom.webpack.webpackConfig', 'webpack.config.ts');

      const badDeps = function() {
        new ServerlessWebpack(serverless, {});
      };

      expect(badDeps).to.throw(
        'If you want to use TypeScript with serverless-webpack, please add "ts-node" as dependency.'
      );

      expect(Module._load).to.have.been.calledOnce;
      expect(Module._load).to.have.been.calledWith('ts-node/register');
{'fraction_non_alphanumeric': 0.16897600540723218, 'fraction_numerical': 0.0016897600540723217, 'mean_word_length': 3.5741885625966, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4388087', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1821, 'n_words': 351}