-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed.xml
136 lines (114 loc) · 14.1 KB
/
feed.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>The Attic</title>
<description>Andrew Winder's take on node.js, AngularJS, and all other things technology.</description>
<link>winder.ws</link>
<atom:link href="winder.ws/feed.xml" rel="self" type="application/rss+xml" />
<item>
<title>Octopress 3.0</title>
<description><p>The next version of Octopress (3.0) is nearing release and is in beta now. I’ve switched this blog over to using the new version and it’s been great. It’s a pretty radical change from the previous version, but for me, this is exactly what I wanted Octopress to be originally. It’s highly modularized and lightweight, with a new <a href="https://github.com/octopress/deploy">deployment plugin</a> that can natively push to S3 as well as github. Octopress at it’s core is now nicely separated from your Jekyll design. If you’re like me, you’ll appreciate that Octopress is now about extending Jekyll as opposed to being highly intertwined with it. I’ll write more about my experience in a future post, but I wanted to get a quick complement out to those working on Octopress 3.0. Love the new direction, keep up the great work!</p></description>
<pubDate>Fri, 04 Apr 2014 21:53:46 -0400</pubDate>
<link>http://github.com/octopress/octopress</link>
<guid isPermaLink="true">winder.ws/2014/04/04/octopress-3-0.html</guid>
</item>
<item>
<title>Unit Testing Express.js Routes</title>
<description><p>Unit testing of model-type functionality in Node has been really straightforward for me so far. By injecting mocks using wonderful libraries like <a href="https://github.com/thlorenz/proxyquire">Proxyquire</a>, you can smoothly control flow and trigger conditional branches while testing to ensure that you’re fully covering a module. But what happens when you need to test out <a href="http://expressjs.com">Express.js</a> route handlers? Imagine you have configured a route in an Express.js application like so:</p>
<div>
<pre data-line=''><code class='language-javascript'>var express = require('express')
, app = exports = module.exports = express()
, ctrl = require('./controller');
app.get('/', ctrl.test);</code></pre>
</div>
<p>And imagine you have a controller that implements the callback you configured your application with for the “/” endpoint:</p>
<div>
<pre data-line=''><code class='language-javascript'>module.exports = {
test : function(req, res) {
setTimeout(function() {
res.send({ testing : true }, 200);
}, 5000);
},
}</code></pre>
</div>
<p>The timeout here isn’t exactly practical, but it is representative: Most non-trivial functionality in an Express.js project is going to need to communicate with some system, like a database or caching system, or read from the file system. In other words: Something asynchronous! Asynchronous code is much less straightforward to test because a unit test needs to have some kind of idea when the code you’re testing is done. Unlike testing a synchronous function, which returns to signify that it’s completed, you just can’t do that with asynchronous logic. Further complicating this situation is that Express route handlers signal that they are done by calling a method on the Response object, like res.send. This is a little different from situations where I’ve tested my own modules (usually in models) because the events that my models emit are easily subscribed to in the tests. I can wait for the events to fire in a unit test, inspect any data that is in the event, and then exit the unit test. What’s the right move here?</p>
<p>It’s actually relatively straightforward: I just need to inject a mock Response object into the route handler. By injecting a mock, I can have some control over what happens once the methods on the response are called. Because response methods are often called in asynchronous workflows, I’ll want a mock that emits an event itself. Lets see what that might look like:</p>
<div>
<pre data-line=''><code class='language-javascript'>var util = require('util')
, events = require('events').EventEmitter;
var res = function () {
};
util.inherits(res, events);
res.prototype.send = function(payload, code) {
this.emit('response', {
code: code,
response: payload
});
}
module.exports = function() {
return new res();
};</code></pre>
</div>
<p>Hopefully this looks as expected. I’m utilizing the EventEmitter prototype that Node.JS provides, and now when the response object’s <em>send</em> method is invoked, I fire a <em>response</em> event. That way, in the unit test, I can attach an event handler to the mock’s event, and then kick off the route handler method that I want to test. Whenever the response methods are actually invoked, the handler in the unit test will invoke, and there we can perform any assertions on the output of our controller’s action: the response code and response body.</p>
<div>
<pre data-line=''><code class='language-javascript'>var expect = require('chai').expect
, res = require('./mocks/response')()
, testing = require('./controller');
describe('Testing', function(){
it('Should send an object with a testing key', function(done){
res.on('response', function(resp) {
expect(resp.response).to.have.property('testing');
expect(resp.response.testing).to.equal(true);
done();
});
testing.test({}, res);
});
});</code></pre>
</div>
<p>And just like that, my route handler has unit test coverage. As the controller gets built out, we’ll want to use tools like <a href="https://github.com/thlorenz/proxyquire">Proxyquire</a>, as mentioned before, to inject mocks for our external resources to keep our tests running smoothly and to not duplicate testing efforts. A unit test for a route handler should exist primarily to test any branching logic or business logic that exists inside the handler itself: Not the branching logic and business logic in the route handlers <em>dependencies</em>. We try to keep our logic pretty light inside of route handler itself at work, but there are some situations, like in error reporting, where we do branch. These situations are definitely not trivial, and it’s important that we have appropriate testing coverage for it. And now, we have a straightforward and repeatable method for doing that testing.</p></description>
<pubDate>Mon, 20 Jan 2014 22:00:00 -0500</pubDate>
<link>http://winder.ws/2014/01/20/unit-testing-express-dot-js-routes.html</link>
<guid isPermaLink="true">winder.ws/2014/01/20/unit-testing-express-dot-js-routes.html</guid>
</item>
<item>
<title>Structuring Library Functionality in Node.js Projects</title>
<description><p>Recently I needed to start thinking about adding reusable “library” components to an <a href="http://www.expressjs.com">Express.js</a> node.js project, and I hit a bit of a brick wall. The library functionality was fairly specific to the application, and it just doesn’t make that much sense to break it out as seperately-packaged module right now. Seems like a fairly common use-case, right? Well, apparently not. You can load any filepath through the <code>require()</code> system, like so:</p>
<div>
<pre data-line=''><code class='language-javascript'>var module = require('./lib/myModule');
...</code></pre>
</div>
<p>But where this can quickly become a nuisance is when you are nested down a deep folder hierarchy:</p>
<div>
<pre data-line=''><code class='language-javascript'>var module = require('../../../lib/myModule');
...</code></pre>
</div>
<p>Having to track the folder hierarchy is a little bit of an inconvience, especially when compared to the convience of the <a href="https://npmjs.org">NPM</a> module system. Searching around for some solutions, I found issues with the approaches commonly suggested:</p>
<ul>
<li><strong>Modify the paths where require.js scans for modules</strong> - In <a href="http://nodejs.org/docs/v0.4.1/api/all.html#loading_from_the_require.paths_Folders">previous versions</a> of node, it looks like you could modify the paths require.js searched through, but this method has been deprecated and was not a “best practice” while available.</li>
<li><strong>Load the path to your library directory into a global</strong> - I wanted to figure out a way around needing to declare some application-wide globals. doing something like this might also create some unit testing concerns.</li>
<li><strong>Utilize <a href="https://npmjs.org/doc/link.html">npm-link</a></strong> - This seemed great at first blush, but the actual mechanics of how npm-link works are a little perculiar. Npm-link first symlinks a specified module in your project directory to the global npm module location. Then, it’s symlinks that global location to a module in your local project’s node_modules directory. This means naming collisions could occur if you tried to use this method with two projects on the same machine.</li>
<li><strong>Just put your library modules in the node_modules directory!</strong> - Right now, this project’s node_modules directory only includes community packages, and they’re all installed through <code>package.json</code>. This provides nice separation in the code – <code>node_modules</code> is not our code, everything else is. The node_modules directory is actually ignored in source control to disuade developers from mucking around with our open-source libraries.</li>
</ul>
<p>Npm-link got me thinking though, and a <a href="http://mathematism.com">co-worker</a> clued in on that too. We could create a “library” module, which acted mostly as a namespace for our project’s unique library-type functionality. Each of those unique pieces would be submodules of the “library” module. Then, we can create a symlink from the node_modules directory to the library directory, and commit that symlink into our source control. Now our code can look like this:</p>
<div>
<pre data-line=''><code class='language-javascript'>var module = require('lib/myModule');
...</code></pre>
</div>
<p><img src="http://winder.ws/img/posts/2013-10-15-structuring-local-node-modules/folder-structure.png" alt="Example folder structure" /></p>
<p>We get the benefits of keeping our code outside of the node_modules directory, but with the benefits of it living within the <code>require()</code> lookup path. It’s working out great so far, and I’m happy we ended up finding a very straightforward solution to our problem.</p></description>
<pubDate>Tue, 15 Oct 2013 22:05:00 -0400</pubDate>
<link>http://winder.ws/2013/10/15/structuring-local-node-modules.html</link>
<guid isPermaLink="true">winder.ws/2013/10/15/structuring-local-node-modules.html</guid>
</item>
<item>
<title>What's In the Attic</title>
<description><p>Hello World! Welcome to the Attic, that musty place containing the out-of-season or otherwise discarded riff-raff of your life. Or in this case, the random musings and technical discussions of Andrew Winder, a web engineer at Message Systems in Columbia, MD. I’m primarily focused on the areas of PHP API development and well-structured <a href="http://www.angularjs.org">Javascript</a> on the client-side right now, along with <a href="https://en.wikipedia.org/wiki/NoSQL">non-rdbms</a> data storage design and <a href="http://www.nodejs.org">server-side javascript</a> slowly commanding more and more of my attention. As I continue to explore these areas of technology, I’ve found myself wanting somewhere to collect and gather my thoughts, and record my experiences designing and building solutions using these tools. This blog will serve as that somewhere, that place to gather and store my thoughts, so that one day someone might find some use for them. My attic.</p>
<p>Along with acting as a place to purely write though, this blog will also act as an experimental playground for front-end development as well. I’m utilizing <a href="http://jekyllrb.com">Jekyll</a> as a static-site generator of these posts and listing pages, <a href="http://twitter.github.io/bootstrap/">Twitter Bootstrap</a> as a CSS framework, and <a href="http://fortawesome.github.io/Font-Awesome/">FontAwesome</a> to provide iconography across the site. I’ll be sure not to break things as I go along, but I’ve taken a first stab at a responsive design that seems to make sense to me across various device breakpoints. That may not be the last stab, though. This site will also evolve as I continue to actually write out posts. Right now, with one post, RSS feeds don’t make much sense, but if I can get into a good rhythm of posting updates, that’ll be next up. Figuring out better tag paging support might come after that.</p>
<p>That about wraps up my introduction to this site. Please feel free to ping me over the social media links on this site with any feedback, suggestions, or requests you might have!</p>
<p>-Andy, <a href="http://www.twitter.com/awinder">@awinder</a></p></description>
<pubDate>Sun, 13 Oct 2013 16:34:00 -0400</pubDate>
<link>http://winder.ws/2013/10/13/whats-in-the-attic.html</link>
<guid isPermaLink="true">winder.ws/2013/10/13/whats-in-the-attic.html</guid>
</item>
</channel>
</rss>