Skip to content

Commit

Permalink
feat: add hub test env as well as supporting nuxt test option (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux authored Jan 22, 2025
1 parent 0430ff6 commit 2097df2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
32 changes: 32 additions & 0 deletions playground/test/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { fileURLToPath } from 'node:url'
import { describe, it, expect } from 'vitest'
import { setup, $fetch } from '@nuxt/test-utils/e2e'

describe('ssr', async () => {
await setup({
rootDir: fileURLToPath(new URL('..', import.meta.url)),
dev: true
})

it('Clear todos table', async () => {
await $fetch('/api/_hub/database/query', {
method: 'POST',
body: {
query: 'DELETE FROM todos'
}
})
})

it('List todos', async () => {
const todos = await $fetch('/api/todos')
expect(todos).toMatchObject([])
})

it('Create todo', async () => {
const todo = await $fetch('/api/todos', {
method: 'POST',
body: { title: 'Test todo' }
})
expect(todo).toMatchObject({ id: expect.any(Number), title: 'Test todo' })
})
})
21 changes: 18 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ export default defineNuxtModule<ModuleOptions>({
const rootDir = nuxt.options.rootDir
const { resolve } = createResolver(import.meta.url)

let remoteArg = parseArgs(argv, { remote: { type: 'string' } }).remote as string
remoteArg = (remoteArg === '' ? 'true' : remoteArg)
const cliArgs = parseArgs(argv, {
remote: { type: 'string' },
hubEnv: { type: 'string' }
})
const remoteArg = cliArgs.remote === '' ? 'true' : cliArgs.remote
const runtimeConfig = nuxt.options.runtimeConfig
const databaseMigrationsDirs = nuxt.options._layers?.map(layer => join(layer.config.serverDir!, 'database/migrations')).filter(Boolean)
const hub = defu(runtimeConfig.hub || {}, options, {
Expand Down Expand Up @@ -66,7 +69,7 @@ export default defineNuxtModule<ModuleOptions>({
databaseQueriesPaths: [],
// Other options
version,
env: process.env.NUXT_HUB_ENV || 'production',
env: process.env.NUXT_HUB_ENV || (cliArgs.hubEnv as string) || 'production',
openapi: nuxt.options.nitro.experimental?.openAPI === true,
// Extra bindings for the project
bindings: {
Expand All @@ -79,6 +82,18 @@ export default defineNuxtModule<ModuleOptions>({
clientSecret: process.env.NUXT_HUB_CLOUDFLARE_ACCESS_CLIENT_SECRET || null
}
})
if (!['test', 'preview', 'production'].includes(hub.env)) {
log.error('Invalid hub environment, should be `test`, `preview` or `production`')
process.exit(1)
}
// If testing environment detects, set the hub env to `test`
if (nuxt.options.test) {
hub.env = 'test'
}
if (hub.env === 'test') {
log.info('NuxtHub test environment detected, using `test` dataset for all storage & disabling remote storage.')
hub.remote = false
}
runtimeConfig.hub = hub
runtimeConfig.public.hub = {}
// Make sure to tell Nitro to not generate the wrangler.toml file
Expand Down
13 changes: 7 additions & 6 deletions src/utils/wrangler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import type { HubConfig } from '../features'
// Please update nuxt-hub/cli preview command as well
export function generateWrangler(nuxt: Nuxt, hub: HubConfig) {
const wrangler: { [key: string]: any } = {}
const name = hub.env === 'test' ? 'test' : 'default'

if (hub.analytics && !hub.remote) {
wrangler['analytics_engine_datasets'] = [{
binding: 'ANALYTICS',
dataset: 'default'
dataset: name
}]
}

if (hub.blob && !hub.remote) {
wrangler['r2_buckets'] = [{
binding: 'BLOB',
bucket_name: 'default'
bucket_name: name
}]
}

Expand All @@ -27,23 +28,23 @@ export function generateWrangler(nuxt: Nuxt, hub: HubConfig) {
if (hub.kv && !hub.remote) {
wrangler['kv_namespaces'].push({
binding: 'KV',
id: 'kv_default'
id: `kv_${name}`
})
}

if (hub.cache) {
wrangler['kv_namespaces'].push({
binding: 'CACHE',
id: 'cache_default'
id: `cache_${name}`
})
}
}

if (hub.database && !hub.remote) {
wrangler['d1_databases'] = [{
binding: 'DB',
database_name: 'default',
database_id: 'default'
database_name: name,
database_id: name
}]
}

Expand Down

0 comments on commit 2097df2

Please sign in to comment.