CodeceptJS HTTP Helper
codeceptjs-http is an abandoned CodeceptJS helper that wraps the `then-request` library to facilitate making HTTP requests within CodeceptJS tests. It offers functionality to send requests and validate JSON responses against schemas. The package, currently at version 0.0.7, was last updated over six years ago and is not actively maintained, making it potentially incompatible with newer versions of Node.js or CodeceptJS. Its primary differentiation was providing a more flexible request management alternative to other HTTP helpers at the time, integrating directly into the CodeceptJS `I` object for test interactions.
Common errors
-
TypeError: I.sendRequest is not a function
cause The `HTTP` helper has not been correctly configured or enabled in the CodeceptJS configuration file (`codecept.conf.js` or `codecept.json`).fixAdd the `HTTP` helper to your `codecept.conf.js` under the `helpers` section with `require: 'codeceptjs-http'` and ensure CodeceptJS is loading this configuration. -
Error: Cannot find module 'then-request'
cause The package's primary dependency, `then-request`, or other peer dependencies were not installed correctly.fixRun `npm install codeceptjs-http` or ensure `then-request`, `chai`, and `chai-json-schema` are listed in your project's `package.json` and then run `npm install`. -
Error: ENOENT: no such file or directory, open '.../schemas/yourSchema.json'
cause The specified JSON schema file for `I.seeHttpResponseHasValidJsonSchema` could not be found. The helper expects schemas to be in a `schemas` directory relative to the CodeceptJS project root.fixCreate a `schemas` directory at your project root and place `yourSchema.json` inside it, or verify the path provided to `seeHttpResponseHasValidJsonSchema` is correct relative to this `schemas` folder.
Warnings
- breaking This package is abandoned and unmaintained. It was last updated over six years ago (v0.0.7) and is highly likely to be incompatible with modern Node.js versions (e.g., Node.js 16+), newer CodeceptJS releases, or other contemporary libraries. Expect installation failures, runtime errors, and security vulnerabilities due to outdated dependencies.
- gotcha Functionality is accessed via the global CodeceptJS `I` object. Direct `import` or `require` statements for methods like `sendRequest` will not work and are incorrect for this helper type.
- gotcha JSON schema files for `seeHttpResponseHasValidJsonSchema` are resolved relative to a 'schemas' folder located at the root of your CodeceptJS project (e.g., `path.join(global.codecept_dir, './schemas/')`). Misplacing schema files will result in 'file not found' errors.
Install
-
npm install codeceptjs-http -
yarn add codeceptjs-http -
pnpm add codeceptjs-http
Imports
- HTTP Helper Configuration
import { HTTP } from 'codeceptjs-http';{ "helpers": { "HTTP" : { "require": "codeceptjs-http", "endpoint": "http://localhost:8080" } } } - I.sendRequest
import { sendRequest } from 'codeceptjs-http'; sendRequest(...);I.sendRequest('/users', 'POST', { json: { name: 'Alice' } }); - I.seeHttpResponseHasValidJsonSchema
import { seeHttpResponseHasValidJsonSchema } from 'codeceptjs-http';I.seeHttpResponseHasValidJsonSchema('userSchema.json');
Quickstart
const { I } = inject();
Feature('API Tests');
BeforeSuite(() => {
// Assume a server is running on http://localhost:3000
});
Scenario('Verify user creation and retrieve by ID', async () => {
const userData = { name: 'John Doe', email: 'john.doe@example.com' };
const createResponse = await I.sendRequest('/users', 'POST', {
headers: { 'Content-Type': 'application/json' },
json: userData
});
// Assuming the API returns the created user with an ID
const createdUser = createResponse.body;
I.expect(createResponse.statusCode).to.eql(201);
I.expect(createdUser).to.have.property('id');
I.seeHttpResponseHasValidJsonSchema('schemas/userCreateResponse.json', null, createdUser);
const userId = createdUser.id;
const getResponse = await I.sendRequest(`/users/${userId}`, 'GET');
I.expect(getResponse.statusCode).to.eql(200);
I.expect(getResponse.body.name).to.eql(userData.name);
I.seeHttpResponseHasValidJsonSchema('schemas/userGetResponse.json', null, getResponse.body);
});
// Minimal codecept.conf.js for this quickstart:
// {
// "helpers": {
// "HTTP" : {
// "require": "codeceptjs-http",
// "endpoint": "http://localhost:3000"
// },
// "ChaiWrapper": {
// "require": "codeceptjs-chai"
// }
// }
// }