N
Nuxt2mo ago
h2o

Test for nitro storage

I want to test if the nitro storage returns a certain item. How do I write a test for it? In my endpoint I use a server util with the function getContent(path), that's what I want to unit test. It's a server plugin that sets up the storage in the first place.
import { useNitroApp } from '#internal/nitro/app'

export async function getContent (path) {
const nitroApp = useNitroApp()
const storageType = 'website'
const db = nitroApp[`${storageType}ContentStorage`]
const res = await db.getItem(path.replace(/\//g, ':'))
return res
}
import { useNitroApp } from '#internal/nitro/app'

export async function getContent (path) {
const nitroApp = useNitroApp()
const storageType = 'website'
const db = nitroApp[`${storageType}ContentStorage`]
const res = await db.getItem(path.replace(/\//g, ':'))
return res
}
3 Replies
kapa.ai
kapa.ai2mo ago
Hi @h2o. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To test if the Nitro storage returns a certain item, you can use the mockNuxtImport function provided by Nuxt's testing utilities. This allows you to mock the storage functionality and test your getContent function. Here's how you can write a test for it: 1. First, you'll need to mock the useNitroApp import:
```typescript
import { mockNuxtImport } from '@nuxt/test-utils/runtime'
import { describe, it, expect, vi } from 'vitest'

// Mock the useNitroApp import
mockNuxtImport('useNitroApp', () => {
return () => ({
websiteContentStorage: {
getItem: vi.fn()
}
})
})

// Import your getContent function
import { getContent } from './your-file-path'
```typescript
import { mockNuxtImport } from '@nuxt/test-utils/runtime'
import { describe, it, expect, vi } from 'vitest'

// Mock the useNitroApp import
mockNuxtImport('useNitroApp', () => {
return () => ({
websiteContentStorage: {
getItem: vi.fn()
}
})
})

// Import your getContent function
import { getContent } from './your-file-path'
2. Now you can write your test:
2. Now you can write your test:
markdown
describe('getContent', () => {
it('should retrieve content from storage', async () => {
// Setup the mock return value
const mockContent = { title: 'Test Content' }
const useNitroApp = await import('#imports').then(m => m.useNitroApp)
useNitroApp().websiteContentStorage.getItem.mockResolvedValue(mockContent)

// Call the function
const result = await getContent('/test/path')

// Assert the result
expect(result).toEqual(mockContent)

// Verify that getItem was called with the correct key
expect(useNitroApp().websiteContentStorage.getItem).toHaveBeenCalledWith('test:path')
})
})
describe('getContent', () => {
it('should retrieve content from storage', async () => {
// Setup the mock return value
const mockContent = { title: 'Test Content' }
const useNitroApp = await import('#imports').then(m => m.useNitroApp)
useNitroApp().websiteContentStorage.getItem.mockResolvedValue(mockContent)

// Call the function
const result = await getContent('/test/path')

// Assert the result
expect(result).toEqual(mockContent)

// Verify that getItem was called with the correct key
expect(useNitroApp().websiteContentStorage.getItem).toHaveBeenCalledWith('test:path')
})
})
`` This test does the following: 1. It mocks the useNitroApp function to return an object with a websiteContentStorage that has a mocked getItem method. 2. It sets up a mock return value for the getItem method. 3. It calls the getContent function with a test path. 4. It asserts that the result matches the mock content. 5. It verifies that the getItem method was called with the correct key (note that it replaces '/' with ':' as your original function does). Remember to adjust the import paths according to your project structure. This approach allows you to unit test your getContent` function without needing to set up actual storage. It isolates the function and tests its behavior based on the expected interaction with the Nitro storage. Nuxt Testing Docs (https://nuxt.com/docs/getting-started/testing#mocknuxtimport) Nitro Storage Guide (https://nitro.unjs.io/guide/storage#usage) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@259759566118584322> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
h2o
h2oOP2mo ago
I want to test with real data.
Want results from more Discord servers?
Add your server