Skip to content

Commit

Permalink
test(*): add missing tests
Browse files Browse the repository at this point in the history
I hate myself for not writing tests at first place and everyone faced issues coz of same. Fixing it
  • Loading branch information
thetutlage committed Jan 31, 2017
1 parent 972b718 commit 0918cb5
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 15 deletions.
Binary file added .package.json.swp
Binary file not shown.
13 changes: 3 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
language: node_js
env:
- CXX=g++-4.8
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
node_js:
- node
- 5.3.0
- 6.0.0
- 5.0.0
- 4.0.0
sudo: false
install:
- npm install --no-optional
- npm install
notifications:
slack:
secure: m91zkX2cLVDRDMBAUnR1d+hbZqtSHXLkuPencHadhJ3C3wm53Box8U25co/goAmjnW5HNJ1SMSIg+DojtgDhqTbReSh5gSbU0uU8YaF8smbvmUv3b2Q8PRCA7f6hQiea+a8+jAb7BOvwh66dV4Al/1DJ2b4tCjPuVuxQ96Wll7Pnj1S7yW/Hb8fQlr9wc+INXUZOe8erFin+508r5h1L4Xv0N5ZmNw+Gqvn2kPJD8f/YBPpx0AeZdDssTL0IOcol1+cDtDzMw5PAkGnqwamtxhnsw+i8OW4avFt1GrRNlz3eci5Cb3NQGjHxJf+JIALvBeSqkOEFJIFGqwAXMctJ9q8/7XyXk7jVFUg5+0Z74HIkBwdtLwi/BTyXMZAgsnDjndmR9HsuBP7OSTJF5/V7HCJZAaO9shEgS8DwR78owv9Fr5er5m9IMI+EgSH3qtb8iuuQaPtflbk+cPD3nmYbDqmPwkSCXcXRfq3IxdcV9hkiaAw52AIqqhnAXJWZfL6+Ct32i2mtSaov9FYtp/G0xb4tjrUAsDUd/AGmMJNEBVoHtP7mKjrVQ35cEtFwJr/8SmZxGvOaJXPaLs43dhXKa2tAGl11wF02d+Rz1HhbOoq9pJvJuqkLAVvRdBHUJrB4/hnTta5B0W5pe3mIgLw3AmOpk+s/H4hAP4Hp0gOWlPA=
24 changes: 24 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
environment:
matrix:
- nodejs_version: 'Stable'
- nodejs_version: '6'
- nodejs_version: '5'
- nodejs_version: '4'

init:
git config --global core.autocrlf true

install:
- ps: Install-Product node $env:nodejs_version
- npm install

test_script:
- node --version
- npm --version
- npm run test:win

build: off
clone_depth: 1

matrix:
fast_finish: true
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@
"example": "examples"
},
"scripts": {
"test": "npm run lint && node test/youch.spec.js",
"lint": "standard src/Youch"
},
"author": "amanvirk",
"license": "MIT",
"devDependencies": {
"standard": "^8.6.0"
"japa": "^1.0.0",
"standard": "^8.6.0",
"supertest": "^3.0.0"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"dependencies": {
"cookie": "^0.3.1",
"mustache": "^2.3.0",
"stack-trace": "0.0.9"
},
Expand Down
8 changes: 4 additions & 4 deletions src/Youch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const Mustache = require('mustache')
const path = require('path')
const stackTrace = require('stack-trace')
const fs = require('fs')
const cookie = require('cookie')
const VIEW_PATH = '../resources/error.mustache'
const startingSlashRegex = /\\|\//

Expand Down Expand Up @@ -204,7 +205,6 @@ class Youch {
*/
_serializeRequest () {
const headers = []
const headerCookies = (this.request.headers.cookie || '').split(/;\s/)

Object.keys(this.request.headers).forEach((key) => {
if (this._filterHeaders.indexOf(key) > -1) {
Expand All @@ -216,9 +216,9 @@ class Youch {
})
})

const cookies = Object.keys(headerCookies).map((cookie) => {
const [key, value] = headerCookies[cookie].split(/=(.*)/)
return {key, value}
const parsedCookies = cookie.parse(this.request.headers.cookie || '')
const cookies = Object.keys(parsedCookies).map((key) => {
return {key, value: parsedCookies[key]}
})

return {
Expand Down
Binary file added test/.youch.spec.js.swp
Binary file not shown.
126 changes: 126 additions & 0 deletions test/youch.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
'use strict'

const http = require('http')
const supertest = require('supertest')
const test = require('japa')
const Youch = require('../src/Youch')

const DEFAULT_PORT=8000

test.group('Youch', () => {

test('initiate a new instance by passing error object', (assert) => {
const error = new Error('foo')
const youch = new Youch(error, {})
assert.equal(youch.error.message, 'foo')
assert.deepEqual(youch.request, {})
})

test('parse the error into frames', (assert, done) => {
assert.plan(2)
const error = new Error('foo')
const youch = new Youch(error, {})
youch
._parseError()
.then((stack) => {
assert.equal(stack[0].fileName, __filename)
assert.equal(stack[0].native, false)
done()
}).catch(done)
})

test('parse stack frame context to tokens', (assert, done) => {
const error = new Error('this is bar')
const youch = new Youch(error, {})

youch
._parseError()
.then((stack) => {
const context = youch._getContext(stack[0])
assert.equal(context.line.trim(), 'const error = new Error(\'this is bar\')')
done()
})
.catch(done)
})

test('return active class when index is 0', (assert) => {
const error = new Error('this is bar')
const youch = new Youch(error, {})
const frame = {
isNative: () => false,
getFileName: () => './hello.js'
}

const classes = youch._getDisplayClasses(frame, 0)
assert.equal(classes, 'active')
})

test('return native frame class when frame is native', (assert) => {
const error = new Error('this is bar')
const youch = new Youch(error, {})
const frame = {
isNative: () => true,
getFileName: () => './hello.js'
}

const classes = youch._getDisplayClasses(frame, 0)
assert.equal(classes, 'active native-frame')
})

test('return native frame class when frame is from node_modules', (assert) => {
const error = new Error('this is bar')
const youch = new Youch(error, {})
const frame = {
isNative: () => false,
getFileName: () => './node_modules/hello.js'
}

const classes = youch._getDisplayClasses(frame, 0)
assert.equal(classes, 'active native-frame')
})

test('serialize http request', (assert, done) => {
const server = http.createServer((req, res) => {
const youch = new Youch({}, req)
res.writeHead(200, {'content-type': 'application/json'})
res.write(JSON.stringify(youch._serializeRequest()))
res.end()
}).listen(DEFAULT_PORT)

supertest(server).get('/').end((error, response) => {
if (error) {
done(error)
return
}

assert.isArray(response.body.cookies)
assert.deepEqual(response.body.cookies, [])
assert.equal(response.body.url, '/')
assert.isArray(response.body.headers)
server.close()
done()
})
})

test('serialize http request and return cookies from it', (assert, done) => {
const server = http.createServer((req, res) => {
const youch = new Youch({}, req)
res.writeHead(200, {'content-type': 'application/json'})
res.write(JSON.stringify(youch._serializeRequest()))
res.end()
}).listen(DEFAULT_PORT)

supertest(server).get('/').set('Cookie', 'name=virk; Path=/').end((error, response) => {
if (error) {
done(error)
return
}

assert.isArray(response.body.cookies)
assert.deepEqual(response.body.cookies, [{key: 'name', value: 'virk'}, {key: 'Path', value: '/'}])
assert.equal(response.body.url, '/')
assert.isArray(response.body.headers)
done()
})
})
})

0 comments on commit 0918cb5

Please sign in to comment.