Installations
npm install supertest-fetch
Score
79.1
Supply Chain
100
Quality
77
Maintenance
100
Vulnerability
100
License
Developer
jwalton
Developer Guide
Module System
CommonJS
Min. Node Version
>=18.0.0
Typescript Support
Yes
Node Version
20.15.0
NPM Version
10.8.1
Statistics
41 Stars
198 Commits
7 Forks
3 Watching
9 Branches
8 Contributors
Updated on 10 Oct 2024
Bundle Size
6.93 kB
Minified
2.23 kB
Minified + Gzipped
Languages
TypeScript (98.29%)
JavaScript (1.71%)
Total Downloads
Cumulative downloads
Total Downloads
873,763
Last day
-43.9%
610
Compared to previous day
Last week
-16.9%
4,566
Compared to previous week
Last month
4.7%
19,803
Compared to previous month
Last year
-8%
221,469
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
supertest-fetch
A typescript friendly alternative to Supertest. Backed by native node fetch implementation so it requires node 18+ version.
What is it?
This is a library heavily influenced by Visionmedia's excellent supertest library, but with a WHATWG Fetch-like interface. The advantages of this library are:
- Uses native node Fetch API implementation (support since node v18.0.0).
- Should be instantly familiar to anyone who has used supertest.
- First class support for promises.
- Supertest has some weird quirks when used with Typescript becuase of @types/superagent.
Example
1import http from 'http'; 2import { makeFetch } from 'supertest-fetch'; 3 4const server = http.createServer((req, res) => { 5 res.setHeader('content-type', 'application/json'); 6 res.end(JSON.stringify({ greeting: 'Hello!' })); 7}); 8 9// This is a function with an API identical to the WHATWG `fetch()` function, 10// except the returned Promise has a bunch of supertest like functions on it. 11// 12// If the server is not listening, then `fetch()` will call `listen()` on the 13// server before each fetch, and close it after each fetch. 14const fetch = makeFetch(server); 15 16describe('my server tests', function () { 17 it('should return a response', async function () { 18 await fetch('/hello') 19 .expect(200) 20 .expect('content-type', 'application/json') 21 .expect({ greeting: 'Hello!' }); 22 }); 23 24 it('will work just like fetch if you need to do more advanced things', async function () { 25 const response = await fetch('/hello') 26 .expect(200) 27 .expect('content-type', 'application/json'); 28 29 expect(await response.json()).to.eql({ greeting: 'Hello!' }); 30 }); 31 32 it('should post data', async function () { 33 await fetch('/hello', { 34 method: 'post', 35 body: '<message>Hello</message>', 36 headers: { 'content-type': 'application/xml' }, 37 }); 38 }); 39});
API
makeFetch(server)
Returns a new fetch
function. This is identical to the WHAT-WG fetch function, except that the returned object has some extra assertions added to it.
If the server
passed in is not already listening, each call to fetch()
will call listen()
on the server, and close it after each request. This will assign a random free port to the server, so you don't need to worry about listening on a well-known port for your tests to work.
If the server
passed in is an instance of tls.Server, then the returned fetch
instance will use HTTPS to connect to the server instead of HTTP. Note that it's up to you to appropriately configure the server, supplying a certificate and key, and if you're using a self-signed certificate you'll need to pass an "agent" to the call to fetch
. See this example for details.
.expectStatus(statusCode[, statusText])
Verify response status code and text.
.expectHeader(headerName, value)
Verify headerName matches the given value or regex. If value
is null,
verifies that the header is not present.
.expectBody(body)
Verify body is the given string, JSON object, or matches the given regular expression.
.expect(statusCode[, fn])
Supertest friendly alias for .expectStatus(statusCode)
.
.expect(statusCode, body)
Supertest friendly alias for .expectStatus(statusCode).expectBody(body)
.
.expect(body)
Supertest friendly alias for .expectBody(body)
.
.expect(field, value)
Supertest friendly alias for .expectHeader(field, value)
.
.json()
Convenience function which returns a Promise which resolves to the JSON content of the response. This:
1const result = await fetch('/hello').expect(200).json();
is equivalent to:
1const response = await fetch('/hello').expect(200); 2const result = await response.json();
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
Found 2/7 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/github-ci.yaml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/jwalton/node-supertest-fetch/github-ci.yaml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/github-ci.yaml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/jwalton/node-supertest-fetch/github-ci.yaml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/github-ci.yaml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/jwalton/node-supertest-fetch/github-ci.yaml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/github-ci.yaml:28: update your workflow using https://app.stepsecurity.io/secureworkflow/jwalton/node-supertest-fetch/github-ci.yaml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/github-ci.yaml:30: update your workflow using https://app.stepsecurity.io/secureworkflow/jwalton/node-supertest-fetch/github-ci.yaml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/github-ci.yaml:17
- Warn: npmCommand not pinned by hash: .github/workflows/github-ci.yaml:34
- Info: 0 out of 4 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
- Info: 0 out of 2 npmCommand dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/github-ci.yaml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
license file not detected
Details
- Warn: project does not have a license file
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 25 are checked with a SAST tool
Score
3.3
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More