Skip to content

Commit

Permalink
Added tests for options
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimmykoki committed Jun 11, 2023
1 parent f801bb0 commit 8e05fd9
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions packages/modular-scripts/src/__tests__/options.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os from 'os';
import {
computeConcurrencyOption,
validateCompareOptions,
} from '../utils/options';

// Mocking process.exit and process.stderr.write for testing
const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {
throw new Error('Exit called');
});
const stderrSpy = jest.spyOn(process.stderr, 'write').mockImplementation();

describe('validateCompareOptions', () => {
afterEach(() => {
exitSpy.mockClear();
stderrSpy.mockClear();
});

it('should exit process when compareBranch is defined and changed is false', () => {
expect(() => validateCompareOptions('branch', false)).toThrow(
'Exit called',
);
expect(stderrSpy).toHaveBeenCalledWith(
"Option --compareBranch doesn't make sense without option --changed\n",
);
});

it('should not exit process when compareBranch is undefined', () => {
expect(() => validateCompareOptions(undefined, false)).not.toThrow();
expect(exitSpy).not.toHaveBeenCalled();
});
});

describe('computeConcurrencyOption', () => {
afterEach(() => {
exitSpy.mockClear();
stderrSpy.mockClear();
});

it('should return the number of CPUs when concurrencyLevel is undefined', () => {
const cpus = os.cpus().length;
const result = computeConcurrencyOption(undefined);
expect(result).toBe(cpus || 1);
});

it('should exit process when concurrencyLevel is not a valid number', () => {
expect(() => computeConcurrencyOption('invalid')).toThrow('Exit called');
expect(stderrSpy).toHaveBeenCalledWith(
'--currencyLevel must be a number greater or equal than 0. You specified "invalid" instead.',
);
});

it('should return parsed number when concurrencyLevel is a valid number', () => {
const result = computeConcurrencyOption('2');
expect(result).toBe(2);
});
});

0 comments on commit 8e05fd9

Please sign in to comment.