woocommerce/packages/js/api-core-tests
Paul Sealock ffaef64343 add nx composer-install targets 2021-11-11 16:43:27 +13:00
..
bin Update e2e package locations and add PNPM (#30977) 2021-10-29 08:32:31 +13:00
data More missing files 2021-10-29 16:40:27 -06:00
endpoints More missing files 2021-10-29 16:40:27 -06:00
tests Add in missing refunds test 2021-10-29 16:43:45 -06:00
utils Update e2e package locations and add PNPM (#30977) 2021-10-29 08:32:31 +13:00
.env.example Update e2e package locations and add PNPM (#30977) 2021-10-29 08:32:31 +13:00
.gitignore remove duplicate gitignore entries 2021-11-08 11:03:19 -04:00
CHANGELOG.md Update e2e package locations and add PNPM (#30977) 2021-10-29 08:32:31 +13:00
NEXT_CHANGELOG.md js packages next changelogs 2021-11-11 16:34:50 +13:00
README.md Update e2e package locations and add PNPM (#30977) 2021-10-29 08:32:31 +13:00
composer.json point to next changelog 2021-11-11 16:38:38 +13:00
jest.config.js Update e2e package locations and add PNPM (#30977) 2021-10-29 08:32:31 +13:00
package.json Update e2e package locations and add PNPM (#30977) 2021-10-29 08:32:31 +13:00
project.json add nx composer-install targets 2021-11-11 16:43:27 +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

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);

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.