Skip to content

test: add comprehensive unit tests for controllerUtils#3466

Open
muhammadrashid4587 wants to merge 1 commit intobluewave-labs:developfrom
muhammadrashid4587:test/add-controllerutils-tests
Open

test: add comprehensive unit tests for controllerUtils#3466
muhammadrashid4587 wants to merge 1 commit intobluewave-labs:developfrom
muhammadrashid4587:test/add-controllerutils-tests

Conversation

@muhammadrashid4587
Copy link
Copy Markdown

Summary

  • Adds unit tests for all 10 pure utility functions in controllerUtils.ts
  • Covers: requireString, optionalString, optionalNumber, optionalBoolean, parseSortOrder, requireTeamId, requireUserId, requireUserEmail, requireFirstName, requireUserRoles
  • Tests cover valid inputs, boundary values (empty strings, NaN, Infinity, whitespace), type coercion (string-to-number, string-to-boolean), and error conditions

Motivation

controllerUtils.ts contains validation functions used across every controller in the server, but had zero test coverage. These pure functions are ideal unit test candidates.

Test Plan

  • Run cd server && npm test to verify all tests pass
  • Verify no existing tests are broken

Add test coverage for all pure utility functions in controllerUtils:
requireString, optionalString, optionalNumber, optionalBoolean,
parseSortOrder, requireTeamId, requireUserId, requireUserEmail,
requireFirstName, and requireUserRoles.

Tests cover valid inputs, boundary values, type coercion, and
error conditions for each function.
Copy link
Copy Markdown
Collaborator

@ajhollid ajhollid left a comment

Choose a reason for hiding this comment

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

Decent set of tests for the most part. Some tightening up is needed and we're good to go here. Thanks!

optionalString,
optionalNumber,
optionalBoolean,
parseMonitorTypeFilter,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is imported but not tested

Comment on lines +215 to +218
it("returns the roles when valid", () => {
const roles = ["admin" as any];
expect(requireUserRoles(roles)).toEqual(roles);
});
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Casting to any defeats the purpose here. Essentially this says

const a = 1;
expect a = 1;

Yeah.. its true, but what's the point?


it("throws for boolean", () => {
expect(() => optionalNumber(true, "count")).toThrow("count must be a number");
});
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What about null? Let's add a test case for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants