1
1
import { afterEach , beforeAll , describe , expect , it , vi } from "vitest" ;
2
2
// @ts -expect-error
3
3
import createFetchMock from "vitest-fetch-mock" ;
4
- import createClient , { type Middleware } from "../src/index.js" ;
4
+ import createClient , {
5
+ type MiddlewareRequest ,
6
+ type Middleware ,
7
+ } from "../src/index.js" ;
5
8
import type { paths } from "./fixtures/v7-beta.js" ;
6
9
7
10
// Note
@@ -528,6 +531,8 @@ describe("client", () => {
528
531
} ) ;
529
532
530
533
it ( "can modify response" , async ( ) => {
534
+ const toUnix = ( date : string ) => new Date ( date ) . getTime ( ) ;
535
+
531
536
const rawBody = {
532
537
533
538
created_at : "2024-01-01T00:00:00Z" ,
@@ -541,10 +546,11 @@ describe("client", () => {
541
546
542
547
const client = createClient < paths > ( ) ;
543
548
client . use ( {
549
+ // convert date string to unix time
544
550
async onResponse ( res ) {
545
551
const body = await res . json ( ) ;
546
- body . created_at = new Date ( body . created_at ) . getTime ( ) ;
547
- body . updated_at = new Date ( body . updated_at ) . getTime ( ) ;
552
+ body . created_at = toUnix ( body . created_at ) ;
553
+ body . updated_at = toUnix ( body . updated_at ) ;
548
554
const headers = new Headers ( res . headers ) ;
549
555
headers . set ( "middleware" , "value" ) ;
550
556
return new Response ( JSON . stringify ( body ) , {
@@ -558,8 +564,8 @@ describe("client", () => {
558
564
const { data, response } = await client . GET ( "/self" ) ;
559
565
560
566
// assert body was modified
561
- expect ( data ?. created_at ) . toBe ( new Date ( rawBody . created_at ) . getTime ( ) ) ;
562
- expect ( data ?. updated_at ) . toBe ( new Date ( rawBody . updated_at ) . getTime ( ) ) ;
567
+ expect ( data ?. created_at ) . toBe ( toUnix ( rawBody . created_at ) ) ;
568
+ expect ( data ?. updated_at ) . toBe ( toUnix ( rawBody . updated_at ) ) ;
563
569
// assert rest of body was preserved
564
570
expect ( data ?. email ) . toBe ( rawBody . email ) ;
565
571
// assert status changed
@@ -700,6 +706,8 @@ describe("client", () => {
700
706
} ) ;
701
707
702
708
it ( "can be ejected" , async ( ) => {
709
+ mockFetchOnce ( { status : 200 , body : "{}" } ) ;
710
+
703
711
let called = false ;
704
712
const errorMiddleware = {
705
713
onRequest ( ) {
@@ -759,7 +767,8 @@ describe("client", () => {
759
767
760
768
// expect post_id to be encoded properly
761
769
const req = fetchMocker . mock . calls [ 0 ] [ 0 ] ;
762
- expect ( req . body ) . toBeInstanceOf ( FormData ) ;
770
+ // note: this is FormData, but Node.js doesn’t handle new Request() properly with formData bodies. So this is only in tests.
771
+ expect ( req . body ) . toBeInstanceOf ( Buffer ) ;
763
772
764
773
// TODO: `vitest-fetch-mock` does not add the boundary to the Content-Type header like browsers do, so we expect the header to be null instead
765
774
expect ( ( req . headers as Headers ) . get ( "Content-Type" ) ) . toBeNull ( ) ;
0 commit comments