To run tests for the project, follow these steps:
Install Dependencies
Make sure to have the necessary dependencies installed in your project. You can do this by running:
npm install
Setup Testing Environment
The project utilizes Jest and Enzyme with the
enzyme-adapter-preact-pure
for testing. Ensure that the testing setup is properly configured by checking thesetupTests.ts
file which will configure Enzyme with the appropriate adapter.The setup might look like this:
import { configure } from 'enzyme'; import Adapter from 'enzyme-adapter-preact-pure'; configure({ adapter: new Adapter() });
This file should typically be located in
presenter-app/tests/__mocks__/setupTests.ts
.Executing Tests
To run your tests, execute the following command:
npm run test
This will invoke Jest to run all test files matching the pattern specified in your configuration. Ensure that your tests are structured in a way that Jest recognizes, typically ending with
.test.js
or.test.tsx
.Understanding Test Structure
When writing tests, each test suite is usually defined using
describe()
and individual test cases are represented byit()
. For instance, a typical test case might look like this: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([]); }); // Additional tests... }); });
Review Test Results
After running the tests, review the output in your console. Jest will provide details on the number of tests that passed, failed, and returned errors.
Code Coverage (Optional)
If you want to check the code coverage, you can add the following script to your
package.json
:"scripts": { "test:cov": "jest --coverage" }
Then run it using:
npm run test:cov
Ensure Compliance with Linting
Run ESLint to ensure that your TypeScript files maintain code quality. Execute:
npm run lint
Following these steps will enable you to run tests effectively, ensuring that all functionalities in the project are working as expected.
Sources: