H
Hono4mo ago
Hec7or

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.
1 Reply
Nico
Nico3mo ago
I think it’s because wrangler is not running when using vitest. You can use super test it will run the test with the url provided so you can run the wrangler dev server and test it on it Here’s and example
import r from 'supertest';

const request = r('http://localhost:3030/auth');

describe('Login user', () => {
test('with no data', async () => {
const res = await request.post('/login').send();

expect(res.status).toBe(400);
expect(res.body.success).toBeFalsy();
});
import r from 'supertest';

const request = r('http://localhost:3030/auth');

describe('Login user', () => {
test('with no data', async () => {
const res = await request.post('/login').send();

expect(res.status).toBe(400);
expect(res.body.success).toBeFalsy();
});
Want results from more Discord servers?
Add your server