Skip to content

Modernize all test files to use vitest expect patterns #507

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

Merged
merged 4 commits into from
Jul 2, 2025

Conversation

mattgodbolt
Copy link
Owner

Summary

  • Modernized all 10 unit test files to use vitest expect assertions instead of node's assert module
  • Updated CLAUDE.md with guidance to use idiomatic vitest assertions in tests
  • All 237 unit tests continue to pass after conversion

Test Files Updated

  • test-gzip.js
  • test-utils.js
  • test-fifo.js
  • test-bcd.js
  • test-scheduler.js
  • test-tokenise.js
  • test-disc-drive.js
  • test-intel-fdc.js
  • test-disc.js
  • test-disc-hfe.js

Key Changes

  • assert.strictEqual()expect().toBe()
  • assert.deepStrictEqual()expect().toEqual()
  • assert.throws()expect().toThrow()
  • assert()expect().toBe(true) or expect().toBeTruthy()
  • assert(\!expr)expect().toBe(false) or expect().toBeFalsy()
  • Fixed type mismatches (Buffer vs Uint8Array comparisons)
  • Used expect().toBeFalsy() for null/undefined checks where appropriate

Test Plan

  • All 237 unit tests pass
  • ESLint passes with no errors
  • Code follows existing vitest patterns used elsewhere in the project

🤖 Generated with Claude Code

mattgodbolt and others added 3 commits July 1, 2025 21:30
- Replace assert with expect from vitest
- Use path.join instead of string concatenation for paths
- Update all assertions to use vitest patterns:
  - assert.strictEqual() → expect().toBe()
  - assert.deepStrictEqual() → expect().toEqual()
  - assert.throws() → expect().toThrow()
  - assert() → expect().toBeTruthy()

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
…est expect

- Replace assert with expect from vitest
- Update all assertions to use vitest patterns:
  - assert.strictEqual() → expect().toBe()
  - assert.deepStrictEqual() → expect().toEqual()

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Replace assert with expect from vitest
- Update all assertions to use vitest patterns:
  - assert.strictEqual() → expect().toBe()
  - assert.equal() → expect().toBe()
  - assert() → expect().toBeTruthy()
  - assert(!x) → expect(x).toBe(false)
- Use toBeFalsy() for drive.disc check to better capture intent
  (original assert.equal(undefined, null) passed due to loose equality)
- Add guidance in CLAUDE.md to use idiomatic vitest assertions

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@mattgodbolt mattgodbolt requested a review from Copilot July 2, 2025 04:41
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR modernizes all unit tests to use Vitest’s expect API instead of Node’s assert module, ensuring consistency with existing Vitest patterns and updating documentation accordingly.

  • Updated test imports to include expect and removed the Node assert import.
  • Replaced assert.* calls with expect(...).toBe() or .toEqual() as appropriate.
  • Added a note in CLAUDE.md encouraging idiomatic Vitest assertions.

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/unit/test-utils.js Swapped assert calls for expect and adjusted import
tests/unit/test-tokenise.js Replaced assert with expect and updated import
tests/unit/test-scheduler.js Converted assert checks to expect calls
tests/unit/test-intel-fdc.js Changed assert.equal to expect().toBe()
tests/unit/test-gzip.js Updated file-path handling and switched to expect
tests/unit/test-fifo.js Swapped assert calls for expect
tests/unit/test-disc-drive.js Replaced assertions with expect, used expect.fail
tests/unit/test-bcd.js Updated assert.strictEqual to expect syntax
CLAUDE.md Added guidance to use Vitest expect assertions

assert(arr instanceof Uint8Array);
assert.strictEqual(arr.length, str.length);
assert.strictEqual(uint8ArrayToString(arr), str);
expect(arr instanceof Uint8Array).toBeTruthy();
Copy link
Preview

Copilot AI Jul 2, 2025

Choose a reason for hiding this comment

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

Consider using the toBeInstanceOf matcher for clarity: expect(arr).toBeInstanceOf(Uint8Array).

Suggested change
expect(arr instanceof Uint8Array).toBeTruthy();
expect(arr).toBeInstanceOf(Uint8Array);

Copilot uses AI. Check for mistakes.

assert.equal(drive.disc, null);
assert(!drive.spinning);
expect(drive.trackLength).toBe(IbmDiscFormat.bytesPerTrack);
expect(drive.disc).toBeFalsy();
Copy link
Preview

Copilot AI Jul 2, 2025

Choose a reason for hiding this comment

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

Since drive.disc is expected to be null, you can use the more precise matcher toBeNull(): expect(drive.disc).toBeNull().

Suggested change
expect(drive.disc).toBeFalsy();
expect(drive.disc).toBeNull();

Copilot uses AI. Check for mistakes.

@@ -21,9 +20,9 @@ describe("Tokeniser", function () {
const t = await tokeniser;
t.tokenise(text);
console.log("Failed to give exception with message:", expectedError);
assert.strictEqual(false, true);
expect(false).toBe(true);
Copy link
Preview

Copilot AI Jul 2, 2025

Choose a reason for hiding this comment

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

Use expect.fail() for unreachable branches instead of expect(false).toBe(true) to improve test readability.

Suggested change
expect(false).toBe(true);
expect.fail("Unreachable code executed: expected an exception.");

Copilot uses AI. Check for mistakes.

- Use toBeInstanceOf() for cleaner instanceof checks in test-utils.js
- Use expect.fail() instead of expect(false).toBe(true) in test-tokenise.js
- Keep toBeFalsy() for drive.disc check (not toBeNull) since actual value is undefined

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@mattgodbolt mattgodbolt merged commit 6e317d0 into main Jul 2, 2025
2 checks passed
@mattgodbolt mattgodbolt deleted the claude/modernize-test-patterns branch July 2, 2025 04:46
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.

1 participant