1- const TCGdex = require ( "../src/tcgdex" ) . default
2- const fetch = require ( 'node-fetch' )
1+ import { expect , test , vi } from 'vitest'
2+ import TCGdex from '../src/tcgdex'
33
4- const fakeFetch = ( response , status = 200 ) => jest . fn ( ( ) =>
4+ // change timeout of execution
5+ vi . setConfig ( { testTimeout : 120000 } )
6+
7+ const fakeFetch = ( response , status = 200 ) => vi . fn ( ( ) =>
58 Promise . resolve ( {
69 status : status ,
710 json : ( ) => Promise . resolve ( response ) ,
811 } )
9- ) ;
10-
11-
12+ )
1213
1314test ( 'Basic test' , async ( ) => {
1415 const tcgdex = new TCGdex ( 'en' )
15- TCGdex . fetch = fakeFetch ( { ok : true } )
16+ TCGdex . fetch = fakeFetch ( { ok : true } ) as any
1617 const res = await tcgdex . fetch ( 'cards' , 'basic-test' )
17- expect ( res ) . toEqual ( { ok : true } )
18+ expect ( res ) . toEqual ( { ok : true } )
1819 expect ( TCGdex . fetch ) . toHaveBeenCalledTimes ( 1 )
1920} )
2021
2122test ( 'Cache test' , async ( ) => {
2223 const tcgdex = new TCGdex ( 'en' )
23- TCGdex . fetch = fakeFetch ( { ok : 'a' } )
24+ TCGdex . fetch = fakeFetch ( { ok : 'a' } ) as any
2425 const res1 = await tcgdex . fetch ( 'cards' , 'cache-test' )
25- expect ( res1 ) . toEqual ( { ok : 'a' } )
26- TCGdex . fetch = fakeFetch ( { ok : 'b' } )
26+ expect ( res1 ) . toEqual ( { ok : 'a' } )
27+ TCGdex . fetch = fakeFetch ( { ok : 'b' } ) as any
2728 const res2 = await tcgdex . fetch ( 'cards' , 'cache-test' )
28- expect ( res2 ) . toEqual ( { ok : 'a' } )
29+ expect ( res2 ) . toEqual ( { ok : 'a' } )
2930} )
3031
3132test ( 'endpoint errors' , async ( ) => {
3233 const tcgdex = new TCGdex ( 'en' )
33- TCGdex . fetch = fakeFetch ( { ok : 'a' } )
34+ TCGdex . fetch = fakeFetch ( { ok : 'a' } ) as any
3435 await expect ( tcgdex . fetch ( 'non existing endpoint' ) ) . rejects . toThrow ( )
3536 await expect ( tcgdex . fetch ( ) ) . rejects . toThrow ( )
3637} )
3738
3839test ( '404 test' , async ( ) => {
3940 const tcgdex = new TCGdex ( 'en' )
40- TCGdex . fetch = fakeFetch ( undefined , 404 )
41+ TCGdex . fetch = fakeFetch ( undefined , 404 ) as any
4142 expect (
4243 await tcgdex . fetch ( 'cards' , '404-test' )
4344 ) . not . toBeDefined ( )
@@ -47,15 +48,15 @@ test('test real endpoints', async () => {
4748 const tcgdex = new TCGdex ( 'en' )
4849 TCGdex . fetch = fetch
4950 const endpoints = [
50- { endpoint : 'fetchCard' , params : [ 'swsh1-1' ] } ,
51- { endpoint : 'fetchCard' , params : [ '1' , 'Sword & Shield' ] } ,
52- { endpoint : 'fetchCards' , params : [ 'swsh1' ] } ,
53- { endpoint : 'fetchCards' , params : [ ] } ,
54- { endpoint : 'fetchSet' , params : [ 'swsh1' ] } ,
55- { endpoint : 'fetchSets' , params : [ 'swsh' ] } ,
56- { endpoint : 'fetchSets' , params : [ ] } ,
57- { endpoint : 'fetchSeries' , params : [ ] } ,
58- { endpoint : 'fetchSerie' , params : [ 'swsh' ] } ,
51+ { endpoint : 'fetchCard' , params : [ 'swsh1-1' ] } ,
52+ { endpoint : 'fetchCard' , params : [ '1' , 'Sword & Shield' ] } ,
53+ { endpoint : 'fetchCards' , params : [ 'swsh1' ] } ,
54+ { endpoint : 'fetchCards' , params : [ ] } ,
55+ { endpoint : 'fetchSet' , params : [ 'swsh1' ] } ,
56+ { endpoint : 'fetchSets' , params : [ 'swsh' ] } ,
57+ { endpoint : 'fetchSets' , params : [ ] } ,
58+ { endpoint : 'fetchSeries' , params : [ ] } ,
59+ { endpoint : 'fetchSerie' , params : [ 'swsh' ] } ,
5960 ]
6061
6162 for await ( const item of endpoints ) {
0 commit comments