woocommerce/packages/js/api-core-tests
rodelgc 541abd9178
REP: Post daily smoke test report on Slack (#33203)
* Separate job for api test

* Remove unnecessary comment

* Use ubuntu 20.04 in job "publish-test-report"

* Simplify syntax in "if" condition

* Temporarily add push trigger for testing

* Temporarily disable running jest e2e tests for testing

* Remove unnecessary OR operator

* try: disable certain jobs for testing

* try: run 'hello' api test for testing

* Implement timeout override in API tests

* Allow workflow to override timeout of api tests

* Correct parsing of timeout override

* try: temporarily disable e2e job

* try: make api test verbose for troubleshooting

* try: temporarily disable some install steps for testing

* Correct smoke test env vars

* try: run all api tests

* Re-enable install-build steps

* Correct smoke test env var values

* Temporarily disable running all e2e tests after WC upgrade

* Remove unnecessary flag for daily smoke test runs

* Use different workflow for publishing daily smoke test report

* Use env vars instead of github expressions in accessing job outputs

* Correct multiline syntax

* Correct api_artifact input value

* Take only the first line of commit message

* Correct passing of input value

* Re-enable jobs before submitting for review

* Separate job for api test

* Remove unnecessary comment

* Use ubuntu 20.04 in job "publish-test-report"

* Simplify syntax in "if" condition

* Temporarily add push trigger for testing

* Temporarily disable running jest e2e tests for testing

* Remove unnecessary OR operator

* try: disable certain jobs for testing

* try: run 'hello' api test for testing

* Implement timeout override in API tests

* Allow workflow to override timeout of api tests

* Correct parsing of timeout override

* try: temporarily disable e2e job

* try: make api test verbose for troubleshooting

* try: temporarily disable some install steps for testing

* Correct smoke test env vars

* try: run all api tests

* Re-enable install-build steps

* Correct smoke test env var values

* Temporarily disable running all e2e tests after WC upgrade

* Remove unnecessary flag for daily smoke test runs

* Use different workflow for publishing daily smoke test report

* Use env vars instead of github expressions in accessing job outputs

* Correct multiline syntax

* Correct api_artifact input value

* Take only the first line of commit message

* Correct passing of input value

* Re-enable jobs before submitting for review

* Add push trigger for final testing

* Re-enable e2e tests, remove push trigger before merge

* Fix spacing

* Correct slack channel id
2022-05-31 12:57:04 -06:00
..
bin Publish API test reports to GitHub Pages 2022-05-16 17:59:40 +08:00
changelog Packages: Add Changelogger scripts (#32813) 2022-05-05 15:26:36 +12:00
data Standardize linting: e2e js packages (#32794) 2022-05-05 15:02:50 +12:00
endpoints Standardize linting: e2e js packages (#32794) 2022-05-05 15:02:50 +12:00
tests Standardize linting: e2e js packages (#32794) 2022-05-05 15:02:50 +12:00
utils Standardize linting: e2e js packages (#32794) 2022-05-05 15:02:50 +12:00
.env.example Update e2e package locations and add PNPM (#30977) 2021-10-29 08:32:31 +13:00
.eslintrc Publish API test reports to GitHub Pages 2022-05-16 17:59:40 +08:00
.eslintrc.js Standardize linting: e2e js packages (#32794) 2022-05-05 15:02:50 +12:00
.gitignore Ignore Allure directories globally 2022-05-20 15:08:01 +08:00
CHANGELOG.md Publish API test reports to GitHub Pages 2022-05-16 17:59:40 +08:00
NEXT_CHANGELOG.md js packages next changelogs 2021-11-11 16:34:50 +13:00
README.md Added documentation 2022-02-09 15:14:55 +08:00
allure.config.js Publish API test reports to GitHub Pages 2022-05-16 17:59:40 +08:00
composer.lock update to 3.0.2 of changelogger 2021-11-11 17:03:44 +13:00
jest.config.js REP: Post daily smoke test report on Slack (#33203) 2022-05-31 12:57:04 -06:00
package.json Removed Jetpack Changelogger from test js packages as they follow another approach for handling changelogs. (#32877) 2022-05-10 13:11:19 +12:00
project.json Add NX commands for Jetpack Changelogger (#31166) 2021-12-15 14:14:17 +13:00

README.md

WooCommerce Core API Test Suite

This package contains automated API tests for WooCommerce.

Environment variables

Before running the tests, the following environment variables need to be configured as shown in .env.example:

# Your site's base URL, not including a trailing slash
BASE_URL="https://mysite.com"

# The admin user's username or generated consumer key
USER_KEY=""

# The admin user's password or generated consumer secret
USER_SECRET=""

For local setup, create a .env file in this folder with the three required values described above.

Alternatively, these values can be passed in via the command line. For example:

BASE_URL=http://localhost:8084 USER_KEY=admin USER_SECRET=password npm run test:api

When using a username and password combination instead of a consumer secret and consumer key, make sure to have the JSON Basic Authentication plugin installed and activated on the test site.

For more information about authentication with the WooCommerce API, please see the Authentication section in the WooCommerce REST API documentation.

Optional variables

The following optional variables can be set in your local .env file:

  • VERBOSE: determine whether each individual test should be reported during the run.
  • USE_INDEX_PERMALINKS: determine whether to use index permalinks (?p=123) for the API route.

Running tests

Test API connection

To verify that everything is configured correctly, the following test script is available:

npm run test:hello

This tests connectivity to the API by validating connection to the following:

Run all tests

To run all of the API tests, you can use the following command:

npm run test:api

Running groups of tests

To run a specific group of tests, you can use the npm test -- --group= command and pass in the group's name you want to run.

For example, if you wanted to only run the orders API tests, you can use the following:

npm test -- --group=orders

Alternatively, you can use jest to run test groups:

jest --group=api

Writing tests

Conventions

  1. All tests should be placed in the tests directory.
  2. Always provide a JS doc in all tests, and tag them with @group. See the Test groups section for more info on grouping tests.
  3. Use functions in the data folder when generating test data instead of constructing them from scratch within the test.
  4. Use functions in the endpoints folder to send requests instead of directly using SuperTest's request() function.
  5. Use describe.each() or it.each() when writing repetitive tests.
  6. Always clean up all test data generated by the tests.

Test groups

This package makes use of the jest-runner-groups package, which allows grouping tests together around semantic groups (such as orders API calls, or coupons API calls) to make running test suites more granular.

Before the describe() statement, add in a doc block containing the desired groups:

/**
 * Tests for the WooCommerce API.
 *
 * @group api
 * @group endpoint
 *
 */
describe('', () => {
	it('', async () => {});
});

The api group should be included on all tests that should be run with the rest of the test suite. Groups can also contain a path, such as orders/delete.

For more information on how groups work, please refer to the jest-runner-groups documentation.

Using query strings

For tests that use query strings, these can be passed into the getRequest() method using an object of one or more key value pairs:

const { getRequest } = require('./utils/request');

const queryString = {
  dates_are_gmt: true,
  after: '2021-05-13T19:00:00',
  before: '2021-05-13T22:00:00'
};

const response = await getRequest('/orders', queryString);

Creating test data

Most of the time, test data would be in the form of a request payload. Instead of building them from scratch inside the test, create a test data file inside the data directory. Create a model of the request payload within that file, and export it as an object or a function that generates this object.

Afterwards, make sure to add the test data file to the data/index.js file.

This way, the test data would be decoupled from the test itself, allowing for easier test data management, and more readable tests.

Creating endpoint functions

All functions for sending requests to endpoints should be placed in the endpoints directory.

Newly created files should be added to the endpoints/index.js file.

Debugging tests

You can make use of the REST API log plugin to see how requests are being made, and check the request payload, response, and more.

REST API Log

Generate a Postman Collection

This package also allows generating a collection.json file using the test data in this package. This file can be imported into Postman and other REST clients that support the Postman v2 collection. To generate this file, run:

npm run make:collection

This will output a collection.json file in this directory.

Resources

This package makes use of the SuperTest HTTP assertion package. For more information on the response properties that are available can be found in the SuperAgent documentation.

For the list of WooCommerce API endpoints, expected responses, and more, please see the WooCommerce REST API Documentation.