|
| 1 | +/* eslint-disable no-unused-expressions */ |
| 2 | +import { expect } from 'chai'; |
| 3 | +import sinon from 'sinon'; |
| 4 | +import { getPackageScriptCommands, makeCommand } from './cli'; |
| 5 | +import * as resolvePkg from './resolve-package'; |
| 6 | +import * as packageScript from './run-package-script'; |
| 7 | +import { Arguments } from 'yargs'; |
| 8 | + |
| 9 | +describe('cli', () => { |
| 10 | + describe('getPackageScriptCommands', async () => { |
| 11 | + afterEach(() => { |
| 12 | + sinon.restore(); |
| 13 | + }); |
| 14 | + const packageJson = { |
| 15 | + scripts: { |
| 16 | + first: 'do --this', |
| 17 | + second: 'do --that', |
| 18 | + third: 'do --all', |
| 19 | + }, |
| 20 | + }; |
| 21 | + |
| 22 | + it('should read scripts from package.json and return result with handlers', async () => { |
| 23 | + sinon.stub(resolvePkg, 'default').resolves(packageJson); |
| 24 | + |
| 25 | + const result = await getPackageScriptCommands(); |
| 26 | + const runScriptStub = sinon.stub(packageScript, 'default'); |
| 27 | + const mockArgs: Arguments = { |
| 28 | + _: ['arg1', 'arg2'], |
| 29 | + $0: '', |
| 30 | + }; |
| 31 | + |
| 32 | + expect(Object.keys(packageJson.scripts)).to.be.deep.equal(Object.keys(result)); |
| 33 | + for (const key of Object.keys(result)) { |
| 34 | + const expectedCommand = makeCommand(key); |
| 35 | + for (const field of Object.keys(expectedCommand)) { |
| 36 | + if (typeof expectedCommand[field] === 'function') { |
| 37 | + expectedCommand[field](mockArgs); |
| 38 | + expect(runScriptStub.called).to.be.true; |
| 39 | + } else { |
| 40 | + expect(result[key][field]).to.deep.equal(expectedCommand[field]); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + it('should return empty result when package.json contents are empty', async () => { |
| 47 | + const emptyPackage = {}; |
| 48 | + |
| 49 | + sinon.stub(resolvePkg, 'default').resolves(emptyPackage); |
| 50 | + |
| 51 | + const result = await getPackageScriptCommands(); |
| 52 | + |
| 53 | + expect(result).to.deep.equal(emptyPackage); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should ignore jss script entry', async () => { |
| 57 | + const packageJson = { |
| 58 | + scripts: { |
| 59 | + jss: 'do --this', |
| 60 | + second: 'do --that', |
| 61 | + third: 'do --all', |
| 62 | + }, |
| 63 | + }; |
| 64 | + const { jss: _, ...expectedScripts } = packageJson.scripts; |
| 65 | + |
| 66 | + sinon.stub(resolvePkg, 'default').resolves(packageJson); |
| 67 | + |
| 68 | + const result = await getPackageScriptCommands(); |
| 69 | + |
| 70 | + expect(Object.keys(expectedScripts)).to.be.deep.equal(Object.keys(result)); |
| 71 | + }); |
| 72 | + }); |
| 73 | +}); |
0 commit comments