This repository is a template to create and run Jest test suites for P4SaMD. It enables you to write tests, execute them, and automatically send the results to a specified endpoint.
- Supports Jest for writing and running tests.
- Automatically exports test results in JUnit XML format.
- Sends test results to the P4SaMD backend.
- Node.js installed on your machine.
- An
.envfile with the following environment variables:P4SAMD_BACKEND_HOST: The base URL of the P4SaMD backend.P4SAMD_API_KEY: The secret key for authenticating with the P4SaMD backend.JOB_NAME: The name of the job (used in callback URL).
Tests should follow a structured hierarchy:
repository-root/
├── test-suite-1/
│ ├── test1.test.js
│ └── test2.test.js
├── test-suite-2/
│ ├── anotherTest.test.js
│ └── exampleTest.test.js
├── .env
├── package.json
├── run.sh
├── jest.config.js
└── test-results/
└── junit.xml
test/: Root directory for all test files.- Category Folders: Tests belonging to the same category must be placed in their respective folders.
- Test Files: Each test must be in a separate
.spec.jsfile.
.env: Environment variables file.run.sh: Script to run the tests and send results.test-results/: Directory where the test results (JUnit XML) are stored.
-
Clone the Repository
git clone <repository-url> cd <repository-name>
-
Install Dependencies
npm install
-
Set Up Environment Variables Create a
.envfile in the repository root with the following variables:P4SAMD_BACKEND_HOST=https://example.com P4SAMD_API_KEY=your_secret_api_key JOB_NAME=example-job
-
Write Your Tests Create your Jest tests under the
test/directory. Follow the folder structure described above.Example of a test file:
// test/category1/exampleTest.test.js describe('Example Test', () => { it('should pass this test', () => { expect(1 + 1).toBe(2); }); it('should fail this test', () => { expect(1 + 1).toBe(3); }); });
-
Run Tests Execute the
run.shscript:./run.sh
The script will:
- Run the tests.
- Generate a
junit.xmlfile in thetest-results/directory. - Send the results to the P4SaMD backend.
-
Check Test Results The results will be sent to the endpoint:
${P4SAMD_BACKEND_HOST}/tests-results/callback/${JOB_NAME}
- Ensure that every test file ends with
.spec.js. - Use meaningful folder and file names to categorize and describe the purpose of your tests.
- If the
test-results/junit.xmlfile is not found after running the tests, ensure that Jest is configured to generate JUnit XML output (this is typically done injest.config.js).