Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit d3353fd

Browse files
committed
test: Adds c8 and moves to 100% LoC coverage
1 parent 3184b52 commit d3353fd

7 files changed

+201
-5
lines changed

.c8rc.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"exclude": ["*.env.*", "*.config.*", "test/mocks/*"]
3+
}

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ dist/
1414
archive/
1515
.DS_Code
1616
*.Identifier
17-
node_modules
17+
node_modules
18+
19+
# https://github.com/avajs/ava/blob/main/docs/recipes/code-coverage.md
20+
coverage

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"build": "tsup",
1111
"clean": "shx rm -rf dist",
1212
"prepare": "husky install",
13-
"test": "ava",
13+
"test": "c8 ava",
1414
"typecheck": "tsc --noEmit",
1515
"yeet": "release-it"
1616
},
@@ -59,6 +59,7 @@
5959
"@commitlint/config-conventional": "^15.0.0",
6060
"@types/node": "^18.15.11",
6161
"ava": "^4.3.0",
62+
"c8": "^7.13.0",
6263
"cross-fetch": "^3.1.5",
6364
"dotenv": "^16.0.3",
6465
"esbuild": "0.17.16",

pnpm-lock.yaml

+119
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class MongoClient {
105105
this.fetch = customFetch?.fetch ?? globalThis.fetch;
106106
const HeaderClass = customFetch?.Headers ?? globalThis.Headers;
107107

108-
if (!this.fetch || !HeaderClass) {
108+
if (!this.fetch || typeof this.fetch !== "function" || !HeaderClass) {
109109
throw new Error(
110110
"No viable fetch() found. Please provide a fetch interface"
111111
);
@@ -445,7 +445,7 @@ export class Collection<TSchema = Document> {
445445
try {
446446
errorText = ((await response.json()) as { error: string })?.error;
447447
} catch {
448-
// ignore failed parsing of response text
448+
/* c8 ignore next */
449449
}
450450

451451
const fallbackMessage = {

test/configure.spec.ts

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import test from "ava";
2+
import fetch, { Request, Response, Headers } from "cross-fetch";
3+
import { MongoClient } from "../src/client.js";
4+
import { BASE_URL } from "./mocks/handlers.js";
5+
6+
const fetchInterface = { fetch, Request, Response, Headers };
7+
8+
test("requires a fetch interface", (t) => {
9+
t.throws(() => {
10+
const c = new MongoClient({
11+
endpoint: BASE_URL,
12+
dataSource: "test-datasource",
13+
auth: { apiKey: "validApiKey" },
14+
// @ts-expect-error intentionally knocking out the fetch interface
15+
fetch: "not a fetch interface",
16+
});
17+
});
18+
});
19+
20+
test("requires a valid auth method", (t) => {
21+
t.throws(() => {
22+
const c = new MongoClient({
23+
endpoint: BASE_URL,
24+
dataSource: "test-datasource",
25+
// @ts-expect-error bypassing type check to throw error on bad auth method
26+
auth: { notValid: "not a valid auth method" },
27+
fetch: fetchInterface,
28+
});
29+
});
30+
});
31+
32+
test("accepts an api key for auth", (t) => {
33+
const c = new MongoClient({
34+
endpoint: BASE_URL,
35+
dataSource: "test-datasource",
36+
auth: { apiKey: "validApiKey" },
37+
fetch: fetchInterface,
38+
});
39+
t.pass();
40+
});
41+
42+
test("accepts a jwt token for auth", (t) => {
43+
const c = new MongoClient({
44+
endpoint: BASE_URL,
45+
dataSource: "test-datasource",
46+
auth: { jwtTokenString: "passed through to mongo api" },
47+
fetch: fetchInterface,
48+
});
49+
t.pass();
50+
});
51+
52+
test("accepts an email and password for auth", (t) => {
53+
const c = new MongoClient({
54+
endpoint: BASE_URL,
55+
dataSource: "test-datasource",
56+
auth: { email: "[email protected]", password: "validPassword" },
57+
fetch: fetchInterface,
58+
});
59+
t.pass();
60+
});
61+
62+
test("accepts a bearer token for auth", (t) => {
63+
const c = new MongoClient({
64+
endpoint: BASE_URL,
65+
dataSource: "test-datasource",
66+
auth: { bearerToken: "passed through to mongo api" },
67+
fetch: fetchInterface,
68+
});
69+
t.pass();
70+
});

test/requests.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ test("auth: bad auth results in 401 from Mongo", async (t) => {
255255
const { data, error } = await c
256256
.db("test-db")
257257
.collection<TestDocument>("test-collection")
258-
.findOne({ _id: new ObjectId("6193504e1be4ab27791c8133") });
258+
.find({ _id: new ObjectId("6193504e1be4ab27791c8133") });
259259

260260
t.is(data, undefined);
261261
t.truthy(error instanceof MongoDataAPIError);

0 commit comments

Comments
 (0)