Running Tests with `run test`

Scenario: You need to ensure the functionality and reliability of your trackjs/javascript-gameshow project. To do this, you’ll use the run test command to execute tests and view the results. In this example, we’ll go through the process of setting up and running tests for the project.

First, let’s take a look at the project structure:

audience-app/
presenter-app/
tests/

The audience-app and presenter-app directories contain the source code for the audience and presenter applications, respectively. The tests directory is where we’ll write our tests.

Now, let’s see how to write and run tests using Jest, which is the testing framework used in this project.

  1. Create a test file

Create a new file named GameController.test.tsx inside the tests directory. This file will contain the tests for the GameController class.

touch tests/GameController.test.tsx
  1. Write tests

Open the GameController.test.tsx file and write some tests for the getPrizesWon method of the GameController class.

import { DateTime } from 'luxon';
import { Game, GameController } from '../../src/controllers/GameController';

describe("GameController", () => {
describe("getPrizesWon()", () => {
it("returns no prizes for no correct answers", () => {
let game = getTestGame();
game.questionsAsked = [
{
questionIdx: 0,
questionId: "1234",
answerId: "1",
isCorrect: false,
},
];
expect(GameController.getPrizesWon(game)).toMatchObject([]);
});

// Add more tests here...
});
});

function getTestGame(): Game {
// Implement the getTestGame function here...
}
  1. Install dependencies

Make sure you have Jest and its dependencies installed. Run the following command in the project root directory:

npm install jest @types/jest
  1. Run tests

Now, you can run the tests using the run test command:

npm run test

Jest will search for test files in the tests directory and run them. The output will show the results of each test, including any failures or errors.

  1. Test results

If all tests pass, you’ll see a message similar to the following:

PASS  tests/GameController.test.tsx
GameController
✓ getPrizesWon() returns no prizes for no correct answers (1 ms)

Test Suites: 1 passed, 1 total
## Tests:       1 passed, 1 total

If any tests fail, you’ll see a message indicating which tests failed and the error message.

  1. Code coverage

To see the code coverage for your tests, you can use the --coverage flag when running the tests:

npm run test -- --coverage

The output will include a coverage report, which shows which parts of your code are covered by the tests.

  1. Continuous testing

You can also set up continuous testing using a CI/CD system like GitHub Actions or CircleCI. This will automatically run your tests whenever you push changes to your repository.

Tests to verify the answer:

  1. Write tests for all methods and functions in the GameController class.
  2. Write tests for other controllers, components, and utilities in the project.
  3. Ensure all tests pass when running npm run test.
  4. Check the code coverage report to make sure all lines of code are covered by tests.
  5. Set up continuous testing using a CI/CD system.