Unit testing NodeJS (without exposing internals)
-
okay then......
on a scale from 0 to "hand in your programmer's license"..... how bad would it be to do
1.17
I'd go with a global switch or environmental variable.
Also, if you're already using prototype object pattern (
_name
for private members), you can do the similar thing with module exports and just export everything unconditionally?
-
so what i ended up with was....
look for a global method
describe
that's put there by mocha, and is absent from the global scope at run time.then i change behavior in two places to expose internals that shouldn't be poked by modules (modules don't get that luxury. just the core stuff)
and my tests:
... Did i do good @Yamikuronue?
-
so that leaves me with a very simple module API:
https://github.com/SockDrawer/SockIRC/blob/master/modules/log2console.js#L19-L30
this will probably be expanded to add a
config
parameter to begin when i do module loading proper, but for now...client is an event emitter that emits the events shown in begin (along with
raw
that's ignored but there in case a module wants to handle CTCP messages)payload is an object with the following keys:
-
type: (string) message subtype (for example message_recieved could have: 'action', 'notice', or 'message')
-
who: (string|null) user who sent the message
-
what: (string|null) target of the message (channel or user)
-
text: (string) texdt of the message,
-
raw: (object) parsed irc message as per [the docs](http://node-irc.readthedocs.org/en/latest/API.html#'raw')
-
command: (string) command that was parsed if texts begins with ! empty string otherwise
-
args: (string[]) args that were parsed if text begins with ! empty array otherwise
additionally client has the following methods:
- join(channel): join a channel
- part(channel, [reason]): leave a channel
- say(target, text): say something to person/channel
- notice(target, text): notice a person (/notice)
- action(target, text): do an action. (/me)
-