@interaktiv/dia-scripts
Version:
CLI toolbox with common scripts for most sort of projects at DIA
146 lines (132 loc) • 4.83 kB
JavaScript
;
var _serializers = require("../__tests__/helpers/serializers");
expect.addSnapshotSerializer(_serializers.unquoteSerializer);
expect.addSnapshotSerializer(_serializers.winPathSerializer);
expect.addSnapshotSerializer(_serializers.relativePathSerializer);
let mockIsCI = false;
let originalArgv = process.argv;
let originalExit = process.exit;
let crossSpawnSyncMock, prevCI;
describe('lint javascript', () => {
beforeEach(() => {
jest.mock('is-ci', () => mockIsCI);
({
sync: crossSpawnSyncMock
} = require('cross-spawn'));
originalArgv = process.argv;
originalExit = process.exit;
prevCI = mockIsCI;
process.exit = jest.fn();
});
afterEach(() => {
process.exit = originalExit;
process.argv = originalArgv;
mockIsCI = prevCI;
jest.resetModules();
});
it.each`
title | params
${'calls eslint CLI with default args'} | ${{}}
${'does not use built-in config with --config'} | ${{
args: ['--config', './custom-config.js']
}}
${'does not use built-in config with .eslintrc.json file'} | ${{
hasFile: filename => filename === '.eslintrc.json'
}}
${'does not use built-in config with .eslintrc.js file'} | ${{
hasFile: filename => filename === '.eslintrc.js'
}}
${'does not use built-in config with eslintConfig pkg prop'} | ${{
hasPkgProp: prop => prop === 'eslintConfig'
}}
${'does not use built-in ignore with --ignore-path'} | ${{
args: ['--ignore-path', './my-ignore']
}}
${'does not use built-in ignore with .eslintignore file'} | ${{
hasFile: filename => filename === '.eslintignore'
}}
${'does not use built-in ignore with eslintIgnore pkg prop'} | ${{
hasPkgProp: prop => prop === 'eslintIgnore'
}}
${'will disable caching with --no-cache'} | ${{
args: ['--no-cache']
}}
${'will not show warnings with --quiet argument'} | ${{
args: ['--quiet']
}}
${'runs on given files, but only js files'} | ${{
args: ['./src/index.js', './package.json', './src/index.css', './src/component.js']
}}
${'will not show warnings on CI'} | ${{
ci: true
}}
${'should ignore other eslintrc files if in SFDX project'} | ${{
isSfDx: true
}}
`('$title', ({
params = {}
}) => {
const utils = require('../../utils');
const {
args = [],
hasPkgProp = () => false,
hasFile = () => false,
isSfDx = false,
ci = false
} = params;
mockIsCI = ci; // Before each
Object.assign(utils, {
hasPkgProp,
hasFile,
resolveBin: (modName, {
executable = modName
} = {}) => executable,
isSfdxProject: () => Boolean(isSfDx),
ifSfdxProject: (t, f) => isSfDx ? t : f
});
process.argv = ['node', '../../lint/javascript', ...args];
require('./javascript');
expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1);
const [firstCall] = crossSpawnSyncMock.mock.calls;
const [script, calledArgs] = firstCall;
expect([script, ...calledArgs].join(' ')).toMatchSnapshot();
});
it('should not ignore other .eslintrc files in SFDX project when eslint pkg config given', () => {
const utils = require('../../utils');
mockIsCI = false; // Before each
Object.assign(utils, {
hasPkgProp: prop => prop === 'eslintConfig',
resolveBin: (modName, {
executable = modName
} = {}) => executable,
isSfdxProject: () => true,
ifSfdxProject: t => t
});
process.argv = ['node', '../../lint/javascript'];
require('./javascript');
expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1);
const [firstCall] = crossSpawnSyncMock.mock.calls;
const [, calledArgs] = firstCall;
expect(calledArgs).not.toEqual(expect.arrayContaining(['--no-eslintrc']));
});
it('should ignore other .eslintrc files in SFDX project when .eslintrc.json given', () => {
const utils = require('../../utils');
mockIsCI = false; // Before each
Object.assign(utils, {
hasPkgProp: () => false,
resolveBin: (modName, {
executable = modName
} = {}) => executable,
isSfdxProject: () => true,
ifSfdxProject: t => t,
hasFile: (file = '') => file.includes('.eslintrc.json'),
ifFile: (file, t, f) => (file || '').includes('.eslintrc.json') ? t : f
});
process.argv = ['node', '../../lint/javascript'];
require('./javascript');
expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1);
const [firstCall] = crossSpawnSyncMock.mock.calls;
const [, calledArgs] = firstCall;
expect(calledArgs).toEqual(expect.arrayContaining(['--no-eslintrc']));
});
});