Git Product home page Git Product logo

Comments (5)

kentcdodds avatar kentcdodds commented on May 25, 2024

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.

kentcdodds avatar kentcdodds commented on May 25, 2024

Oh, and thanks for the question :D

from ama.

bahmutov avatar bahmutov commented on May 25, 2024

You can also try to unit test it - http://glebbahmutov.com/blog/how-to-correctly-unit-test-express-server/

from ama.

kentcdodds avatar kentcdodds commented on May 25, 2024

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.

marcusradell avatar marcusradell commented on May 25, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.