jack
jack

Reputation: 381

Mocking/Stubbing CosmosDB connection to perform unit testing node js

I am looking for any method that I can use to mock connections to Azure in order to perform Unit Testing. Are there any good reliable mocking npm modules available.

Upvotes: 2

Views: 3795

Answers (2)

Sam
Sam

Reputation: 11

I was going down this path to mock out the cosmo client and db container.

jest.mock('@azure/cosmos', () => {
return {
    CosmosClient: jest.fn(() => ({
        database: jest.fn(() => ({
            container: jest.fn(() => ({
                items: {
                    query: jest.fn(() => ({
                        fetchAll: jest.fn(() => ({
                            resources: [{ cat: 'dog' }],
                        })),
                    })),
                },
            })),
        })),
    })),
}

for mocking the following

const client: CosmosClient = new CosmosClient('...')
const database: Database = client.database('...')
const container: Container = database.container('...')

const querySpec = {
    query: `SELECT * from ...`,
}

const response: Model[] = await (
        await container.items.query(querySpec).fetchAll()
    ).resources

Upvotes: 1

Jesse Carter
Jesse Carter

Reputation: 21187

Jest is a general purpose JavaScript testing framework. It provides several different ways to do mocking both at the module/import level as well as for individual functions. There are other options out there like Sinon for mocking but I find Jest has everything you need out of the box.

Upvotes: 0

Related Questions