Hec7or
Hec7or
HHono
Created by Hec7or on 6/7/2024 in #help
unit tests with vitest, drizzle and d1
I am having problems running my hono API tests. vites.config.ts
import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config'
import path from 'path';

export default defineWorkersConfig({
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
test: {
globals: true,
poolOptions: {
workers: {
isolatedStorage: false,
wrangler: { configPath: './wrangler.toml' }
},
}
}
})
import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config'
import path from 'path';

export default defineWorkersConfig({
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
test: {
globals: true,
poolOptions: {
workers: {
isolatedStorage: false,
wrangler: { configPath: './wrangler.toml' }
},
}
}
})
wrangler.toml
name = "api"
compatibility_date = "2023-12-01"
compatibility_flags = ['nodejs_compat']

[vars]
MY_VAR = "my-variable"

[[d1_databases]]
binding = "DB" # i.e. available in your Worker on env.DB
database_name = "..."
database_id = "..."
name = "api"
compatibility_date = "2023-12-01"
compatibility_flags = ['nodejs_compat']

[vars]
MY_VAR = "my-variable"

[[d1_databases]]
binding = "DB" # i.e. available in your Worker on env.DB
database_name = "..."
database_id = "..."
collections.test.ts
import app from "../src/index"
import { env } from 'cloudflare:test'

describe('Collections', () => {
test('POST /collections', async () => {
console.log(env)
const res = await app.request('/api/v1/collections', {
method: "POST",
body: JSON.stringify({
name: "collection",
status: "draft",
icon: "icon",
color: "red",
})
}, env)
console.log(await res.json())
// it should return a "{data: {...}, status: {...}}" object but returns an empty object
})

test('GET /collections', async () => {
console.log(env)
const res = await app.request('/api/v1/collections', {}, env)
console.log(await res.json())
// it should return an object {data: [...], status: {...}} where data has length = 3 but returns an empty object
})
})
import app from "../src/index"
import { env } from 'cloudflare:test'

describe('Collections', () => {
test('POST /collections', async () => {
console.log(env)
const res = await app.request('/api/v1/collections', {
method: "POST",
body: JSON.stringify({
name: "collection",
status: "draft",
icon: "icon",
color: "red",
})
}, env)
console.log(await res.json())
// it should return a "{data: {...}, status: {...}}" object but returns an empty object
})

test('GET /collections', async () => {
console.log(env)
const res = await app.request('/api/v1/collections', {}, env)
console.log(await res.json())
// it should return an object {data: [...], status: {...}} where data has length = 3 but returns an empty object
})
})
it is supposed to be using the local database inside .wrangler/state/v3/d1/miniflare-D1DatabaseObject/<SNIP>.sqlite since it is the one I have configured for local development and that works when I make requests with hoppscotch or any other client.
3 replies