@@ -3,16 +3,61 @@ import type { Rstest, RstestUtilities } from '../../types';
3
3
export type { Assertion } from '../../types/expect' ;
4
4
export type { Mock } from '../../types/mock' ;
5
5
6
- export declare const expect : Rstest [ 'expect' ] ;
7
- export declare const assert : Rstest [ 'assert' ] ;
8
- export declare const it : Rstest [ 'it' ] ;
9
- export declare const test : Rstest [ 'test' ] ;
10
- export declare const describe : Rstest [ 'describe' ] ;
11
- export declare const beforeAll : Rstest [ 'beforeAll' ] ;
12
- export declare const afterAll : Rstest [ 'afterAll' ] ;
13
- export declare const beforeEach : Rstest [ 'beforeEach' ] ;
14
- export declare const afterEach : Rstest [ 'afterEach' ] ;
15
- export declare const rstest : RstestUtilities ;
16
- export declare const rs : RstestUtilities ;
17
- export declare const onTestFinished : Rstest [ 'onTestFinished' ] ;
18
- export declare const onTestFailed : Rstest [ 'onTestFailed' ] ;
6
+ declare global {
7
+ var RSTEST_API : Rstest | undefined ;
8
+ }
9
+
10
+ const check = ( name : keyof Rstest ) => {
11
+ if ( ! globalThis . RSTEST_API ?. [ name ] ) {
12
+ throw new Error (
13
+ `Rstest API '${ name } ' is not registered yet, please make sure you are running in a rstest environment.` ,
14
+ ) ;
15
+ }
16
+ } ;
17
+
18
+ const wrapRstestAPI = < T extends keyof Omit < Rstest , 'rstest' | 'rs' > > (
19
+ name : T ,
20
+ ) : Rstest [ T ] => {
21
+ const fn = ( ...args : Parameters < Rstest [ T ] > ) => {
22
+ check ( name ) ;
23
+ return globalThis . RSTEST_API ! [ name ] . call (
24
+ globalThis . RSTEST_API ! [ name ] ,
25
+ // @ts -expect-error
26
+ ...args ,
27
+ ) ;
28
+ } ;
29
+
30
+ return new Proxy ( fn , {
31
+ get ( _target , key , receiver ) {
32
+ check ( name ) ;
33
+ return Reflect . get ( globalThis . RSTEST_API ?. [ name ] || { } , key , receiver ) ;
34
+ } ,
35
+ } ) as Rstest [ T ] ;
36
+ } ;
37
+
38
+ const wrapRstestUtilitiesAPI = < T extends keyof Pick < Rstest , 'rstest' | 'rs' > > (
39
+ name : T ,
40
+ ) : Rstest [ T ] => {
41
+ return new Proxy ( { } as Rstest [ T ] , {
42
+ get ( _target , key , receiver ) {
43
+ check ( name ) ;
44
+ return Reflect . get ( globalThis . RSTEST_API ?. [ name ] || { } , key , receiver ) ;
45
+ } ,
46
+ } ) ;
47
+ } ;
48
+
49
+ export const expect : Rstest [ 'expect' ] = wrapRstestAPI ( 'expect' ) ;
50
+ export const assert : Rstest [ 'assert' ] = wrapRstestAPI ( 'assert' ) ;
51
+ export const it : Rstest [ 'it' ] = wrapRstestAPI ( 'it' ) ;
52
+ export const test : Rstest [ 'test' ] = wrapRstestAPI ( 'test' ) ;
53
+ export const describe : Rstest [ 'describe' ] = wrapRstestAPI ( 'describe' ) ;
54
+ export const beforeAll : Rstest [ 'beforeAll' ] = wrapRstestAPI ( 'beforeAll' ) ;
55
+ export const afterAll : Rstest [ 'afterAll' ] = wrapRstestAPI ( 'afterAll' ) ;
56
+ export const beforeEach : Rstest [ 'beforeEach' ] = wrapRstestAPI ( 'beforeEach' ) ;
57
+ export const afterEach : Rstest [ 'afterEach' ] = wrapRstestAPI ( 'afterEach' ) ;
58
+ export const rstest : RstestUtilities = wrapRstestUtilitiesAPI ( 'rstest' ) ;
59
+ export const rs : RstestUtilities = wrapRstestUtilitiesAPI ( 'rs' ) ;
60
+ export const onTestFinished : Rstest [ 'onTestFinished' ] =
61
+ wrapRstestAPI ( 'onTestFinished' ) ;
62
+ export const onTestFailed : Rstest [ 'onTestFailed' ] =
63
+ wrapRstestAPI ( 'onTestFailed' ) ;
0 commit comments