In this lab, you're going to use git bisect to locate a commit that introduced
a bug in your game's unit tests.
It looks like your unit tests aren't passing! You're not sure when the bug was introduced, but you know it wasn't there when you first created the game.
-
Start the
git bisectprocessgit bisect start
-
Indicate a commit where you know the code contains the change you're looking for
Omitting the commit hash will tell Git that the current commit is bad.
git bisect bad
-
Locate a commit where the code change is not present
git log --oneline
From the output of the above command, find a commit where the code change is not present and copy the commit hash. You can simply select the earliest commit in the logs (the one labeled
Initial commit). -
Indicate a commit where you know the code does not contain the change you're looking for
Replace
<sha>with the commit hash you found in the previous step.git bisect good <sha>
The git bisect command will check out the middle commit between the good and bad ones.
-
Check if the code change is present in the file
grep -w "expect(true).toBe(false)" __tests__/keyboard_input_manager.test.tsIf the code change is present, the
grepcommand will output the matching line(s).# Found $ grep -w "expect(true).toBe(false)" __tests__/keyboard_input_manager.test.ts expect(true).toBe(false); # Not found $ grep -w "expect(true).toBe(false)" __tests__/keyboard_input_manager.test.ts
-
If the change is present, tell
git bisectthat the current commit is badgit bisect bad
-
If the change is not present, tell
git bisectthat the current commit is goodgit bisect good
Git will locate and checkout the new mid point.
-
Repeat the previous steps until Git locates the first commit with the specified change.
When the process is complete, Git will output the commit that introduced the change.
53336f05d7e3d124ed872bbecd38c5cdbcca89be is the first bad commit commit 53336f05d7e3d124ed872bbecd38c5cdbcca89be (HEAD) Author: Nick Alteen <ncalteen@github.com> Date: Fri Sep 27 13:35:45 2024 -0400 Disable broken test __tests__/keyboard_input_manager.test.ts | 10 ++------- solutions/3-bisect/keyboard_input_manager.test.ts | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 8 deletions(-)
-
Finish the
git bisectprocessgit bisect reset
This will return you to the
HEADcommit.
-
Create a new feature branch
git checkout -b fix/unit-test
-
Locate the comment
// Lab 3: Git Bisect -
Add the following code below the comment
Make sure to replace
expect(true).toBe(false)with the correct test code.// Lab 3: Git Bisect const listen = jest .spyOn(KeyboardInputManager, 'listen') .mockImplementation(() => {}) new KeyboardInputManager() expect(KeyboardInputManager.events).toMatchObject({}) expect(listen).toHaveBeenCalledTimes(1)
-
Save the file
-
(Optional) Run the tests
If you have Node.js v22+ installed, you can run the tests with the following commands:
npm i && npm test
Now that you've fixed the broken test, you should commit your changes to your feature branch.
-
Open the terminal or command prompt
-
Add your changes to the staging area
git add __tests__/keyboard_input_manager.test.ts
-
Commit your changes
git commit -m "Fix broken test"
Now that you've added the feature, you should switch back to the main branch
so you can start working on something new!
-
Checkout the
mainbranchgit checkout main
Now that you're on the main branch, you should merge the fix/unit-test
branch into the main branch. That way, when your changes are pushed, the unit
tests will pass the next time the run as part of the continuous integration
process.
-
Merge the
fix/unit-testbranch into themainbranchgit merge fix/unit-test
-
Push your changes to GitHub
git push
If you're having trouble with any of the steps, you can ask for help in the meeting chat.
The code changes for this lab can be found in the solutions directory.
- Copy the contents of
solutions/3-git-bisect/keyboard_input_manager.test.tsand replace the contents of__tests__/keyboard_input_manager.test.ts