Muthu Kumar
7 years ago
17 changed files with 400 additions and 331 deletions
@ -0,0 +1,14 @@ |
|||
{ |
|||
// Use IntelliSense to learn about possible attributes. |
|||
// Hover to view descriptions of existing attributes. |
|||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 |
|||
"version": "0.2.0", |
|||
"configurations": [ |
|||
{ |
|||
"type": "node", |
|||
"request": "launch", |
|||
"name": "Launch Program", |
|||
"program": "${workspaceFolder}/sample.test.js" |
|||
} |
|||
] |
|||
} |
@ -1,6 +0,0 @@ |
|||
const constants = { |
|||
pass: 'pass', |
|||
fail: 'fail', |
|||
}; |
|||
|
|||
module.exports = constants; |
@ -1,67 +0,0 @@ |
|||
'use strict'; |
|||
|
|||
const { isPromise } = require('../../util/helpers'); |
|||
const { pass, fail } = require('./constants'); |
|||
|
|||
const runTests = instance => { |
|||
|
|||
const beforeAll = () => Promise.map( |
|||
instance.__hooks__.before['@start'], |
|||
hook => hook.run(), |
|||
); |
|||
|
|||
const beforeEvery = () => Promise.mapSeries( |
|||
instance.__hooks__.before['*'] || [], |
|||
hook => hook.run(), |
|||
); |
|||
|
|||
const runner = () => Promise.mapSeries(instance.__tests__, each => { |
|||
|
|||
const beforeThis = () => Promise.mapSeries( |
|||
instance.__hooks__.before[each.description] || [], |
|||
hook => hook.run(), |
|||
); |
|||
|
|||
const afterThis = () => Promise.mapSeries( |
|||
instance.__hooks__.after[each.description] || [], |
|||
hook => hook.run(), |
|||
); |
|||
|
|||
return beforeEvery().then(() => beforeThis()).then(() => { |
|||
|
|||
const pred = each.test(); |
|||
|
|||
/* There are 4 different cases at play: |
|||
1. A plain expect() is returned. |
|||
2. An array of [ expect() ] is returned |
|||
3. A plain expect() is wrapped in a promise |
|||
4. An array of [ expect() ] is wrapped in a promise. |
|||
Here we normalise all of them into something we can process */ |
|||
|
|||
if (!isPromise(pred) && !(pred && isPromise(pred[0]))) |
|||
throw new Error(`Malformed test '${each.description}'`); |
|||
const toTest = Array.isArray(pred) |
|||
? Promise.all(pred) |
|||
: pred.then(x => Array.isArray(x) ? Promise.all(x) : x); |
|||
|
|||
return toTest |
|||
.then(() => ({ description: each.description, result: pass })) |
|||
.catch(e => ({ description: each.description, result: fail, error: e })); |
|||
|
|||
}) |
|||
.then(result => afterThis().then(() => result)); |
|||
|
|||
}); |
|||
|
|||
const afterAll = () => Promise.mapSeries( |
|||
instance.__hooks__.before['@end'], |
|||
hook => hook.run(), |
|||
); |
|||
|
|||
return beforeAll() |
|||
.then(() => runner()) |
|||
.then(results => afterAll().then(() => results)); |
|||
|
|||
}; |
|||
|
|||
module.exports = runTests; |
@ -1 +1 @@ |
|||
module.exports = require('./gunner'); |
|||
module.exports = require('./src/gunner'); |
@ -1,9 +1,8 @@ |
|||
const isEq = require('@codefeathers/iseq'); |
|||
|
|||
const { liftPromise, stringify, isPromise } = require('../../util/helpers'); |
|||
const { liftPromise, stringify, isPromise } = require('../util'); |
|||
const _assertPromise = require('./assertPromise'); |
|||
|
|||
|
|||
const expectPromise = (pred, statement, options = {}) => |
|||
toTest => |
|||
(...testValues) => |
@ -0,0 +1,74 @@ |
|||
'use strict'; |
|||
|
|||
const { isPromise } = require('../util'); |
|||
const constants = require('../util/symbols'); |
|||
|
|||
const runTests = instance => { |
|||
|
|||
const beforeAll = () => Promise.map( |
|||
instance.__hooks__.before[constants.Start] || [], |
|||
hook => hook.run(), |
|||
); |
|||
|
|||
const beforeEvery = state => Promise.mapSeries( |
|||
instance.__hooks__.before['*'] || [], |
|||
hook => hook.run(state), |
|||
); |
|||
|
|||
const runner = state => Promise.mapSeries(instance.__tests__, each => { |
|||
|
|||
const beforeThis = state => Promise.mapSeries( |
|||
instance.__hooks__.before[each.description] || [], |
|||
hook => hook.run(state), |
|||
); |
|||
|
|||
const afterThis = state => Promise.mapSeries( |
|||
instance.__hooks__.after[each.description] || [], |
|||
hook => hook.run(state), |
|||
); |
|||
|
|||
return beforeEvery(state) |
|||
.then(newState => ({ ...state, '@every': newState })) |
|||
.then(state => Promise.object({ ...state, '@this': beforeThis() })) |
|||
.then(state => { |
|||
|
|||
const pred = each.test(state); |
|||
|
|||
/* There are 4 different cases at play: |
|||
1. A plain expect() is returned. |
|||
2. An array of [ expect() ] is returned |
|||
3. A plain expect() is wrapped in a promise |
|||
4. An array of [ expect() ] is wrapped in a promise. |
|||
Here we normalise all of them into something we can process */ |
|||
|
|||
if (!isPromise(pred) && !(pred && isPromise(pred[0]))) |
|||
throw new Error(`Malformed test '${each.description}'`); |
|||
const toTest = Array.isArray(pred) |
|||
? Promise.all(pred) |
|||
: pred.then(x => Array.isArray(x) ? Promise.all(x) : x); |
|||
|
|||
return [ |
|||
state, |
|||
toTest |
|||
.then(() => ({ description: each.description, result: constants.pass })) |
|||
.catch(e => ({ description: each.description, result: constants.fail, error: e })), |
|||
]; |
|||
|
|||
}) |
|||
.spread((state, result) => afterThis(state).then(() => result)); |
|||
|
|||
}); |
|||
|
|||
const afterAll = state => Promise.mapSeries( |
|||
instance.__hooks__.before[constants.End] || [], |
|||
hook => hook.run(state, state['@results']), |
|||
); |
|||
|
|||
return Promise.object({ '@start': beforeAll() }) |
|||
.then(state => Promise.object({ ...state, '@results': runner(state)})) |
|||
.then(state => Promise.object({ ...state, '@end': afterAll(state) })) |
|||
.then(state => state['@results']); |
|||
|
|||
}; |
|||
|
|||
module.exports = runTests; |
@ -0,0 +1,9 @@ |
|||
module.exports = { |
|||
|
|||
Start : Symbol('Start'), |
|||
End : Symbol('End'), |
|||
|
|||
pass: 'pass', |
|||
fail: 'fail', |
|||
|
|||
}; |
Loading…
Reference in new issue