Skip to content

Commit 33380dd

Browse files
committed
Run all errors through a central place
In attempting to keep output formatting consistent, all errors now run through the log module. Additionally added a config to suppress errors from the framework.
1 parent 93ca31d commit 33380dd

File tree

11 files changed

+79
-47
lines changed

11 files changed

+79
-47
lines changed

src/config.coffee

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ stringifyAnything = require('./stringify/anything')
33

44
DEFAULTS =
55
ignoreWarnings: false
6+
suppressErrors: false
67

78
config = _.extend({}, DEFAULTS)
89

@@ -16,4 +17,4 @@ module.exports.reset = ->
1617
ensureOverridesExist = (overrides) ->
1718
_.each overrides, (val, key) ->
1819
if !config.hasOwnProperty(key)
19-
throw new Error("Error: td.config - \"#{key}\" is not a valid configuration key (valid keys are: #{stringifyAnything(_.keys(config))})")
20+
require('./log').error("td.config", "\"#{key}\" is not a valid configuration key (valid keys are: #{stringifyAnything(_.keys(config))})")
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
config = require('./config')
22

3-
module.exports = (func, msg, url) ->
3+
module.exports.warn = (func, msg, url) ->
44
return if config().ignoreWarnings
55
console?.warn?("Warning: testdouble.js - #{func} - #{msg}#{withUrl(url)}")
66

7+
module.exports.error = (func, msg, url) ->
8+
return if config().suppressErrors
9+
throw new Error("Error: testdouble.js - #{func} - #{msg}#{withUrl(url)}")
10+
711
withUrl = (url) ->
812
return "" unless url?
913
" (see: #{url} )"

src/matchers/index.coffee

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
_ = require('lodash')
2+
log = require('../log')
23
create = require('./create')
34
stringifyArguments = require('../stringify/arguments')
45

@@ -49,7 +50,7 @@ module.exports =
4950
else if _.isPlainObject(containing)
5051
containsAllSpecified(containing, actualArg)
5152
else
52-
throw new Error("Error: td.matchers.contains - this matcher only supports strings, arrays, and plain objects")
53+
log.error("td.matchers.contains", "this matcher only supports strings, arrays, and plain objects")
5354

5455
argThat: create
5556
name: 'argThat'

src/replace/imitate.coffee

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
_ = require('lodash')
22
object = require('../object')
33
tdFunction = require('../function')
4+
log = require('../log')
45
isConstructor = require('./is-constructor')
56
wrapWithConstructor = require('./wrap-with-constructor')
67
stringifyAnything = require('../stringify/anything')
@@ -11,4 +12,4 @@ module.exports = (realThing, optionalName) ->
1112
else if _.isFunction(realThing)
1213
tdFunction(if realThing?.name then realThing.name else optionalName)
1314
else
14-
throw new Error("Error: td.replace - \"#{optionalName}\" property was found, but test double only knows how to replace functions, constructors, & objects containing functions (its value was #{stringifyAnything(realThing)}).")
15+
log.error("td.replace", "\"#{optionalName}\" property was found, but test double only knows how to replace functions, constructors, & objects containing functions (its value was #{stringifyAnything(realThing)}).")

src/replace/property.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
imitate = require('./imitate')
22
isConstructor = require('./is-constructor')
33
wrapWithConstructor = require('./wrap-with-constructor')
4-
4+
log = require('../log')
55
reset = require('../reset')
66

77
module.exports = (object, property, manualReplacement) ->
88
isManual = arguments.length > 2
99
realThingExists = object[property] || object.hasOwnProperty(property)
1010

1111
if !isManual && !realThingExists
12-
throw new Error("Error: td.replace - No \"#{property}\" property was found.")
12+
log.error("td.replace", "No \"#{property}\" property was found.")
1313

1414
realThing = object[property]
1515
fakeThing = if isManual then manualReplacement else imitate(realThing, property)

src/when.coffee

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ _ = require('lodash')
22
calls = require('./store/calls')
33
stubbings = require('./store/stubbings')
44
callback = require('./matchers/callback')
5+
log = require('./log')
56

67
module.exports = (__userDoesPretendInvocationHere__, config = {}) ->
78
thenReturn: (stubbedValues...) ->
@@ -19,7 +20,7 @@ addStubbing = (stubbedValues, config, plan) ->
1920
stubbings.add(last.testDouble, concatImpliedCallback(last.args, config), stubbedValues, config)
2021
last.testDouble
2122
else
22-
throw new Error """
23+
log.error "td.when", """
2324
No test double invocation call detected for `when()`.
2425
2526
Usage:

test/src/config-test.coffee

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ describe 'td.config', ->
33
Given -> @config = td.config()
44
Then -> expect(@config).to.deep.equal
55
ignoreWarnings: false
6+
suppressErrors: false
67

78
context 'overriding', ->
89
Given -> @config = td.config(ignoreWarnings: true)
@@ -14,4 +15,4 @@ describe 'td.config', ->
1415
@config = td.config(wat: 'wat?')
1516
catch e
1617
@error = e
17-
Then -> @error.message == 'Error: td.config - "wat" is not a valid configuration key (valid keys are: ["ignoreWarnings"])'
18+
Then -> @error.message == 'Error: testdouble.js - td.config - "wat" is not a valid configuration key (valid keys are: ["ignoreWarnings", "suppressErrors"])'

test/src/log-test.coffee

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
return unless NODE_JS
2+
3+
describe 'log', ->
4+
Given -> @subject = requireSource('log')
5+
6+
describe '.warn', ->
7+
Given -> @ogWarn = console.warn
8+
afterEach -> console.warn = @ogWarn
9+
10+
context 'when console.warn is a thing', ->
11+
Given -> @warnings = []
12+
Given -> console.warn = (msg) => @warnings.push(msg)
13+
14+
context 'no URL', ->
15+
When -> @subject.warn('td.someFunc','ugh')
16+
Then -> @warnings[0] == 'Warning: testdouble.js - td.someFunc - ugh'
17+
18+
context 'with a documentation URL', ->
19+
When -> @subject.warn('td.someFunc','ugh', 'http?')
20+
Then -> @warnings[0] == 'Warning: testdouble.js - td.someFunc - ugh (see: http? )'
21+
22+
context 'with td.config({ignoreWarnings: true})', ->
23+
Given -> td.config(ignoreWarnings: true)
24+
When -> @subject.warn('waaaarning')
25+
Then -> @warnings.length == 0
26+
27+
context 'when console.warn does not exist', ->
28+
Given -> console.warn = undefined
29+
When -> @subject.warn('lolololol', 'lol')
30+
Then -> #nothing explodes
31+
32+
context 'when console does not exist', ->
33+
Given -> @ogConsole = console
34+
Given -> delete global.console
35+
When -> @subject.warn('lolololol', 'lol')
36+
Then -> #nothing explodes
37+
afterEach -> global.console = @ogConsole
38+
39+
describe '.error', ->
40+
context 'suppressErrors: true', ->
41+
Given -> td.config(suppressErrors: true)
42+
When -> @subject.error('hi','hi')
43+
Then -> # nothing happens
44+
45+
context 'without url', ->
46+
When -> try
47+
@subject.error('td.lol', 'oops')
48+
catch e
49+
@error = e
50+
Then -> @error.message = "Error: testdouble.js - td.lol - oops"
51+
52+
context 'with url', ->
53+
When -> try
54+
@subject.error('td.lol', 'oops', 'ftp:')
55+
catch e
56+
@error = e
57+
Then -> @error.message = "Error: testdouble.js - td.lol - oops (see: ftp:)"
58+
59+

test/src/matchers-test.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe '.matchers', ->
8181
Then -> @matches(td.matchers.contains('bar'), 'foobarbaz') == true
8282
Then -> @matches(td.matchers.contains('biz'), 'foobarbaz') == false
8383
Then -> shouldThrow (=> td.matchers.contains(48).__matches()), """
84-
Error: td.matchers.contains - this matcher only supports strings, arrays, and plain objects
84+
Error: testdouble.js - td.matchers.contains - this matcher only supports strings, arrays, and plain objects
8585
"""
8686

8787
context 'arrays', ->

test/src/replace/index-test.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe 'td.replace', ->
5353
And -> @doubleBag.age == 18
5454

5555
describe 'Replacing a property that is not an object/function', ->
56-
Given -> @message = 'Error: td.replace - "badType" property was found, but test double only knows how to replace functions, constructors, & objects containing functions (its value was '
56+
Given -> @message = 'Error: testdouble.js - td.replace - "badType" property was found, but test double only knows how to replace functions, constructors, & objects containing functions (its value was '
5757
When -> try
5858
td.replace(@dependency, 'badType')
5959
catch e
@@ -81,7 +81,7 @@ describe 'td.replace', ->
8181
td.replace(@dependency, 'notAThing')
8282
catch e
8383
@error = e
84-
Then -> @error.message == 'Error: td.replace - No "notAThing" property was found.'
84+
Then -> @error.message == 'Error: testdouble.js - td.replace - No "notAThing" property was found.'
8585

8686
context 'with manual replacement', ->
8787
Given -> @myFake = td.replace(@dependency, 'notAThing', 'MY FAKE')

0 commit comments

Comments
 (0)