<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/how-to-automate-your-tests-in-express-using-vitest-m3xroq8sb" -->

---
title: How to Automate Your Tests in Express Using Vitest
description: A step-by-step guide walks through automating tests for an Express API using Vitest, covering both unit and integration testing approaches. Topics include...
canonical: https://daily.dev/posts/how-to-automate-your-tests-in-express-using-vitest-m3xroq8sb
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: How to Automate Your Tests in Express Using Vitest | daily.dev
og:description: A step-by-step guide walks through automating tests for an Express API using Vitest, covering both unit and integration testing approaches. Topics include...
og:url: https://daily.dev/posts/how-to-automate-your-tests-in-express-using-vitest-m3xroq8sb
og:image: https://api.daily.dev/og/posts/m3XrOq8sb.png
og:image:alt: How to Automate Your Tests in Express Using Vitest
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Automate Your Tests in Express Using Vitest

**[freeCodeCamp](https://daily.dev/sources/freecodecamp)** · 23 min read · 3 upvotes · 0 comments

## Summary

A step-by-step guide walks through automating tests for an Express API using Vitest, covering both unit and integration testing approaches. Topics include naming test files, using describe/it/expect and matchers, mocking functions and Mongoose models with vi.mock and mockingoose, and setting up integration tests with Supertest and mongodb-memory-server for an in-memory MongoDB instance. It also explains restructuring an Express app into a separate exportable instance to avoid database connection conflicts during testing, and closes with guidance on when to choose unit versus integration tests.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.freecodecamp.org/news/how-to-automate-your-tests-in-express-using-vitest>

## Questions this post answers

### How do I mock a Mongoose model in Vitest tests without hitting a real database?

Use the mockingoose library: install it as a dev dependency, then call mockingoose(Model).toReturn(fakeData, "save") to define what a Mongoose method like save should return. This avoids writing test data to a real MongoDB instance while still exercising the code path that calls the model.

_Developers wiring up Mongoose test mocks can find step-by-step examples like this through daily.dev._

### How do I set up integration tests for an Express app with an in-memory MongoDB database using Vitest?

Use mongodb-memory-server to spin up an in-memory Mongo instance in a beforeAll hook, connect via mongoose.connect(uri), and start the Express app with app.listen(0). Use supertest's request(app) to send real HTTP requests during tests, then close the server, disconnect mongoose, and stop the memory server in afterAll.

_Developers setting up integration test environments for Express APIs can track patterns like this on daily.dev._

### Why does exporting the Express app instance directly from the server entry file cause problems in integration tests?

When the app and the database connection bootstrap logic live in the same file, importing that file for tests also triggers the production database connection, which conflicts with the test database connection and breaks the tests. The fix is to define and export the Express app in its own file (app.ts) separate from the file that calls connectToDB and app.listen.

_Backend developers structuring Express apps for testability can compare setup patterns on daily.dev._

## Similar posts on daily.dev

- [The Node.js built-in test runner](https://daily.dev/posts/the-node-js-built-in-test-runner-qw7ywroa9) · Flavio Copes · 0 upvotes · 0 comments
- [Testing Node.js APIs with Jest: A Frontend Developer's Guide to Backend Testing](https://daily.dev/posts/testing-node-js-apis-with-jest-a-frontend-developer-s-guide-to-backend-testing-ykfrkag4k) · SitePoint · 0 upvotes · 0 comments
- [Setting Up Vitest for React and Next.js Applications](https://daily.dev/posts/setting-up-vitest-for-react-and-next-js-applications-0sldabkfz) · TypeScript.TV · 9 upvotes · 0 comments
- [Comparing the best Node.js unit testing frameworks](https://daily.dev/posts/comparing-the-best-node-js-unit-testing-frameworks-ezow8yale) · LogRocket · 0 upvotes · 0 comments

---

Tags: [#testing](https://daily.dev/tags/testing), [#typescript](https://daily.dev/tags/typescript), [#express](https://daily.dev/tags/express), [#mongoose](https://daily.dev/tags/mongoose), [#vitest](https://daily.dev/tags/vitest)

[View this post on daily.dev](https://daily.dev/posts/how-to-automate-your-tests-in-express-using-vitest-m3xroq8sb)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"How to Automate Your Tests in Express Using Vitest","url":"https://daily.dev/posts/how-to-automate-your-tests-in-express-using-vitest-m3xroq8sb","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/how-to-automate-your-tests-in-express-using-vitest-m3xroq8sb"},"datePublished":"2026-08-29T00:06:14.020Z","dateModified":"2026-09-14T06:43:51.924Z","description":"A step-by-step guide walks through automating tests for an Express API using Vitest, covering both unit and integration testing approaches. Topics include...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/5f4e3c46fe467faa0b61d350a6529bbd?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/5f4e3c46fe467faa0b61d350a6529bbd?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"freeCodeCamp","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"freeCodeCamp","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1628412854/logos/freecodecamp","url":"https://daily.dev/sources/freecodecamp"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/how-to-automate-your-tests-in-express-using-vitest-m3xroq8sb","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":3},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"testing,typescript,express,mongoose,vitest","timeRequired":"PT23M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"freeCodeCamp","item":"https://daily.dev/sources/freecodecamp"},{"@type":"ListItem","position":3,"name":"How to Automate Your Tests in Express Using Vitest"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/how-to-automate-your-tests-in-express-using-vitest-m3xroq8sb#faq","mainEntity":[{"@type":"Question","name":"How do I mock a Mongoose model in Vitest tests without hitting a real database?","acceptedAnswer":{"@type":"Answer","text":"Use the mockingoose library: install it as a dev dependency, then call mockingoose(Model).toReturn(fakeData, \"save\") to define what a Mongoose method like save should return. This avoids writing test data to a real MongoDB instance while still exercising the code path that calls the model. Developers wiring up Mongoose test mocks can find step-by-step examples like this through daily.dev."}},{"@type":"Question","name":"How do I set up integration tests for an Express app with an in-memory MongoDB database using Vitest?","acceptedAnswer":{"@type":"Answer","text":"Use mongodb-memory-server to spin up an in-memory Mongo instance in a beforeAll hook, connect via mongoose.connect(uri), and start the Express app with app.listen(0). Use supertest's request(app) to send real HTTP requests during tests, then close the server, disconnect mongoose, and stop the memory server in afterAll. Developers setting up integration test environments for Express APIs can track patterns like this on daily.dev."}},{"@type":"Question","name":"Why does exporting the Express app instance directly from the server entry file cause problems in integration tests?","acceptedAnswer":{"@type":"Answer","text":"When the app and the database connection bootstrap logic live in the same file, importing that file for tests also triggers the production database connection, which conflicts with the test database connection and breaks the tests. The fix is to define and export the Express app in its own file (app.ts) separate from the file that calls connectToDB and app.listen. Backend developers structuring Express apps for testability can compare setup patterns on daily.dev."}}]}
```

