Tim Perry

Tech lead and open-source champion at Softwire. Creator of Build Focus. Keen open-source contributor, and maintainer of Loglevel and Server Components.

Server-Side Web Components at ConFoo

CSS-only tabs are a fun topic, and :target is a delightfully elegant declarative approach, except for the bit where it doesn’t work. The…

Opening Open Source with DevOps at DevDay
Promises Are So Passé at Frontend Conference

How can you use client-side libraries like Leaflet, on the server?

Get easy confidence on exactly what you’re committing.

You’ve written an application deployed using Dokku, and you’ve got it all up and running and great. You’ve heard a lot about why HTTPS is…

Why do we need better maps, and how does server-rendering help?

Opening Open Source with DevOps at DevOps BCN
If you're responsible for any SaaS apps, you should read this list of #security pointers: http://cto-security-checklist.sqreen.io/
Publish v0.1.0 package (https://www.npmjs.com/package/http-server-mock) to NPM, docs only say "work in progress", has no instructions. 200 downloads/week anyway.
Scary&interesting looking at how targeted #phishing attacks against orgs play out in reality: https://www.exploratorium.edu/blogs/tangents/we-got-phished-2 (from @exploratorium)
Fix extra ] typo
DDoS on DynDNS atm, breaking Heroku, Github & more: http://thenextweb.com/security/2016/10/21/massive-ddos-attack-dyn-dns-causing-havoc-online/ (mainly just seems to mean Github is crazy slow though, for me)
https://github.com/github/octocatalog-diff: a #Puppet testing&dev tool from @GitHubEng. Take 2 branches, and see what changes in their resulting catalogs.
Return the changed server
Return the changed server
Stop net.Server incorrectly extending net.Socket
This is amazing! Fascinating to see the norms of the web change so fast. https://twitter.com/0xjosh/status/786971412959420424
The @SoftwireUK blog now has a mountain of technical people mgmt tips, from the excellent @ZoeFCunningham. Go learn: http://www.softwire.com/blog/2016/10/14/tips-managing-technical-people-blog-post-wrap/
Add optionally() and make isDone and pendingMocks consistent.
This is not what I signed up for #Barcelona https://t.co/NQfLYY8UoL

The problem is that with sinon.stub(Lookup, 'getService') you're mutating the insides of the Lookup variable you're holding in your tests, and then getting the method from that variable. In your Lookup module though the function is just finding getService directly from its local scope. Externally I don't think there's any way you can mess with that scope, so there's no easy magic fix for this I'm afraid.

Generally, you usually can't nicely mock parts of a single module in a test. You need to restructure this a bit, and there's a few options:

  • Test them totally separately. Change getServiceWithRetry into a generic retry method, e.g. so you can call it like retry(nTimes, getService, "serviceName") or retry(() => getService("serviceName"), nTimes)). If it's practical to do this (i.e. if it's not too tied it to getService) then you can then easily test this on its own:

    var myStub = sinon.stub();
    myStub.onCall(0).throw("fail once");
    myStub.onCall(0).throw("fail twice");
    myStub.returns(true); // then return happily
    expect(retry(myStub, 1)).to.throw("fail twice"); // gives up after one retry
    expect(retry(myStub, 5)).to.return(true); // keeps going to success

    If elsewhere you want to be able to just call a single getServiceWithRetry, you can build one easily: var getServiceWithRetry = (arg, triesLeft) => retry(getService, tries)

  • Give up, and test them together. This means stubbing out the things that getService depends on, rather than stubbing it directly. It depends on what level of granularity you want from your tests, but if this code is simple and you can test more coarsely, this might be an easy option.

    You might want to do this even if you've separately them anyway, to get a unit and integration test for extra coverage. This is doubly true if there is some more complicated interactions going on between them.

  • Maybe not relevant in what this case, from what I can see, but in other cases sort-of like put the method-under-test (getServiceWithRetry) in a class, and use dependency injection. You'd create a class that takes the dependency (the getService method) in its constructor, stores it internally, and uses it later when you call methods on resulting object. In your production code something else will have to glue those together correctly, and then in your tests you can pass in a stub instead.

  • Also overkill for this case I expect, but you could pull getService into a totally separate module that Lookup imports, and use something like Rewire to swap it out for a different module during testing.

    This is quite similar to the dependency injection option really, and makes your production code simpler, but at the cost of making your testing code more complicated and magical.

Insecure IoT isn't just bad for users, it's now also powering botnets attacking the everybody: https://blog.cloudflare.com/say-cheese-a-snapshot-of-the-massive-ddos-attacks-coming-from-iot-cameras/ (cc @internetofshit)
Update PendingMocks when an interceptor is removed
My @ABBDevDay video's up: https://www.youtube.com/watch?v=46nQY2NybbU. Pretty proud of this one. Let's use #DevOps to make #OpenSource more open! #abbdevday
Phew - I was getting worried we wouldn't had any fundamental shifts in common JS infrastructure this week. https://twitter.com/yarnpkg/status/785857780838232064
Building simple, clean, accessible, shareable, history-tracking #CSS-only tabs, using the :target pseudo-class: https://medium.com/p/better-css-only-tabs-with-target-7886c88deb75
Lovely way to make sure you've given a little (github-star-based) credit to all the NPM projects you depend on. https://twitter.com/musically_ut/status/785172981882429442