Sinon: Use path-to-regexp for fake server urls

Created on 24 Jul 2016  路  10Comments  路  Source: sinonjs/sinon

Sinon's fake server supports writing regex for the URL. This is powerful, but routing DSLs are becoming increasingly common in JS. path-to-regexp provides a powerful & familiar one.

tl;dr:

Current:

server.respondWith(/\/books\/(\d+)/, ...)

with DSL:

server.respondWith('/books/:bookId/', ...)

It allows you to embed mini-regexes as custom params for when ya need it.

Small change but I think it'd go a long way :v:

Feature Request Help wanted

Most helpful comment

@lucasfcosta, start at the top, not at the bottom, when digging in 馃槃 The script you found should never have been there. It was what we were using before Maximillian changed the test setup to just use Mochify, and it should have been removed at that point. It's just leftover cruft that should be removed. If you start looking at what will run when you run npm test I think you would quickly find what you are looking for :-)

P.S. Mochify is a little bit like an opinionated version Karma, bundled along with Mocha, and optimized for cli-friendlyness. At least that's my impression, but Max might disagree 馃槢

All 10 comments

AFAIK this seems to be a non-breaking feature, am I right? If this just expands on our options I think this makes for quite readable tests where you can reuse strings from your routing logic instead of rewriting them as regexes. That's an improvement IMHO.

I _think_ it'd be non-breaking, unless there's any situations where a "regular" URL passed through path-to-regexp would get transformed. I can't think of any such URLs atm, but I'd need to look closer at it to be sure.

I like this suggestion. Readability FTW.

Ship a PR, and we can take it from there 馃憤

Great suggestion! I'd love to see a PR for this

Hello, friends, so, you'd expect this PR to be made using the path-to-regexp module or would you like to reimplement the whole feature?

Just reuse the existing path-to-regexp module. More lines in a project only means it gets harder to manage.

Hi everyone, sorry to bother you but I was starting to work on this early this morning and I noticed that you do not run the fake-server tests neither in the local environment nor on Travis due to this line which makes it run on browsers only.

I can easily set up Karma and start testing this on multiple browsers, but I'm more worried about how you could test it without having to do all this work again.

I've taken a look at your testing scripts and the whole environment but I couldn't find out how to run these tests without adding karma to the whole structure.

I tried to install the buster and phantomjs modules but I also noticed the config files mentioned in this script do not exist anymore.

What would be the most adequate way to run these tests on my local environment? Thanks for your help and sorry if I missed something.

@lucasfcosta, start at the top, not at the bottom, when digging in 馃槃 The script you found should never have been there. It was what we were using before Maximillian changed the test setup to just use Mochify, and it should have been removed at that point. It's just leftover cruft that should be removed. If you start looking at what will run when you run npm test I think you would quickly find what you are looking for :-)

P.S. Mochify is a little bit like an opinionated version Karma, bundled along with Mocha, and optimized for cli-friendlyness. At least that's my impression, but Max might disagree 馃槢

@fatso83 thanks for your help! I should've done that indeed. I think maybe my brain is so used to reading mocha everywhere I just went through this without noticing.
Sorry for that 馃槄

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stephanwlee picture stephanwlee  路  3Comments

byohay picture byohay  路  3Comments

zimtsui picture zimtsui  路  3Comments

NathanHazout picture NathanHazout  路  3Comments

sudhirbits picture sudhirbits  路  4Comments