Skip to content

Commit

Permalink
Merge pull request #55 from Srokap/more_assertions
Browse files Browse the repository at this point in the history
More assertions
  • Loading branch information
jakewmeyer authored Nov 13, 2017
2 parents 88075bf + 4b5cb4d commit f3907f2
Show file tree
Hide file tree
Showing 3 changed files with 369 additions and 15 deletions.
7 changes: 6 additions & 1 deletion routes/v1-launches.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const express = require("express")
const v1 = express.Router()
const error = {error: "No results found"}

// Get most recent launch
v1.get("/latest", (req, res, next) => {
Expand All @@ -10,7 +11,11 @@ v1.get("/latest", (req, res, next) => {
if (err) {
return next(err)
}
res.json(doc)
if (doc.length == 0) {
res.status(404)
return res.json(error)
}
res.json(doc[0])
})
})

Expand Down
103 changes: 103 additions & 0 deletions test/utilities/custom-asymmetric-matchers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* We make ourselves compatible with AsymmetricMatcher class used by Jest
* @see https://github.com/facebook/jest/blob/master/packages/expect/src/asymmetric_matchers.js
*
* Perhaps we can simplify that a bit and write only Jasmine-compatible matchers.
* @see https://jasmine.github.io/2.4/introduction.html#section-Custom_asymmetric_equality_tester
*/
class CustomAsymmetricMatcher {
constructor() {
// $$typeof is used internally by Jest and just to be sure let's use jest Symbol
this.$$typeof = Symbol.for("jest.asymmetricMatcher")
}
}

/**
* Expect metric and imperial volume numbers in object.
*/
class SpacexVolume extends CustomAsymmetricMatcher {
asymmetricMatch(any) {
expect(any).toEqual(expect.anything())
expect(any).toHaveProperty("cubic_meters", expect.any(Number))
expect(any).toHaveProperty("cubic_feet", expect.any(Number))
expect(any.cubic_meters).toBeGreaterThanOrEqual(0)
expect(any.cubic_feet).toBeGreaterThanOrEqual(0)
return true
}

toString() {
return "SpacexVolume"
}

getExpectedType() {
return "object"
}

toAsymmetricMatcher() {
return "SpacexVolume"
}
}

/**
* Expect metric and imperial length (or dimension) numbers in object.
*/
class SpacexLength extends CustomAsymmetricMatcher {
asymmetricMatch(any) {
expect(any).toEqual(expect.anything())
expect(any).toHaveProperty("meters", expect.any(Number))
expect(any).toHaveProperty("feet", expect.any(Number))
expect(any.meters).toBeGreaterThanOrEqual(0)
expect(any.feet).toBeGreaterThanOrEqual(0)
return true
}

toString() {
return "SpacexLength"
}

getExpectedType() {
return "object"
}

toAsymmetricMatcher() {
return "SpacexLength"
}
}

/**
* Expect metric and imperial mass numbers in object.
*/
class SpacexMass extends CustomAsymmetricMatcher {
asymmetricMatch(any) {
expect(any).toEqual(expect.anything())
expect(any).toHaveProperty("kg", expect.any(Number))
expect(any).toHaveProperty("lb", expect.any(Number))
expect(any.kg).toBeGreaterThanOrEqual(0)
expect(any.lb).toBeGreaterThanOrEqual(0)
return true
}

toString() {
return "SpacexMass"
}

getExpectedType() {
return "object"
}

toAsymmetricMatcher() {
return "SpacexMass"
}
}

module.exports = {
volume: () => {
return new SpacexVolume()
},
length: () => {
return new SpacexLength()
},
mass: () => {
return new SpacexMass()
}
}
Loading

0 comments on commit f3907f2

Please sign in to comment.