@cslug/testing 0.3.0 → 0.4.1
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/package.json +15 -15
- package/src/fakes.ts +24 -8
- package/src/peers.d.ts +16 -0
- package/src/request.ts +2 -1
- package/src/test-app.ts +8 -9
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cslug/testing",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Test kit: testApp(), HTTP and HTML assertions, database isolation, fakes, time travel.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -28,20 +28,20 @@
|
|
|
28
28
|
"bun": ">=1.3.14"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@cslug/core": "0.
|
|
32
|
-
"@cslug/validation": "0.
|
|
33
|
-
"@cslug/http": "0.
|
|
34
|
-
"@cslug/vue": "0.
|
|
35
|
-
"@cslug/database": "0.
|
|
36
|
-
"@cslug/redis": "0.
|
|
37
|
-
"@cslug/queue": "0.
|
|
38
|
-
"@cslug/auth": "0.
|
|
39
|
-
"@cslug/mail": "0.
|
|
40
|
-
"@cslug/notification": "0.
|
|
41
|
-
"@cslug/storage": "0.
|
|
42
|
-
"@cslug/webhook": "0.
|
|
43
|
-
"@cslug/ai": "0.
|
|
44
|
-
"@cslug/cli": "0.
|
|
31
|
+
"@cslug/core": "0.4.1",
|
|
32
|
+
"@cslug/validation": "0.4.1",
|
|
33
|
+
"@cslug/http": "0.4.1",
|
|
34
|
+
"@cslug/vue": "0.4.1",
|
|
35
|
+
"@cslug/database": "0.4.1",
|
|
36
|
+
"@cslug/redis": "0.4.1",
|
|
37
|
+
"@cslug/queue": "0.4.1",
|
|
38
|
+
"@cslug/auth": "0.4.1",
|
|
39
|
+
"@cslug/mail": "0.4.1",
|
|
40
|
+
"@cslug/notification": "0.4.1",
|
|
41
|
+
"@cslug/storage": "0.4.1",
|
|
42
|
+
"@cslug/webhook": "0.4.1",
|
|
43
|
+
"@cslug/ai": "0.4.1",
|
|
44
|
+
"@cslug/cli": "0.4.1"
|
|
45
45
|
},
|
|
46
46
|
"peerDependenciesMeta": {
|
|
47
47
|
"@cslug/validation": {
|
package/src/fakes.ts
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
import { expect } from 'bun:test'
|
|
2
2
|
import { type AnyEventClass, type Application, EventBus, type EventClass, Logger } from '@cslug/core'
|
|
3
|
+
import type {
|
|
4
|
+
MailFake,
|
|
5
|
+
MailModule,
|
|
6
|
+
NotificationFake,
|
|
7
|
+
NotificationModule,
|
|
8
|
+
QueueFake,
|
|
9
|
+
QueueModule,
|
|
10
|
+
StorageFake,
|
|
11
|
+
StorageModule,
|
|
12
|
+
} from './peers'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Import an optional package by name. The specifier is a variable, so an app that does not install the package
|
|
16
|
+
* still type-checks; the types come from `peers.d.ts`.
|
|
17
|
+
*/
|
|
18
|
+
export const importOptional = <M>(name: string): Promise<M> => import(name)
|
|
3
19
|
|
|
4
20
|
/** Fakes available as `t.<name>`; packages add theirs with module augmentation and `registerFake`. */
|
|
5
21
|
// biome-ignore lint/suspicious/noEmptyInterface: augmented by packages
|
|
@@ -16,10 +32,10 @@ export const fakeInstaller = (name: string) => installers.get(name)
|
|
|
16
32
|
|
|
17
33
|
/** Fakes of optional packages, loaded on first use (the package may not be installed). */
|
|
18
34
|
const lazy: Record<string, () => Promise<FakeInstaller>> = {
|
|
19
|
-
queue: async () => (await
|
|
20
|
-
mail: async () => (await
|
|
21
|
-
storage: async () => (await
|
|
22
|
-
notify: async () => (await
|
|
35
|
+
queue: async () => (await importOptional<QueueModule>('@cslug/queue')).installQueueFake,
|
|
36
|
+
mail: async () => (await importOptional<MailModule>('@cslug/mail')).installMailFake,
|
|
37
|
+
storage: async () => (await importOptional<StorageModule>('@cslug/storage')).installStorageFake,
|
|
38
|
+
notify: async () => (await importOptional<NotificationModule>('@cslug/notification')).installNotificationFake,
|
|
23
39
|
}
|
|
24
40
|
|
|
25
41
|
/** The installer for `name`: registered, or loaded from its package. Undefined if neither. */
|
|
@@ -80,9 +96,9 @@ registerFake('events', (app) => {
|
|
|
80
96
|
declare module './fakes' {
|
|
81
97
|
interface TestFakes {
|
|
82
98
|
events: EventFake
|
|
83
|
-
queue:
|
|
84
|
-
mail:
|
|
85
|
-
storage:
|
|
86
|
-
notify:
|
|
99
|
+
queue: QueueFake
|
|
100
|
+
mail: MailFake
|
|
101
|
+
storage: StorageFake
|
|
102
|
+
notify: NotificationFake
|
|
87
103
|
}
|
|
88
104
|
}
|
package/src/peers.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types of the optional packages @cslug/testing works with. A `.d.ts` on purpose: apps type-check with
|
|
3
|
+
* `skipLibCheck`, so when one of them is not installed (an API without @cslug/queue, a web app without mail) its
|
|
4
|
+
* types become `any` instead of a "cannot find module" error. Installed, they are the real types.
|
|
5
|
+
*/
|
|
6
|
+
export type DatabaseModule = typeof import('@cslug/database')
|
|
7
|
+
export type HttpModule = typeof import('@cslug/http')
|
|
8
|
+
export type QueueModule = typeof import('@cslug/queue')
|
|
9
|
+
export type MailModule = typeof import('@cslug/mail')
|
|
10
|
+
export type StorageModule = typeof import('@cslug/storage')
|
|
11
|
+
export type NotificationModule = typeof import('@cslug/notification')
|
|
12
|
+
export type QueueFake = import('@cslug/queue').QueueFake
|
|
13
|
+
export type ScheduleRun = import('@cslug/queue').ScheduleRun
|
|
14
|
+
export type MailFake = import('@cslug/mail').MailFake
|
|
15
|
+
export type StorageFake = import('@cslug/storage').StorageFake
|
|
16
|
+
export type NotificationFake = import('@cslug/notification').NotificationFake
|
package/src/request.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { expect } from 'bun:test'
|
|
2
2
|
import type { Application } from '@cslug/core'
|
|
3
3
|
import { SessionStore } from '@cslug/core'
|
|
4
|
+
import { importOptional } from './fakes'
|
|
4
5
|
|
|
5
6
|
type Assertion = (res: TestResponse) => void | Promise<void>
|
|
6
7
|
export type Handler = (request: Request) => Promise<Response>
|
|
@@ -410,7 +411,7 @@ type VueHooks = {
|
|
|
410
411
|
let vue: Promise<VueHooks | undefined> | undefined
|
|
411
412
|
/** @cslug/vue's view hook, if installed. */
|
|
412
413
|
const vueHooks = () => {
|
|
413
|
-
vue ??=
|
|
414
|
+
vue ??= importOptional<VueHooks>('@cslug/vue').catch(() => undefined)
|
|
414
415
|
return vue
|
|
415
416
|
}
|
|
416
417
|
|
package/src/test-app.ts
CHANGED
|
@@ -16,7 +16,8 @@ import {
|
|
|
16
16
|
SessionStore,
|
|
17
17
|
type Token,
|
|
18
18
|
} from '@cslug/core'
|
|
19
|
-
import { loadFake, type TestFakes } from './fakes'
|
|
19
|
+
import { importOptional, loadFake, type TestFakes } from './fakes'
|
|
20
|
+
import type { DatabaseModule, HttpModule, QueueModule, ScheduleRun } from './peers'
|
|
20
21
|
import { CookieJar, type Handler, PendingRequest, readSession, testSession } from './request'
|
|
21
22
|
|
|
22
23
|
type Db = {
|
|
@@ -26,7 +27,6 @@ type Db = {
|
|
|
26
27
|
pin(): Promise<{ release(): Promise<void> }>
|
|
27
28
|
pool: { unsafe(text: string): Promise<unknown> }
|
|
28
29
|
}
|
|
29
|
-
type DatabaseModule = typeof import('@cslug/database')
|
|
30
30
|
|
|
31
31
|
export type TestAppOptions = {
|
|
32
32
|
/** Build the app; default: `createApp()` from `<cwd>/bootstrap/app.ts`. */
|
|
@@ -79,7 +79,7 @@ export type TestApp = {
|
|
|
79
79
|
}
|
|
80
80
|
/** The scheduler (needs @cslug/queue's ScheduleProvider): `runDue()` runs what is due at the fake clock's time. */
|
|
81
81
|
readonly schedule: {
|
|
82
|
-
runDue(): Promise<
|
|
82
|
+
runDue(): Promise<ScheduleRun[]>
|
|
83
83
|
}
|
|
84
84
|
assertDatabaseHas(table: string, where: Record<string, unknown>): Promise<void>
|
|
85
85
|
assertDatabaseMissing(table: string, where: Record<string, unknown>): Promise<void>
|
|
@@ -132,7 +132,7 @@ export function testApp<const F extends readonly string[] = []>(
|
|
|
132
132
|
for (const [path, value] of Object.entries(options.config ?? {})) app.setConfig(path, value)
|
|
133
133
|
await app.start()
|
|
134
134
|
try {
|
|
135
|
-
dbModule = await
|
|
135
|
+
dbModule = await importOptional<DatabaseModule>('@cslug/database')
|
|
136
136
|
if (app.has(dbModule.Database) && app.config('database.url')) db = app.resolve(dbModule.Database) as unknown as Db
|
|
137
137
|
} catch {
|
|
138
138
|
// @cslug/database not installed
|
|
@@ -144,7 +144,7 @@ export function testApp<const F extends readonly string[] = []>(
|
|
|
144
144
|
}).migrate()
|
|
145
145
|
}
|
|
146
146
|
try {
|
|
147
|
-
const http = await
|
|
147
|
+
const http = await importOptional<HttpModule>('@cslug/http')
|
|
148
148
|
if (app.has(http.HttpKernel)) {
|
|
149
149
|
const kernel = app.resolve(http.HttpKernel)
|
|
150
150
|
handle = (req) => kernel.handle(req, { ip: '127.0.0.1' })
|
|
@@ -260,10 +260,10 @@ export function testApp<const F extends readonly string[] = []>(
|
|
|
260
260
|
schedule: {
|
|
261
261
|
async runDue() {
|
|
262
262
|
const a = need()
|
|
263
|
-
const { Scheduler } = await
|
|
263
|
+
const { Scheduler } = await importOptional<QueueModule>('@cslug/queue')
|
|
264
264
|
if (!a.has(Scheduler)) throw new Error('testApp(): t.schedule needs ScheduleProvider (@cslug/queue)')
|
|
265
265
|
// no once-per-tick lock: tests replay the same tick
|
|
266
|
-
return a.resolve(Scheduler).runDue(clock.now(), { exclusive: false })
|
|
266
|
+
return a.resolve<InstanceType<QueueModule['Scheduler']>>(Scheduler).runDue(clock.now(), { exclusive: false })
|
|
267
267
|
},
|
|
268
268
|
},
|
|
269
269
|
db: {
|
|
@@ -331,10 +331,9 @@ type AuthModule = {
|
|
|
331
331
|
): Promise<{ plainText: string }>
|
|
332
332
|
}>
|
|
333
333
|
}
|
|
334
|
-
const AUTH_PACKAGE: string = '@cslug/auth'
|
|
335
334
|
const authModule = async (): Promise<AuthModule> => {
|
|
336
335
|
try {
|
|
337
|
-
return
|
|
336
|
+
return await importOptional<AuthModule>('@cslug/auth')
|
|
338
337
|
} catch {
|
|
339
338
|
throw new Error('testApp(): t.auth and t.tokens need @cslug/auth')
|
|
340
339
|
}
|