Skip to content

Commit 77e85dc

Browse files
committed
✅ Better tests coverage
1 parent 3a1f7b8 commit 77e85dc

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

src/core.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@ export interface Wretch<Self = unknown, Chain = unknown> {
3232
*/
3333
addon: <W, R>(addon: WretchAddon<W, R>) => W & Self & Wretch<Self & W, Chain & R>
3434

35-
/**
36-
* Sets the default fetch options used for every subsequent fetch call.
37-
* @param options - New default options
38-
* @param replace - If true, replaces the existing options instead of mixing in
39-
*/
40-
defaults(this: Self & Wretch<Self, Chain>, options: WretchOptions, replace?: boolean): this
41-
4235
/**
4336
* Sets the method (text, json ...) used to parse the data contained in the response body in case of an HTTP error.
4437
*
@@ -206,19 +199,15 @@ export const core: Wretch = {
206199
}
207200
},
208201

209-
defaults(options, replace = false) {
202+
errorType(errorType: string) {
210203
return this.clone({
211204
config: {
212205
...this._config,
213-
defaults: replace ? options : mix(this._config.defaults, options)
206+
errorType
214207
}
215208
})
216209
},
217210

218-
errorType(errorType: string) {
219-
return this.clone({ errorType })
220-
},
221-
222211
polyfills(polyfills, replace = false) {
223212
return this.clone({
224213
config: {

test/node/wretch.spec.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ const duckImage = fs.readFileSync(duckImagePath)
5050

5151
describe("Wretch", function () {
5252

53+
beforeEach(() => {
54+
wretch.defaults({}, true)
55+
wretch.errorType("text")
56+
})
57+
5358
it("should set and use non global polyfills", async function () {
5459
global["FormData"] = null
5560
global["URLSearchParams"] = null
@@ -73,7 +78,7 @@ describe("Wretch", function () {
7378
performance,
7479
PerformanceObserver,
7580
AbortController
76-
})
81+
}, true)
7782
})
7883

7984
it("should perform crud requests and parse a text response", async function () {
@@ -384,7 +389,7 @@ describe("Wretch", function () {
384389
expect(check).toBe(3)
385390
})
386391

387-
it("should set default fetch options", async function () {
392+
it("should set global default fetch options", async function () {
388393
let rejected = await new Promise(res => wretch(`${_URL}/customHeaders`).get().badRequest(_ => {
389394
res(true)
390395
}).res(result => res(!result)))
@@ -458,19 +463,27 @@ describe("Wretch", function () {
458463
})
459464

460465
it("should change the parsing used in the default error handler", async function () {
466+
// Local
467+
await wretch(`${_URL}/json500`)
468+
.errorType("json")
469+
.get()
470+
.internalError(error => { expect(error.json).toEqual({ error: 500, message: "ok" }) })
471+
.res(_ => fail("I should never be called because an error was thrown"))
472+
.then(_ => expect(_).toBe(undefined))
473+
// Default (text)
461474
await wretch(`${_URL}/json500`)
462475
.get()
463476
.internalError(error => { expect(error.text).toEqual(`{"error":500,"message":"ok"}`) })
464477
.res(_ => fail("I should never be called because an error was thrown"))
465478
.then(_ => expect(_).toBe(undefined))
479+
// Global
466480
wretch.errorType("json")
467481
await wretch(`${_URL}/json500`)
468482
.get()
469483
.internalError(error => { expect(error.json).toEqual({ error: 500, message: "ok" }) })
470484
.res(_ => fail("I should never be called because an error was thrown"))
471485
.then(_ => expect(_).toBe(undefined))
472-
// Change back
473-
wretch.errorType("text")
486+
474487
})
475488

476489
it("should retrieve performance timings associated with a fetch request", function (done) {

0 commit comments

Comments
 (0)