Testing your CosmosDB apps locally without actually using Azure


I think you’ll find this implementation of CosmosDB Server for testing your applications locally quite interesting.

We will going through the following steps:

  1. Download node
  2. Make an empty package.json file
  3. Install cosmosdb-server using npm
  4. Create an app.js file
  5. Install cosmos dependency using npm
  6. Start your cosmosdb-server
  7. Make sure in your TestClass TestInitialize method to ignore SSL verification (self-signed cert)
  8. Run your unit tests

Download and install node

Navigate to the site I linked to above and install it, the latest LTS Version: 10.16.0 (includes npm 6.9.0) . After you install it you can verify the versions installed on your computer by opening a command prompt and entering the following commands:

Check versions of npm and node at your command prompt

Make an empty package.json

This is easy, just fire up your favorite editor and put to curly braces in it to avoid warnings during the install of cosmosdb-server

Empty package.json

Install cosmosdb-server using npm

In your command prompt execute the following command, which you can also find in the readme at the GitHub repo.

npm install @zeit/cosmosdb-server
npm install @zeit/cosmosdb-server

Create an app.js file

A sample of what is required is also in the readme at the GitHub repo. Here is an example of mine. I also created the database and container required for my unit tests

app.js configuration to start an instance of cosmos server on port 3000

Install Azure Cosmos Client

We need this dependency for the runClient() method in the app.js, at the command prompt enter:

npm install @azure/cosmos

npm install @azure/cosmos

Your package.json should now resemble the following, reflecting the packages for your app.

package.json

Start your cosmosdb-server

Again while at your command prompt, you can launch the cosmosdb-server by executing:

node app.js

running the local cosmosdb server

Get ready to execute your Unit Tests

This implementation uses a self-signed certificate so we need to make sure our Unit Test class doesn’t validate the certificate

I added a ServicePointManager callback to ignore the self-signed certificate in the TestInitialize method of my CosmosDB test class.

Ignore self-signed Certificate used by local cosmosdb-server

Here is my app.config for my Test project which has the settings for this cosmosdb server

app.config

Let’s execute some of the unit tests in my test project, the only one that fails is where we are attempting a SQL query with a where exists clause.

Next steps are to get this working during an Azure DevOps build, so until next time if you have any questions or comments feel free to leave them below,

About Wes MacDonald

Wes MacDonald is a Developer Technologies MVP, PSD, MCT and a Principal Consultant for LIKE 10 INC., a DevOps Consulting firm providing premium support, guidance and services for Azure and VSTS.

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: