Registry / testing / api-testing

api-testing

JSON →
library1.0.3jsnpmunverified

API-Testing is an open-source JavaScript library designed for conducting end-to-end integration tests against MediaWiki's Action API and REST API. It runs on Node.js environments (specifically Node.js >= 14.18.0) and is built upon established testing tools: `supertest` for making HTTP requests, `Chai` for its flexible assertion capabilities, and `Mocha` as the test runner. The current stable version is 1.7.3. This library provides a specialized framework for testing MediaWiki instances, whether locally installed or remote, offering a higher-level abstraction compared to using the underlying HTTP and assertion libraries directly. Its release cadence is tied to MediaWiki development and is actively maintained, as evidenced by its integration with Wikimedia's Gerrit and Phabricator for contributions and bug tracking. Key differentiators include its tight coupling with MediaWiki API structures, simplifying test authoring for that ecosystem, and its comprehensive integration test approach.

npm install api-testing
INSTALL
IMPORT
SIG · API-TESTING
A
api-testing
testingjavascriptv1.0.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createMediaWikiTestClient
import { createMediaWikiTestClient } from 'api-testing';
const createMediaWikiTestClient = require('api-testing').createMediaWikiTestClient;
This is the primary factory function to initialize and configure an API testing client instance for a MediaWiki endpoint. ESM import is preferred in modern Node.js environments.
MediaWikiActionApiClient
import { MediaWikiActionApiClient } from 'api-testing';
import MediaWikiActionApiClient from 'api-testing/action';
Represents the client for interacting with the MediaWiki Action API. It exposes chainable methods for constructing and sending API requests.
expectMediaWikiApiError
import { expectMediaWikiApiError } from 'api-testing';
expect(response).to.have.mediawikiError('code');
A utility function or custom Chai assertion for robustly checking common MediaWiki API error structures within test responses, streamlining error assertion logic.

Demonstrates how to set up `api-testing` with Mocha and Chai to test both MediaWiki Action and REST APIs, including success and error scenarios.

import { expect } from 'chai'; import 'mocha'; // Mocha's describe/it are globally available after importing 'mocha' import { createMediaWikiTestClient } from 'api-testing'; // Configure the client to point to your test MediaWiki instance. // For demonstration, we use a public wiki, but you'd typically use a local test wiki or a dedicated service. const wikiBaseUrl = process.env.MEDIAWIKI_BASE_URL ?? 'https://en.wikipedia.org'; // Create a client instance. This object handles making requests to the configured MediaWiki API. const apiTestClient = createMediaWikiTestClient(wikiBaseUrl); describe('MediaWiki Action API - Basic Site Info Tests', () => { it('should be able to get basic site information without authentication', async () => { // Use the Action API client to query for site info const response = await apiTestClient.action({ action: 'query', meta: 'siteinfo', format: 'json' }) .expectStatusCode(200) // Expect an HTTP 200 OK status .expectSuccess() // Expect a successful MediaWiki API response (no 'error' field) .send(); // Execute the API request expect(response.body.query.general.sitename).to.be.a('string').and.not.empty; expect(response.body.query.general.generator).to.match(/^MediaWiki/); expect(response.body.query.general.base).to.include(wikiBaseUrl); }).timeout(10000); // Set a higher timeout for network requests it('should gracefully handle requests for non-existent actions', async () => { const response = await apiTestClient.action({ action: 'nonExistentMediaWikiAction123', format: 'json' }) .expectStatusCode(200) // MediaWiki often returns 200 for API errors within the response body .expectFailure() // Expect a MediaWiki API failure (presence of 'error' field) .send(); expect(response.body.error.code).to.equal('apierror-unknown_action'); expect(response.body.error.info).to.include('Unrecognized parameter value for action'); }).timeout(5000); }); describe('MediaWiki REST API - Page Content Tests', () => { it('should retrieve the HTML content for the Main Page', async () => { // Use the REST API client for specific page content const response = await apiTestClient.rest(`/page/Main_Page/html`) // Example path for REST API .expectStatusCode(200) .send(); expect(response.text).to.be.a('string'); expect(response.text).to.include('<!DOCTYPE html>'); // Expect HTML content expect(response.text).to.include('Main Page'); }).timeout(10000); }); // To run this test file: // 1. Install dependencies: `npm install mocha chai api-testing` // 2. Ensure your package.json has `"type": "module"` if you're using ESM directly. // 3. Configure `MEDIAWIKI_BASE_URL` environment variable if targeting a specific wiki. // 4. Execute: `npx mocha path/to/your/test-file.ts` (if using TypeScript with ts-node) // or `npx mocha path/to/your/test-file.js` (for plain JavaScript or transpiled code).
Debug
Known issues
gotchaThis library requires Node.js version 14.18.0 or newer. Running with older Node.js versions may lead to unexpected errors or unsupported features.
fix
Upgrade your Node.js environment to version 14.18.0 or later. Consider using nvm (Node Version Manager) for easy switching.
affects: <14.18.0
gotchaAPI-Testing relies on a running MediaWiki instance for executing tests. Tests will fail if the configured `MEDIAWIKI_BASE_URL` is inaccessible or does not point to a valid MediaWiki API endpoint.
fix
Ensure a MediaWiki instance is running and accessible from your test environment. Configure the `MEDIAWIKI_BASE_URL` environment variable or directly pass the URL when initializing the client. For local testing, consider using Docker to spin up a MediaWiki container.
affects: >=1.0.0
gotchaAuthentication to MediaWiki APIs, especially for write actions or protected content, requires proper setup of user credentials (e.g., bot passwords, OAuth). Failure to configure authentication correctly will result in permission errors.
fix
Refer to the MediaWiki API documentation for specific authentication methods. Configure your test client with the necessary credentials (e.g., `apiTestClient.withCredentials('username', 'password')` if such methods are exposed).
affects: >=1.0.0
breakingStarting with a major version (e.g., v2.0.0 if it occurs), the library might transition to an ESM-only distribution, dropping CommonJS support. This would require changes to `require()` statements.
fix
Update all `require()` statements to `import` statements and ensure your `package.json` includes `"type": "module"` for Node.js projects, or use a bundler like Webpack/Rollup if targeting the browser (though this library is Node.js focused).
affects: future major versions (e.g., >=2.0.0)
Errors
Common errors & fixes
SyntaxError: Cannot use import statement outside a module
Attempting to use ES Modules (import/export) syntax in a Node.js environment configured for CommonJS, or without transpilation.
fix
Add `"type": "module"` to your `package.json` file, or rename your test file to `.mjs` to enable ESM support. Alternatively, if using CommonJS, convert `import` statements to `require()` (though `api-testing` might primarily offer ESM).
Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done()" is called or a Promise is returned.
A test took longer than the default or specified Mocha timeout, often due to slow network requests or a non-responsive MediaWiki API.
fix
Increase the timeout for the specific test or suite using `this.timeout(20000)` inside the test function, or globally via Mocha's `--timeout` option. Investigate network connectivity or the responsiveness of the MediaWiki instance.
TypeError: apiTestClient.action is not a function
The `apiTestClient` object was not correctly initialized, or the method name is incorrect/unavailable in the current version.
fix
Ensure `createMediaWikiTestClient` is imported correctly and that the client object is properly instantiated. Consult the library's documentation for the exact API method names available on the client instance.
Upgrade
Version history
1.0.3latest on npm
Audit
Dependencies
supertestrequiredUsed internally by api-testing for making HTTP requests; users will typically need it for advanced request customization.
chairequiredAssertion library commonly used alongside api-testing for writing test assertions.
mocharequiredThe primary testing framework expected for running tests written with api-testing.
Agent activity
35 hits · last 30 days
node
30
OpenAI (training)
1
Resources