Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positives on errors with different messages. #23

Merged
merged 3 commits into from
Aug 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
--reporter spec
--compilers js:babel-register
--require babel-polyfill
--ui tdd

src/specs/test.spec.js
153 changes: 152 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "effects-as-data",
"version": "2.11.0",
"version": "2.11.1",
"description": "A micro abstraction layer for Javascript that makes writing, testing, and monitoring side-effects easy.",
"main": "src/index.js",
"repository": {
Expand All @@ -9,7 +9,7 @@
},
"scripts": {
"test": "jest",
"test-mocha": "mocha src/**/*.spec.js --recursive --compilers js:babel-register --require babel-polyfill",
"test:mocha": "mocha --opts mocha.opts",
"transpile": "babel src --out-dir es5 --source-maps",
"deploy": "npm run transpile && npm test && npm publish; rm -rf es5",
"perf": "node src/perf/fs"
Expand All @@ -23,7 +23,8 @@
"babel-cli": "^6.24.1",
"babel-jest": "^20.0.0",
"babel-preset-es2015": "^6.24.1",
"jest-cli": "^20.0.0"
"jest-cli": "^20.0.0",
"mocha": "^3.5.0"
},
"jest": {
"testPathIgnorePatterns": [
Expand Down
27 changes: 26 additions & 1 deletion src/specs/test-util.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const assert = require("assert")

function errorToJson(e) {
const props = Object.getOwnPropertyNames(e).concat('name')
return props.reduce((p, c) => {
Expand Down Expand Up @@ -35,7 +37,30 @@ function sleep(ms) {
})
}

function deepEqual(actual, expected) {
const a = normalizeError(actual)
const e = normalizeError(expected)
if (usingJest()) expect(a).toEqual(e)
else assert.deepEqual(a, e)
}

function normalizeError(v) {
const isError = v instanceof Error
if (!isError) return v
const props = Object.getOwnPropertyNames(v).concat("name")
return props.reduce((p, c) => {
if (c === "stack") return p
p[c] = v[c]
return p
}, {})
}

function usingJest() {
return typeof expect !== "undefined" && expect.extend && expect.anything
}

module.exports = {
expectError,
sleep
sleep,
deepEqual
}
44 changes: 29 additions & 15 deletions src/specs/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
badHandler
} = functions
const { testFn, testFnV2, args } = require("../test")
const { deepEqual } = require("./test-util")

function* singleLine(id) {
const s1 = yield cmds.httpGet(`http://example.com/api/v1/users/${id}`)
Expand All @@ -34,9 +35,30 @@ function* rethrow() {
}
}

function* throwFoo() {
throw new Error("foo")
}

test(
"testFn should fail if the function error is different than the test error",() => {
try {
testFn(throwFoo, () => {
// prettier-ignore
return [
[ [], new Error("bar") ]
]
})()
} catch (e) {
deepEqual(e.name, 'Error on Step 1')
return
}
throw new Error("Failed: Did not compare error messages")
}
)

test(
"should be able to rethrow errors",
testFnV2(rethrow)(() => {
testFnV2(rethrow, () => {
// prettier-ignore
return [
[],
Expand Down Expand Up @@ -491,10 +513,10 @@ test("testFn should throw proper error if function throws incorrect error", () =
.throws(new Error("wrong"))
})()
} catch (e) {
expect(e.name).toEqual("Error on Step 2")
deepEqual(e.name, "Error on Step 2")
return
}
fail("Did not throw")
throw new Error("Failed: Did not throw")
})

// Single line
Expand Down Expand Up @@ -529,19 +551,15 @@ test("testFn should give proper error message if yielding array but no results",
]
})()
} catch (e) {
expect(e.message).toEqual(
"Your spec does not have as many steps as your function. Are you missing a return line?"
)
deepEqual(e.message, "Your spec does not have as many steps as your function. Are you missing a return line?")
}
})

test("testFn should give proper error message if spec is returning undefined", () => {
try {
testYieldArray(() => {})()
} catch (e) {
expect(e.message).toEqual(
'Your spec must return an array of tuples. It is currently returning a value of type "undefined".'
)
deepEqual(e.message, 'Your spec must return an array of tuples. It is currently returning a value of type "undefined".')
}
})

Expand All @@ -551,9 +569,7 @@ test("testFn should give proper error message if spec is returning an object", (
return {}
})()
} catch (e) {
expect(e.message).toEqual(
'Your spec must return an array of tuples. It is currently returning a value of type "object".'
)
deepEqual(e.message, 'Your spec must return an array of tuples. It is currently returning a value of type "object".')
}
})

Expand All @@ -563,8 +579,6 @@ test("testFn should give proper error message if spec is returning an string", (
return "what?"
})()
} catch (e) {
expect(e.message).toEqual(
'Your spec must return an array of tuples. It is currently returning a value of type "string".'
)
deepEqual(e.message, 'Your spec must return an array of tuples. It is currently returning a value of type "string".')
}
})
10 changes: 1 addition & 9 deletions src/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const assert = require("assert")
const curry = require("lodash/curry")
const chunk = require("lodash/chunk")
const { deepEqual } = require("./specs/test-util")

const testRunner = (fn, expected, index = 0, previousOutput = null) => {
checkForExpectedTypeMismatches(expected)
Expand Down Expand Up @@ -103,15 +104,6 @@ const testFnV2 = (fn, spec) => {
}
}

function deepEqual(actual, expected) {
// a little bit of jest support
if (typeof expect !== "undefined" && expect.extend && expect.anything) {
expect(actual).toEqual(expected)
} else {
assert.deepEqual(actual, expected)
}
}

// Semantic test builder
const args = (...fnArgs) => {
const t = [[fnArgs]]
Expand Down