Skip to content

Commit 2097df2

Browse files
authored
feat: add hub test env as well as supporting nuxt test option (#429)
1 parent 0430ff6 commit 2097df2

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

playground/test/basic.test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { fileURLToPath } from 'node:url'
2+
import { describe, it, expect } from 'vitest'
3+
import { setup, $fetch } from '@nuxt/test-utils/e2e'
4+
5+
describe('ssr', async () => {
6+
await setup({
7+
rootDir: fileURLToPath(new URL('..', import.meta.url)),
8+
dev: true
9+
})
10+
11+
it('Clear todos table', async () => {
12+
await $fetch('/api/_hub/database/query', {
13+
method: 'POST',
14+
body: {
15+
query: 'DELETE FROM todos'
16+
}
17+
})
18+
})
19+
20+
it('List todos', async () => {
21+
const todos = await $fetch('/api/todos')
22+
expect(todos).toMatchObject([])
23+
})
24+
25+
it('Create todo', async () => {
26+
const todo = await $fetch('/api/todos', {
27+
method: 'POST',
28+
body: { title: 'Test todo' }
29+
})
30+
expect(todo).toMatchObject({ id: expect.any(Number), title: 'Test todo' })
31+
})
32+
})

src/module.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ export default defineNuxtModule<ModuleOptions>({
3535
const rootDir = nuxt.options.rootDir
3636
const { resolve } = createResolver(import.meta.url)
3737

38-
let remoteArg = parseArgs(argv, { remote: { type: 'string' } }).remote as string
39-
remoteArg = (remoteArg === '' ? 'true' : remoteArg)
38+
const cliArgs = parseArgs(argv, {
39+
remote: { type: 'string' },
40+
hubEnv: { type: 'string' }
41+
})
42+
const remoteArg = cliArgs.remote === '' ? 'true' : cliArgs.remote
4043
const runtimeConfig = nuxt.options.runtimeConfig
4144
const databaseMigrationsDirs = nuxt.options._layers?.map(layer => join(layer.config.serverDir!, 'database/migrations')).filter(Boolean)
4245
const hub = defu(runtimeConfig.hub || {}, options, {
@@ -66,7 +69,7 @@ export default defineNuxtModule<ModuleOptions>({
6669
databaseQueriesPaths: [],
6770
// Other options
6871
version,
69-
env: process.env.NUXT_HUB_ENV || 'production',
72+
env: process.env.NUXT_HUB_ENV || (cliArgs.hubEnv as string) || 'production',
7073
openapi: nuxt.options.nitro.experimental?.openAPI === true,
7174
// Extra bindings for the project
7275
bindings: {
@@ -79,6 +82,18 @@ export default defineNuxtModule<ModuleOptions>({
7982
clientSecret: process.env.NUXT_HUB_CLOUDFLARE_ACCESS_CLIENT_SECRET || null
8083
}
8184
})
85+
if (!['test', 'preview', 'production'].includes(hub.env)) {
86+
log.error('Invalid hub environment, should be `test`, `preview` or `production`')
87+
process.exit(1)
88+
}
89+
// If testing environment detects, set the hub env to `test`
90+
if (nuxt.options.test) {
91+
hub.env = 'test'
92+
}
93+
if (hub.env === 'test') {
94+
log.info('NuxtHub test environment detected, using `test` dataset for all storage & disabling remote storage.')
95+
hub.remote = false
96+
}
8297
runtimeConfig.hub = hub
8398
runtimeConfig.public.hub = {}
8499
// Make sure to tell Nitro to not generate the wrangler.toml file

src/utils/wrangler.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ import type { HubConfig } from '../features'
66
// Please update nuxt-hub/cli preview command as well
77
export function generateWrangler(nuxt: Nuxt, hub: HubConfig) {
88
const wrangler: { [key: string]: any } = {}
9+
const name = hub.env === 'test' ? 'test' : 'default'
910

1011
if (hub.analytics && !hub.remote) {
1112
wrangler['analytics_engine_datasets'] = [{
1213
binding: 'ANALYTICS',
13-
dataset: 'default'
14+
dataset: name
1415
}]
1516
}
1617

1718
if (hub.blob && !hub.remote) {
1819
wrangler['r2_buckets'] = [{
1920
binding: 'BLOB',
20-
bucket_name: 'default'
21+
bucket_name: name
2122
}]
2223
}
2324

@@ -27,23 +28,23 @@ export function generateWrangler(nuxt: Nuxt, hub: HubConfig) {
2728
if (hub.kv && !hub.remote) {
2829
wrangler['kv_namespaces'].push({
2930
binding: 'KV',
30-
id: 'kv_default'
31+
id: `kv_${name}`
3132
})
3233
}
3334

3435
if (hub.cache) {
3536
wrangler['kv_namespaces'].push({
3637
binding: 'CACHE',
37-
id: 'cache_default'
38+
id: `cache_${name}`
3839
})
3940
}
4041
}
4142

4243
if (hub.database && !hub.remote) {
4344
wrangler['d1_databases'] = [{
4445
binding: 'DB',
45-
database_name: 'default',
46-
database_id: 'default'
46+
database_name: name,
47+
database_id: name
4748
}]
4849
}
4950

0 commit comments

Comments
 (0)