Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests for options #2409

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

@benpryke benpryke Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this test is so simple, it's hard to avoid reimplementing the function. I think you have 2 options:

  1. Include os.cpus().length || 1 in this variable so as to mimic the intended implementation as closely as possible (breaking this up increases the chance of bugs creeping in).
  2. Mock os.cpus() to return different values, e.g. test for 2 and undefined to cover more possibilities.

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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're actually missing a bug that exists in the implementation here.

It's important to test the boundary cases; in this case, there is a bug that needs fixing: the function will return 0 when '0' is passed when it actually should exit. This needs fixing and we need a test case to cover that. Note that the comment on line 37 of options.ts is also misleading and needs to be updated.

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);
});
});