{"size": 8441, "ext": "js", "max_stars_repo_path": "test/index.spec.js", "max_stars_repo_name": "pastelmind/rollup-plugin-prettier", "max_stars_repo_stars_event_min_datetime": "2017-05-09T13:08:29.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-04T15:53:12.000Z", "max_issues_repo_path": "test/index.spec.js", "max_issues_repo_name": "mjeanroy/rollup-plugin-prettier", "max_issues_repo_issues_event_min_datetime": "2017-05-29T09:15:49.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-21T20:14:27.000Z", "max_forks_repo_path": "test/index.spec.js", "max_forks_repo_name": "pastelmind/rollup-plugin-prettier", "max_forks_repo_forks_event_min_datetime": "2018-01-16T09:59:08.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-05T07:38:05.000Z"}
{"max_stars_count": 63, "max_issues_count": 144, "max_forks_count": 14, "avg_line_length": 29.9326241135, "max_line_length": 99, "alphanum_fraction": 0.6321525886}
/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2017-2020 Mickael Jeanroy
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import path from 'path';
import prettier from 'prettier';
import rollupPluginPrettier from '../src/index.js';
import {verifyWarnLogsBecauseOfSourcemap} from './utils/verify-warn-logs-because-of-source-map.js';
import {verifyWarnLogsNotTriggered} from './utils/verify-warn-logs-not-triggered.js';
import {installWarnSpy} from './utils/install-warn-spy.js';

describe('rollup-plugin-prettier', () => {
  beforeEach(() => {
    installWarnSpy();
  });

  it('should have a name', () => {
    const instance = rollupPluginPrettier();
    expect(instance.name).toBe('rollup-plugin-prettier');
  });

  it('should run prettier', () => {
    const instance = rollupPluginPrettier({
      parser: 'babel',
    });

    const code = 'var foo=0;var test="hello world";';
    const chunk = {isEntry: false, imports: []};
    const outputOptions = {};
    const result = instance.renderChunk(code, chunk, outputOptions);

    expect(console.warn).not.toHaveBeenCalled();
    expect(result.map).not.toBeDefined();
    expect(result.code).toBe(
        'var foo = 0;\n' +
        'var test = "hello world";\n'
    );
  });

  it('should run prettier with sourcemap in output options', () => {
    const instance = rollupPluginPrettier({
      parser: 'babel',
    });

    const code = 'var foo=0;var test="hello world";';
    const chunk = {isEntry: false, imports: []};
    const outputOptions = {sourcemap: true};
    const result = instance.renderChunk(code, chunk, outputOptions);

    verifyWarnLogsBecauseOfSourcemap();
    expect(result.map).toBeDefined();
    expect(result.code).toBe(
        'var foo = 0;\n' +
        'var test = "hello world";\n'
    );
  });

  it('should run prettier with sourcemap in output options skipping warn message', () => {
    const instance = rollupPluginPrettier({
      sourcemap: 'silent',
      parser: 'babel',
    });

    const code = 'var foo=0;var test="hello world";';
    const chunk = {isEntry: false, imports: []};
    const outputOptions = {sourcemap: true};
    const result = instance.renderChunk(code, chunk, outputOptions);

    verifyWarnLogsNotTriggered();
    expect(result.map).toBeDefined();
    expect(result.code).toBe(
        'var foo = 0;\n' +
        'var test = "hello world";\n'
    );
  });

  it('should run prettier with sourcemap (lowercase) in plugin options', () => {
    const instance = rollupPluginPrettier({
      sourcemap: true,
      parser: 'babel',
    });

    const code = 'var foo=0;var test="hello world";';
    const chunk = {isEntry: false, imports: []};
    const outputOptions = {};
    const result = instance.renderChunk(code, chunk, outputOptions);

    verifyWarnLogsBecauseOfSourcemap();
    expect(result.map).toBeDefined();
    expect(result.code).toBe(
        'var foo = 0;\n' +
        'var test = "hello world";\n'
    );
  });

  it('should run prettier with sourcemap disabled in output options', () => {
    const instance = rollupPluginPrettier({
      parser: 'babel',
    });

    const code = 'var foo=0;var test="hello world";';
    const chunk = {isEntry: false, imports: []};
    const outputOptions = {sourcemap: false};
    const result = instance.renderChunk(code, chunk, outputOptions);

    verifyWarnLogsNotTriggered();
    expect(result.map).not.toBeDefined();
    expect(result.code).toBe(
        'var foo = 0;\n' +
        'var test = "hello world";\n'
    );
  });

  it('should run prettier with options', () => {
    const options = {
      parser: 'babel',
      singleQuote: true,
    };

    const instance = rollupPluginPrettier(options);
    const code = 'var foo=0;var test="hello world";';
    const chunk = {isEntry: false, imports: []};
    const outputOptions = {sourcemap: false};
    const result = instance.renderChunk(code, chunk, outputOptions);

    expect(result.code).toBe(
        `var foo = 0;\n` +
        `var test = 'hello world';\n`
    );
  });

  it('should remove unnecessary spaces', () => {
    const instance = rollupPluginPrettier({
      parser: 'babel',
    });

    const code = 'var foo    =    0;\nvar test = "hello world";';
    const chunk = {isEntry: false, imports: []};
    const outputOptions = {sourcemap: false};
    const result = instance.renderChunk(code, chunk, outputOptions);

    expect(result.code).toBe(
        'var foo = 0;\n' +
        'var test = "hello world";\n'
    );
  });

  it('should add and remove characters', () => {
    const instance = rollupPluginPrettier({
      parser: 'babel',
    });

    const code = 'var foo    =    0;var test = "hello world";';
    const chunk = {isEntry: false, imports: []};
    const outputOptions = {sourcemap: false};
    const result = instance.renderChunk(code, chunk, outputOptions);

    expect(result.code).toBe(
        'var foo = 0;\n' +
        'var test = "hello world";\n'
    );
  });

  it('should avoid side effect and do not modify plugin options', () => {
    const options = {
      parser: 'babel',
      sourcemap: false,
    };

    const instance = rollupPluginPrettier(options);
    const code = 'var foo = 0;';
    const chunk = {isEntry: false, imports: []};
    const outputOptions = {};
    instance.renderChunk(code, chunk, outputOptions);

    // It should not have been touched.
    expect(options).toEqual({
      parser: 'babel',
      sourcemap: false,
    });
  });

  it('should run prettier without sourcemap options', () => {
    const options = {
      parser: 'babel',
      sourcemap: false,
    };

    spyOn(prettier, 'format').and.callThrough();

    const code = 'var foo = 0;';
    const instance = rollupPluginPrettier(options);
    const chunk = {isEntry: false, imports: []};
    const outputOptions = {};
    instance.renderChunk(code, chunk, outputOptions);

    expect(prettier.format).toHaveBeenCalledWith(code, {
      parser: 'babel',
    });

    expect(options).toEqual({
      parser: 'babel',
      sourcemap: false,
    });
  });

  it('should run prettier without sourcemap options and custom other options', () => {
    const options = {
      parser: 'babel',
      sourcemap: false,
      singleQuote: true,
    };

    spyOn(prettier, 'format').and.callThrough();

    const code = 'var foo = 0;';
    const instance = rollupPluginPrettier(options);
    const chunk = {isEntry: false, imports: []};
    const outputOptions = {};
    instance.renderChunk(code, chunk, outputOptions);

    expect(prettier.format).toHaveBeenCalledWith(code, {
      parser: 'babel',
      singleQuote: true,
    });

    expect(options).toEqual({
      parser: 'babel',
      sourcemap: false,
      singleQuote: true,
    });
  });

  it('should run prettier using config file from given current working directory', () => {
    const cwd = path.join(__dirname, 'fixtures');
    const parser = 'babel';
    const options = {
      parser,
      cwd,
    };

    spyOn(prettier, 'format').and.callThrough();

    const instance = rollupPluginPrettier(options);
    const code = 'var foo = 0;';
    const chunk = {isEntry: false, imports: []};
    const outputOptions = {};
    instance.renderChunk(code, chunk, outputOptions);

    expect(options).toEqual({
      parser,
      cwd,
    });

    expect(prettier.format).toHaveBeenCalledWith(code, {
      parser,
      singleQuote: true,
      tabWidth: 2,
    });
  });
});
