@stacksjs/testing 0.70.88 → 0.70.90
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/feature.d.ts +46 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +2 -0
- package/package.json +3 -3
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export declare function setupTestEnvironment(): void;
|
|
2
|
+
export declare function featureTest(baseUrl?: string): FeatureTestClient;
|
|
3
|
+
/**
|
|
4
|
+
* Lightweight feature-test helper. Returns a fluent client that wraps
|
|
5
|
+
* the request flow (`actingAs`, `json`, `assertStatus`, etc.) so test
|
|
6
|
+
* files don't have to re-implement boilerplate around `serverResponse`.
|
|
7
|
+
*
|
|
8
|
+
* Tests that need raw `fetch`-style access can still call the underlying
|
|
9
|
+
* server entrypoint directly — the helper is purely additive.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { featureTest, refreshDatabase } from '@stacksjs/testing'
|
|
14
|
+
*
|
|
15
|
+
* test('create post', async () => {
|
|
16
|
+
* await refreshDatabase()
|
|
17
|
+
* const user = await User.create({ email: 'a@b.com' })
|
|
18
|
+
*
|
|
19
|
+
* const res = await featureTest()
|
|
20
|
+
* .actingAs(user)
|
|
21
|
+
* .post('/api/posts', { title: 'hello' })
|
|
22
|
+
*
|
|
23
|
+
* res.assertStatus(201)
|
|
24
|
+
* const body = await res.json<{ id: number, title: string }>()
|
|
25
|
+
* expect(body.title).toBe('hello')
|
|
26
|
+
* })
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare interface FeatureTestResponse {
|
|
30
|
+
status: number
|
|
31
|
+
headers: Headers
|
|
32
|
+
text: () => Promise<string>
|
|
33
|
+
json: <T = unknown>() => Promise<T>
|
|
34
|
+
assertStatus: (expected: number) => FeatureTestResponse
|
|
35
|
+
assertJson: <T extends Record<string, unknown>>(partial: T) => Promise<FeatureTestResponse>
|
|
36
|
+
assertHeader: (name: string, expected?: string | RegExp) => FeatureTestResponse
|
|
37
|
+
}
|
|
38
|
+
export declare interface FeatureTestClient {
|
|
39
|
+
actingAs: (user: { id: number | string, [k: string]: unknown }) => FeatureTestClient
|
|
40
|
+
withHeaders: (headers: Record<string, string>) => FeatureTestClient
|
|
41
|
+
get: (path: string) => Promise<FeatureTestResponse>
|
|
42
|
+
post: (path: string, body?: unknown) => Promise<FeatureTestResponse>
|
|
43
|
+
put: (path: string, body?: unknown) => Promise<FeatureTestResponse>
|
|
44
|
+
patch: (path: string, body?: unknown) => Promise<FeatureTestResponse>
|
|
45
|
+
delete: (path: string, body?: unknown) => Promise<FeatureTestResponse>
|
|
46
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Lightweight test surface — `bun:test` re-exports + `setupTestEnvironment`.
|
|
2
|
+
//
|
|
3
|
+
// The database fixture helpers (`setupDatabase`, `refreshDatabase`,
|
|
4
|
+
// `truncateMysql`, …) used to be re-exported from this root entry too,
|
|
5
|
+
// but importing them eagerly drags in `@stacksjs/database`, which has
|
|
6
|
+
// a self-import cycle through its `./drivers/*` re-exports that
|
|
7
|
+
// deadlocks bun's module loader (60s @ 99% CPU) when imported outside
|
|
8
|
+
// the framework's preloader context (e.g. by `bun test`'s preload).
|
|
9
|
+
//
|
|
10
|
+
// The vast majority of tests don't need the database fixtures — they
|
|
11
|
+
// just want `describe / test / expect / beforeAll / afterAll` and
|
|
12
|
+
// `setupTestEnvironment()` to flip APP_ENV. So the database helpers
|
|
13
|
+
// are now opt-in via the package's `./database` subpath export
|
|
14
|
+
// (already declared in package.json's `exports`):
|
|
15
|
+
//
|
|
16
|
+
// import { setupDatabase, refreshDatabase } from '@stacksjs/testing/database'
|
|
17
|
+
//
|
|
18
|
+
// Tests that don't need them get a fast, hang-free import from
|
|
19
|
+
// '@stacksjs/testing'.
|
|
20
|
+
export * from './feature';
|
|
21
|
+
export * from 'bun:test';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
var W=import.meta.require;import K from"process";function N(){K.env.NODE_ENV="test",K.env.APP_ENV="test"}async function Y(){return(await import("@stacksjs/router")).serverResponse}function Z(F){let G={status:F.status,headers:F.headers,text:()=>F.text(),json:async()=>await F.clone().json(),assertStatus(C){if(this.status!==C)throw Error(`Expected status ${C}, got ${this.status}`);return G},async assertJson(C){let D=await F.clone().json();for(let[B,I]of Object.entries(C))if(JSON.stringify(D[B])!==JSON.stringify(I))throw Error(`assertJson: expected ${B}=${JSON.stringify(I)}, got ${JSON.stringify(D[B])}`);return G},assertHeader(C,D){let B=F.headers.get(C);if(B===null)throw Error(`assertHeader: missing header '${C}'`);if(D!==void 0){if(!(typeof D==="string"?B===D:D.test(B)))throw Error(`assertHeader: expected ${C}=${String(D)}, got '${B}'`)}return G}};return G}function R(F="http://localhost"){let G=null,C={};function D(q){let z={Accept:"application/json",...C};if(q!==void 0)z["Content-Type"]="application/json";if(G)z["X-Test-Acting-User"]=JSON.stringify(G);return z}async function B(q,z,J){let L=await Y(),M=z.startsWith("http")?z:`${F}${z}`,O={method:q,headers:D(J),body:J===void 0?void 0:typeof J==="string"?J:JSON.stringify(J)},Q=await L(new Request(M,O));return Z(Q)}let I={actingAs(q){return G=q,I},withHeaders(q){return C={...C,...q},I},get:(q)=>B("GET",q),post:(q,z)=>B("POST",q,z),put:(q,z)=>B("PUT",q,z),patch:(q,z)=>B("PATCH",q,z),delete:(q,z)=>B("DELETE",q,z)};return I}export*from"bun:test";export{N as setupTestEnvironment,R as featureTest};
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@stacksjs/testing",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"sideEffects": false,
|
|
5
|
-
"version": "0.70.
|
|
5
|
+
"version": "0.70.90",
|
|
6
6
|
"description": "The Stacks way of testing.",
|
|
7
7
|
"author": "Chris Breuer",
|
|
8
8
|
"contributors": [
|
|
@@ -74,8 +74,8 @@
|
|
|
74
74
|
"@stacksjs/ts-cloud": "^0.7.17"
|
|
75
75
|
},
|
|
76
76
|
"devDependencies": {
|
|
77
|
-
"@stacksjs/config": "0.70.
|
|
78
|
-
"@stacksjs/database": "0.70.
|
|
77
|
+
"@stacksjs/config": "0.70.90",
|
|
78
|
+
"@stacksjs/database": "0.70.90",
|
|
79
79
|
"better-dx": "^0.2.16"
|
|
80
80
|
}
|
|
81
81
|
}
|