Comments (5)
Sounds like you've got the right idea mostly. It depends on the kind of testing you're going for though... Do you want to write unit tests or end to end tests? If you're doing unit tests, then don't worry about checking with the database and just test the logic. Unit tests should test units.
If you're doing E2E (End to End) similar to and sometimes confused (by myself) with Integration Tests, you'll want to test more of the stack (including what's in the database) and you'll probably do it a bit differently.
My opinion is that you should have both. If you can only do one, do E2E. It's a bit tougher to set up and be flaky, but you get a better picture of the health of your system. But I believe you should do both. (Just make sure to not just do unit tests, otherwise you wind up with this) :-)
Good luck!
from ama.
Oh, and thanks for the question :D
from ama.
You can also try to unit test it - http://glebbahmutov.com/blog/how-to-correctly-unit-test-express-server/
from ama.
Thanks for chiming in @bahmutov. I should have prefaced this with the fact that the last significant express server I wrote was 2 years ago, so I'm a bit out of practice :-) Hopefully the concepts I shared were sound though :-)
from ama.
tl;dr: Why not unit test just the router URL and verb?
I also had the same question and I'm working on this idea that unit tests for routers shouldn't need to spin up a server. I think you should just test the router endpoints, meaning URLs and verbs.
I do backend components for reusability reasons (not really, but I'm a former AngularJS dev so that's what I do by default), and a component has a router, repository/store, schema, ORM model, and a testfile for every one of the previous ones.
For the route, my test looks like:
var test = require('tape');
var routerInit = require('./router');
var routerInfo = routerInit();
test('/filters base path', function onTest(assert) {
assert.plan(1);
var path = routerInfo.path;
assert.equals(path, '/filters');
});
test('GET /', function onTest(assert) {
assert.plan(2);
var route = routerInfo.router.stack[0].route;
var routeGetMethod = route.methods.get;
assert.equals(routeGetMethod, true);
var routePath = route.path;
assert.equals(routePath, '/');
});
routerInfo.router
is the Express router object. You can mount a router onto another route, so the full path for the GET / in filters will be http://localhost:3000/api/filters/
where /api
is the root router that mounts filterRouter
via apiRouter.use(filterRouterInfo.path, filterRouterInfo.router)
.
Oh, and don't forget to follow above tips for end-to-end testing too!
I was going to make a tweet out of this... Don't think I will :-)
from ama.
Related Issues (20)
- Trying to find a video I saw 2-3 years ago HOT 1
- What were the features you were missing in buttondown? HOT 1
- Article Idea
- JSDOM doesn't dynamically provide the clientHeight HOT 1
- How to keep updated react context value inside a callback in a custom hook HOT 2
- UseEffect hook and testing-library HOT 4
- Testing visuals with React Testing Library HOT 2
- Who is the version of you for React Native
- What is the best approach to analyze the code base healthy of existing projects?
- Epic React Course HOT 3
- question according to useImperativeHandle in react?
- Compound Component in React
- What approach do you prefer to cache data to not call the server every time when useEffect re-render? HOT 1
- Async beforeEach for fetching data HOT 1
- Choose a Backend As A Service for prototyping HOT 1
- Using Error boundry in react app HOT 2
- Question regarding your useMemo & useCallback blog HOT 4
- Using testing-library wrapper with redux to test components
- React/Redux testing with react-testing-library
- What resources do you use to keep up to date on the direction of the industry? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ama.