diff --git a/playground/test/basic.test.ts b/playground/test/basic.test.ts new file mode 100644 index 00000000..31a17bd9 --- /dev/null +++ b/playground/test/basic.test.ts @@ -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' }) + }) +}) diff --git a/src/module.ts b/src/module.ts index 9a5439bc..103a277c 100644 --- a/src/module.ts +++ b/src/module.ts @@ -35,8 +35,11 @@ export default defineNuxtModule({ 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, { @@ -66,7 +69,7 @@ export default defineNuxtModule({ 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: { @@ -79,6 +82,18 @@ export default defineNuxtModule({ 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 diff --git a/src/utils/wrangler.ts b/src/utils/wrangler.ts index a8fae422..f7ef8bd5 100644 --- a/src/utils/wrangler.ts +++ b/src/utils/wrangler.ts @@ -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 }] } @@ -27,14 +28,14 @@ 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}` }) } } @@ -42,8 +43,8 @@ export function generateWrangler(nuxt: Nuxt, hub: HubConfig) { if (hub.database && !hub.remote) { wrangler['d1_databases'] = [{ binding: 'DB', - database_name: 'default', - database_id: 'default' + database_name: name, + database_id: name }] }