Skip to content
This repository has been archived by the owner on Feb 27, 2022. It is now read-only.

Commit

Permalink
feat(*): that fat commit
Browse files Browse the repository at this point in the history
completed base implementation required for test runner
  • Loading branch information
thetutlage committed Jun 1, 2017
1 parent 12f7303 commit d14f0bc
Show file tree
Hide file tree
Showing 20 changed files with 984 additions and 230 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,36 @@ function (suite) {



## Folder Structure

```
|__ Functional
|_____ test.spec.js
|__ Unit
|_____ test.spec.js
```

Also one can have `vowFile.js` to define the cli behavior

```js
const cli = use('Test/Cli')

cli.envFile('') // custom env file

cli.filter(function () {
})

cli.grep('custom grep')

cli.unit('') // define path to unit tests
cli.functional('') // define path to functional tests

// Add traits to suites globally
cli.suite('glob pattern', function ({ path, trait }) {
if (path.endsWith('browser.js')) {
trait(['Test/Browser'], { driver: 'chrome' })
}
})
```


38 changes: 38 additions & 0 deletions commands/RunTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict'

/*
* adonis-vow
*
* (c) Harminder Virk <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const { Command } = require('adonis-ace')

class RunTests extends Command {
static get signature () {
return `test
{ type?: Define test types, needs to be unit or functional }
{ --bail: Stop running tests on first failure }
{ --timeout: Define a global timeout for all the tests }
{ --files: Pick only specific files. File names are seperated by comma }`
}

static get description () {
return 'Run application tests'
}

constructor (runner, cli) {
super()
this.runner = runner
this.cli = cli
}

handle ({ type }, { bail, timeout, files }) {
console.log(type)
}
}

module.exports = RunTests
14 changes: 14 additions & 0 deletions japaFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

/*
* adonis-vow
*
* (c) Harminder Virk <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const cli = require('japa/cli')

cli.run('test/*/*.spec.js')
16 changes: 14 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,34 @@
"test:win": "set DEBUG=adonis:vow && node --harmony-async-await ./node_modules/japa-cli/index.js",
"coverage": "nyc report --reporter=text-lcov | coveralls"
},
"standard": {
"globals": [
"use"
]
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"adonis-ace": "git+https://github.com/adonisjs/ace.git#dawn",
"adonis-fold": "git+https://github.com/poppinss/adonis-fold.git#dawn",
"adonis-sink": "^1.0.2",
"adonis-sink": "^1.0.3",
"coveralls": "^2.13.1",
"cz-conventional-changelog": "^2.0.0",
"fs-extra": "^3.0.1",
"japa": "git+https://github.com/thetutlage/japa.git#develop",
"japa-cli": "^1.0.1",
"node-res": "^3.0.1",
"nyc": "^10.3.2",
"standard": "^10.0.2"
},
"dependencies": {
"glob": "^7.1.2",
"japa": "git+https://github.com/thetutlage/japa.git#develop",
"p-series": "^1.0.0"
"lodash": "^4.17.4",
"macroable": "^1.0.0",
"p-series": "^1.0.0",
"superagent": "^3.5.2"
},
"config": {
"commitizen": {
Expand Down
34 changes: 31 additions & 3 deletions providers/VowProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,48 @@
*/

const { ServiceProvider } = require('adonis-fold')
const _ = require('lodash')

class VowProvider extends ServiceProvider {
register () {
this.app.singleton('Test/Runner', (app) => {
const Runner = require('../src/Runner')
return new Runner(app.use('Adonis/Src/Config'))
return new Runner(app.use('Adonis/Src/Env'))
})

this.app.singleton('Test/Cli', (app) => {
const Env = app.use('Adonis/Src/Env')
const Cli = require('../src/Cli')
return new Cli(Env)
})

this.app.bind('Test/Suite', (app) => {
const Runner = app.use('Test/Runner')
return function (title) {
return Runner.suite(title)
return (title) => {
const suite = Runner.suite(title)
return _.transform(Object.getOwnPropertyNames(Object.getPrototypeOf(suite)), (result, prop) => {
result[prop] = function (...args) {
return suite[prop](...args)
}
}, {})
}
})

this.app.bind('Test/ApiClient', (app) => {
const Env = app.use('Adonis/Src/Env')
const ApiClient = require('../src/ApiClient')

return function ({ Context }) {
Context.getter('client', function () {
return new ApiClient(Env, this.assert)
}, true)
}
})

this.app.bind('Adonis/Commands/Test', (app) => {
const RunTests = require('../commands/RunTests')
return new RunTests(app.use('Test/Runner'), app.use('Test/Cli'))
})
}
}

Expand Down
67 changes: 67 additions & 0 deletions src/ApiClient/Request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict'

/*
* adonis-vow
*
* (c) Harminder Virk <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const superagent = require('superagent')
const Response = require('./Response')

const superagentMethods = [
'send',
'set',
'query',
'type',
'retry',
'accept',
'key',
'cert',
'timeout',
'redirects',
'attach',
'withCredentials',
'accept'
]

class Request {
constructor (url, verb, assert) {
this.agent = superagent[verb](url)
this._assert = assert
}

then (userResolve, userReject) {
if (!this._fullfilledPromise) {
this._fullfilledPromise = new Promise((resolve) => {
this.agent.end((error, response) => {
if (error) {
resolve(new Response(error, this._assert))
return
}
resolve(new Response(response, this._assert))
})
})
}
return this._fullfilledPromise.then(userResolve, userReject)
}

catch (callback) {
return this.then(undefined, callback)
}

loginAs (user) {
}
}

superagentMethods.forEach((method) => {
Request.prototype[method] = function (...args) {
this.agent[method](...args)
return this
}
})

module.exports = Request
67 changes: 67 additions & 0 deletions src/ApiClient/Response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict'

/*
* adonis-vow
*
* (c) Harminder Virk <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const urlModule = require('url')

class Response {
constructor (resOrError, assert) {
this._assert = assert
this._isError = resOrError.name === 'Error'
this._res = this._isError ? resOrError.response : resOrError
}

get status () {
return this._res.status
}

get text () {
return this._res.text
}

get body () {
return this._res.body
}

get redirects () {
return this._res.redirects
}

assertStatus (expected) {
this._assert.equal(this.status, expected)
}

assertText (expected) {
this._assert.equal(this.text, expected)
}

assertBody (expected) {
try {
this._assert.deepEqual(this.body, expected)
} catch (error) {
this._assert.equal(this.text, expected)
}
}

assertJSON (expected) {
this._assert.deepEqual(this.body, expected)
}

assertError (expected) {
this.assertBody(expected)
}

assertRedirect (expectedUrl) {
const routes = this.redirects.map((url) => urlModule.parse(url).pathname)
this._assert.include(routes, expectedUrl, `Request was not redirected to ${expectedUrl}`)
}
}

module.exports = Response
43 changes: 43 additions & 0 deletions src/ApiClient/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict'

/*
* adonis-vow
*
* (c) Harminder Virk <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

const Request = require('./Request')
const apiMethods = ['get', 'post', 'put', 'delete', 'patch']

class ApiClient {
constructor (Env, assert) {
this._baseUrl = Env.get('TEST_SERVER_URL')
this._assert = assert
}

/**
* Returns complete url to the http server.
*
* @method _getCompleteUrl
*
* @param {String} url
*
* @return {String}
*
* @private
*/
_getCompleteUrl (url) {
return `${this._baseUrl}/${url}`.replace(/([^:]\/)\/+/g, '$1')
}
}

apiMethods.forEach((method) => {
ApiClient.prototype[method] = function (url) {
return new Request(this._getCompleteUrl(url), method, this._assert)
}
})

module.exports = ApiClient
Loading

0 comments on commit d14f0bc

Please sign in to comment.