@interaktiv/dia-scripts
Version:
CLI toolbox with common scripts for most sort of projects at DIA
256 lines (205 loc) • 7.7 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _path = _interopRequireDefault(require("path"));
var _fsExtra = _interopRequireDefault(require("fs-extra"));
var _tempy = _interopRequireDefault(require("tempy"));
var _serializers = require("./__tests__/helpers/serializers");
expect.addSnapshotSerializer(_serializers.unquoteSerializer);
expect.addSnapshotSerializer(_serializers.winPathSerializer);
jest.mock('read-pkg-up', () => ({
sync: jest.fn(() => ({
package: {
name: 'dia-scripts-test'
},
path: '/blah/package.json'
}))
}));
jest.mock('is-ci', () => false);
jest.mock('which', () => ({
sync: jest.fn(() => {})
}));
let originalArgv, originalEnv, originalExit, originalConsoleLog, readPkgUpSyncMock, testDirectory, consoleLogMock, whichSyncMock; // eslint-disable-next-line max-lines-per-function
describe('run init', () => {
beforeEach(() => {
readPkgUpSyncMock = require('read-pkg-up').sync;
whichSyncMock = require('which').sync;
whichSyncMock.mockImplementation((executable = '') => executable === 'dia-scripts' ? require.resolve('..') : jest.requireActual('which').sync(executable));
originalConsoleLog = console.log;
originalEnv = process.env;
originalArgv = process.argv;
originalExit = process.exit;
console.log = jest.fn();
process.exit = jest.fn();
consoleLogMock = console.log;
testDirectory = _tempy.default.directory();
process.env.INIT_CWD = testDirectory;
});
afterEach(() => {
console.log = originalConsoleLog;
process.exit = originalExit;
process.env = originalEnv;
process.argv = originalArgv;
if (testDirectory != null) {
_fsExtra.default.removeSync(testDirectory);
testDirectory = null;
}
jest.resetModules();
});
function mockPkg({
pkg = {
name: 'dia-scripts-test'
},
pkgPath = '/blah/package.json'
}) {
readPkgUpSyncMock.mockImplementationOnce(() => ({
package: pkg,
path: pkgPath
}));
}
const fromHere = (...p) => _path.default.join(__dirname, ...p);
const createFromTemplate = (template, dest) => _fsExtra.default.copySync(fromHere(`./__tests__/fixtures/init/${template}`), dest);
it.each`
title | params
${'should do nothing if running by self'} | ${{
pkg: {
name: '@interaktiv/dia-scripts'
},
template: 'self'
}}
${'should exclude build script for SFDX project'} | ${{
isSfdxProject: () => true,
expectedScripts: ['cm', 'format', 'lint', 'test', 'validate']
}}
${'should exclude build script for Titanium project'} | ${{
isTitaniumProject: () => true,
expectedScripts: ['cm', 'format', 'lint', 'test', 'validate']
}}
${'should exclude build script for Commerce project'} | ${{
isCommerceProject: () => true,
expectedScripts: ['cm', 'format', 'lint', 'test', 'validate']
}}
${'should backup existent .eslintrc.js and .prettierrc.js'} | ${{
template: 'config-files'
}}
`('$title', ({
params = {}
}) => {
const {
args = [],
pkg = {
name: 'dia-scripts-test'
},
pkgPath = _path.default.resolve(testDirectory, 'package.json'),
isSfdxProject = () => false,
isTitaniumProject = () => false,
isCommerceProject = () => false,
template = 'default',
expectedScripts = ['build', 'cm', 'format', 'lint', 'test', 'validate']
} = params;
mockPkg({
pkg,
pkgPath
});
const utils = require('../utils');
Object.assign(utils, {
isSfdxProject,
isTitaniumProject,
isCommerceProject
});
createFromTemplate(template, testDirectory);
process.argv = ['node', './init', ...args];
require('./init'); // If running self should skip
if (pkg.name === '@interaktiv/dia-scripts') {
const [, logCall] = consoleLogMock.mock.calls;
const [log] = logCall;
expect(log).toEqual(expect.stringMatching(/skipping/));
return;
} // Check available scripts
const {
scripts = {}
} = _fsExtra.default.readJsonSync(pkgPath);
const scriptNames = Object.keys(scripts);
expect(scriptNames).toHaveLength(expectedScripts.length);
expect(scriptNames.sort()).toEqual(expectedScripts.sort()); // Check config files backup
if (template === 'config-files') {
const eslintrcFile = _path.default.resolve(testDirectory, '.eslintrc.js');
const prettierrcFile = _path.default.resolve(testDirectory, '.prettierrc.js');
expect(_fsExtra.default.existsSync(eslintrcFile)).toBe(true);
expect(_fsExtra.default.existsSync(`${eslintrcFile}.old`)).toBe(true);
expect(_fsExtra.default.existsSync(prettierrcFile)).toBe(true);
expect(_fsExtra.default.existsSync(`${prettierrcFile}.old`)).toBe(true);
}
});
it('should copy over ignore files', () => {
createFromTemplate('default', testDirectory);
const pkgPath = _path.default.resolve(testDirectory, 'package.json');
mockPkg({
pkgPath
});
process.argv = ['node', './init', ...[]];
require('./init'); // Check ignore files
['.prettierignore', '.eslintignore', '.gitignore', '.npmignore'].filter(Boolean).map(f => _path.default.resolve(testDirectory, f)).forEach(f => expect(_fsExtra.default.existsSync(f)).toBe(true));
});
it('should copy over .forceignore if in SFDX project', () => {
createFromTemplate('default', testDirectory);
const pkgPath = _path.default.resolve(testDirectory, 'package.json');
mockPkg({
pkgPath
});
const utils = require('../utils');
Object.assign(utils, {
isSfdxProject: () => true,
ifSfdxProject: t => t
});
process.argv = ['node', './init', ...[]];
require('./init'); // Check ignore files
['.forceignore'].filter(Boolean).map(f => _path.default.resolve(testDirectory, f)).forEach(f => expect(_fsExtra.default.existsSync(f)).toBe(true));
});
it('should contain husky config in package.json', () => {
createFromTemplate('default', testDirectory);
const pkgPath = _path.default.resolve(testDirectory, 'package.json');
mockPkg({
pkgPath
});
process.argv = ['node', './init', ...[]];
require('./init');
const pkg = _fsExtra.default.readJSONSync(pkgPath);
expect(pkg).toEqual(expect.objectContaining({
husky: {
hooks: {
'prepare-commit-msg': expect.any(String),
'pre-commit': expect.any(String),
'commit-msg': expect.any(String)
}
}
}));
});
it('should resolve self correctly if running via lifecycle hook e.g. postinstall', () => {
createFromTemplate('default', testDirectory);
const pkgPath = _path.default.resolve(testDirectory, 'package.json');
mockPkg({
pkgPath
});
process.env.INIT_CWD = testDirectory;
process.argv = ['node', './init', ...[]];
require('./init');
const pkg = _fsExtra.default.readJSONSync(pkgPath);
expect(pkg).toEqual(expect.objectContaining({
husky: require('../config/huskyrc')
}));
});
it('should not run on CI', () => {
createFromTemplate('default', testDirectory);
const pkgPath = _path.default.resolve(testDirectory, 'package.json');
mockPkg({
pkgPath
});
jest.mock('is-ci', () => true);
process.argv = ['node', './init', ...[]];
require('./init');
expect(consoleLogMock).toHaveBeenCalledTimes(2);
const [, logCall] = consoleLogMock.mock.calls;
const [log] = logCall;
expect(log).toEqual(expect.stringMatching(/skipping/));
});
});