Registry / testing / trust-rest

trust-rest

JSON →
library2.1.0jsnpmunverified

Trust REST is a REST API testing library for Node.js (v2.1.0) that simplifies integration testing of RESTful services with a declarative API. Released under the MIT license, it provides a fluent interface for making HTTP requests and asserting responses, including support for JSON, headers, status codes, and body validation. Compared to alternatives like supertest, trust-rest focuses on lightweight syntax and minimal configuration. The library is stable with infrequent releases, primarily driven by bug fixes and minor feature additions. It supports both CommonJS and ES module systems, but users must ensure proper require/import usage to avoid runtime errors.

npm install trust-rest
INSTALL
IMPORT
SIG · TRUST-REST
T
trust-rest
testingjavascriptv2.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

trust
import trust from 'trust-rest'
const trust = require('trust-rest')
Default export with CommonJS require works but import preferred.
Trust
import { Trust } from 'trust-rest'
const { Trust } = require('trust-rest')
Named export for class types; available since v2.
default
const trust = require('trust-rest')
const { trust } = require('trust-rest')
CommonJS require yields the default export directly, not as part of an object.

Demonstrates basic GET and POST requests with status and header assertions using trust-rest.

import trust from 'trust-rest'; const api = trust('https://jsonplaceholder.typicode.com'); // Test GET endpoint const getResponse = await api .get('/posts/1') .expect(200) .expect('Content-Type', /json/) .end(); console.log('Response body:', getResponse.body); // Test POST endpoint const postResponse = await api .post('/posts') .send({ title: 'foo', body: 'bar', userId: 1 }) .expect(201) .expect(res => res.body.title === 'foo') .end(); console.log('Created post ID:', postResponse.body.id);
Debug
Known issues
gotchaUsing wrong import style: import { trust } instead of import trust
fix
Use default import: import trust from 'trust-rest'
affects: >=2.0.0
gotchaCalling .end() twice throws error
fix
Assign result to variable and avoid calling .end() again
affects: >=1.0.0
deprecatedSupport for Node.js <10 is dropped
fix
Upgrade Node.js to version 10 or later
affects: >=2.0.0
breakingAPI changed: trust() no longer accepts a base URL as optional parameter in v2
fix
Always pass base URL: trust('http://example.com')
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: trust is not a function
Incorrect import or require style leading to undefined
fix
Use correct import: import trust from 'trust-rest' or const trust = require('trust-rest')
Error: Can't set headers after they are sent
Calling .end() twice on same request
fix
Store response in variable and use that variable
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies
superagentrequiredused for making HTTP requests in tests
Agent activity
6 hits · last 30 days
node
6
Resources
trust-rest — npm install trust-rest · libregistry