Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

given-when-then

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

given-when-then - npm Package Compare versions

Comparing version 1.3.1-alpha to 1.3.1-alpha.1

124

gwt/lib/common/gwt.js
// Generated by CoffeeScript 1.9.2
(function() {
var I, Q, _, assert, configure, uuid,
var I, Q, Result, _, assert, buildGwt, hasNestedResults, uuid,
slice = [].slice;

@@ -16,40 +16,51 @@

configure = function(arg) {
var Result, buildDescription, configOptions, crossCombineResults, describeScenario, exports, getCounts, interpolate, isRunner, lastResult, makeResult, options, resolveResultArray, resolveResultObject;
exports = arg.exports, options = arg.options;
assert(exports, 'Exports object required');
assert(options, 'Options object required');
configOptions = options;
Result = (function() {
function Result(id1) {
this.id = id1;
assert(this.id, 'Result id not given');
this.value = null;
Result = (function() {
function Result(id1) {
this.id = id1;
assert(this.id, 'Result id not given');
this.value = null;
}
Result.prototype.getFromContext = function(context) {
if (this.overriden) {
return this.value;
} else {
return context[this.id];
}
};
Result.prototype.getFromContext = function(context) {
if (this.overriden) {
return this.value;
} else {
return context[this.id];
}
};
Result.prototype.setInContext = function(context, value) {
return context[this.id] = value;
};
Result.prototype.setInContext = function(context, value) {
return context[this.id] = value;
};
Result.prototype.set = function(value1) {
this.value = value1;
return this.overriden = true;
};
Result.prototype.set = function(value1) {
this.value = value1;
return this.overriden = true;
};
return Result;
return Result;
})();
})();
hasNestedResults = function(result) {
var r, rkey;
for (rkey in result) {
r = result[rkey];
if (r instanceof Result) {
return true;
}
}
return false;
};
buildGwt = function(arg) {
var buildDescription, configOptions, crossCombineResults, describeScenario, exports, getCounts, interpolate, isRunner, lastResult, makeResult, options, resolveResultArray, resolveResultObject;
options = arg.options;
exports = {};
assert(options, 'Options object required');
configOptions = options;
exports.configure = function(arg1) {
var bddIt, ref;
bddIt = arg1.it;
return configure({
exports: exports,
return buildGwt({
options: _.extend({}, configOptions, {

@@ -231,3 +242,3 @@ bddIt: (ref = configOptions.bddIt) != null ? ref : bddIt

isRunner = function(fn) {
return fn._isRunner;
return fn != null ? fn._isRunner : void 0;
};

@@ -310,3 +321,3 @@ buildDescription = function(fullDescription) {

return function(context, extraContext, args) {
var newContext, ref;
var newContext, ref, resultToUnwrap, resultWrapped;
if (!configOptions.sharedContext) {

@@ -318,4 +329,9 @@ newContext = _.extend({}, context, extraContext);

}
return Q(fn.apply(newContext, resolveResultArray((ref = crossCombineResults.getFromContext(context)) != null ? ref : {}, args))).then(function(result) {
var nextStep;
resultWrapped = fn.apply(newContext, resolveResultArray((ref = crossCombineResults.getFromContext(context)) != null ? ref : {}, args));
resultToUnwrap = isRunner(resultWrapped) ? {
_runner: resultWrapped
} : resultWrapped;
return Q(resultToUnwrap).then(function(thenResult) {
var nextStep, result;
result = (thenResult != null ? thenResult._runner : void 0) ? thenResult._runner : thenResult;
nextStep = function(result) {

@@ -326,3 +342,7 @@ lastResult.setInContext(newContext, result);

};
if (typeof result === 'function') {
if (isRunner(result)) {
return result.run({
world: newContext
}).then(nextStep);
} else if (typeof result === 'function') {
return Q.denodeify(result)().then(nextStep);

@@ -512,3 +532,4 @@ } else {

run = function(options, done) {
var bddIt, fail, finish, multipleIt, ref, ref1, ref2, testBodyFn, world;
var bddIt, fail, finish, multipleIt, ref, ref1, ref2, runResult, testBodyFn, world;
assert(!done || typeof done === 'function', 'Done isnt a function');
ref = options != null ? options : {}, bddIt = ref.bddIt, multipleIt = ref.multipleIt, world = ref.world;

@@ -534,5 +555,6 @@ if (bddIt == null) {

ref2 = handlers(done), finish = ref2.finish, fail = ref2.fail;
return testBodyFn().then(finish).then(function() {
runResult = testBodyFn().then(finish).then(function() {
return typeof spec.done === "function" ? spec.done() : void 0;
}).fail(fail);
return runResult;
}

@@ -546,7 +568,11 @@ };

run: function() {
var arg2, cb, j, options;
arg2 = 2 <= arguments.length ? slice.call(arguments, 0, j = arguments.length - 1) : (j = 0, []), cb = arguments[j++];
options = arg2[0];
if (options == null) {
options = {};
var args, cb;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
assert(!cb || typeof cb === 'function', 'Cb is not a function');
options = {};
cb = null;
if (typeof args[0] === 'function') {
cb = args[0];
} else {
options = args[0], cb = args[1];
}

@@ -559,3 +585,3 @@ return run(options, cb);

thenFn: function(context) {
var lastResultValue, r, ref, results, rkey;
var j, key, lastResultValue, len, r, ref, ref1, results, rkey;
results = (ref = crossCombineResults.getFromContext(context)) != null ? ref : {};

@@ -566,3 +592,3 @@ lastResultValue = lastResult.getFromContext(context);

result.setInContext(results, lastResultValue);
} else {
} else if (hasNestedResults(result)) {
for (rkey in result) {

@@ -573,2 +599,9 @@ r = result[rkey];

}
} else {
ref1 = Object.keys(result);
for (j = 0, len = ref1.length; j < len; j++) {
key = ref1[j];
delete result[key];
}
_.extend(result, lastResultValue);
}

@@ -698,4 +731,3 @@ crossCombineResults.setInContext(context, results);

module.exports = function(options) {
return configure({
exports: {},
return buildGwt({
options: options

@@ -702,0 +734,0 @@ });

{
"name": "given-when-then",
"version": "1.3.1-alpha",
"version": "1.3.1-alpha.1",
"description": "Given, when, then",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -5,37 +5,35 @@ <div id="table-of-contents">

<ul>
<li><a href="#sec-1">1. Given, when, then</a>
<li><a href="#orgheadline22">1. Given, when, then V1.3.1-alpha</a>
<ul>
<li><a href="#sec-1-1">1.1. Declaring steps, building a test, and running the test</a>
<li><a href="#orgheadline8">1.1. Declaring steps, building a test, and running the test</a>
<ul>
<li><a href="#sec-1-1-1">1.1.1. Steps declaration</a></li>
<li><a href="#sec-1-1-2">1.1.2. Building a test</a></li>
<li><a href="#sec-1-1-3">1.1.3. Running a test directly</a></li>
<li><a href="#sec-1-1-4">1.1.4. Running a test using mocha</a></li>
<li><a href="#sec-1-1-5">1.1.5. Override `it`</a></li>
<li><a href="#orgheadline1">1.1.1. Steps declaration</a></li>
<li><a href="#orgheadline2">1.1.2. Building a test</a></li>
<li><a href="#orgheadline5">1.1.3. Running a test directly</a></li>
<li><a href="#orgheadline6">1.1.4. Running a test using mocha</a></li>
<li><a href="#orgheadline7">1.1.5. Override `it`</a></li>
</ul>
</li>
<li><a href="#sec-1-2">1.2. Context</a>
<li><a href="#orgheadline10">1.2. Context</a>
<ul>
<li><a href="#sec-1-2-1">1.2.1. Normal use of context, without lexical closures:</a></li>
<li><a href="#sec-1-2-2">1.2.2. Writing to a previous context object does not carry over to the rest:</a></li>
<li><a href="#sec-1-2-3">1.2.3. Using `getContext` from within closures to retrieve the current context</a></li>
<li><a href="#orgheadline9">1.2.1. Normal use of context, without lexical closures:</a></li>
</ul>
</li>
<li><a href="#sec-1-3">1.3. Asynchronous steps</a>
<li><a href="#orgheadline13">1.3. Asynchronous steps</a>
<ul>
<li><a href="#sec-1-3-1">1.3.1. Steps can return promises:</a></li>
<li><a href="#sec-1-3-2">1.3.2. Steps can use callbacks:</a></li>
<li><a href="#orgheadline11">1.3.1. Steps can return promises:</a></li>
<li><a href="#orgheadline12">1.3.2. Steps can use callbacks:</a></li>
</ul>
</li>
<li><a href="#sec-1-4">1.4. Results can be retrieved from and passed back into steps</a>
<li><a href="#orgheadline19">1.4. Results can be retrieved from and passed back into steps</a>
<ul>
<li><a href="#sec-1-4-1">1.4.1. Single results can be returned from and passed into steps</a></li>
<li><a href="#sec-1-4-2">1.4.2. Multiple results can be passed into steps</a></li>
<li><a href="#sec-1-4-3">1.4.3. Multiple results can be returned from steps</a></li>
<li><a href="#sec-1-4-4">1.4.4. Results can be permanently overriden with `set`</a></li>
<li><a href="#sec-1-4-5">1.4.5. Use `tap()` instead of `result.set`</a></li>
<li><a href="#orgheadline14">1.4.1. Single results can be returned from and passed into steps</a></li>
<li><a href="#orgheadline15">1.4.2. Multiple results can be passed into steps</a></li>
<li><a href="#orgheadline16">1.4.3. Multiple results can be returned from steps</a></li>
<li><a href="#orgheadline17">1.4.4. Results can be permanently overriden with `set`</a></li>
<li><a href="#orgheadline18">1.4.5. Use `tap()` instead of `result.set`</a></li>
</ul>
</li>
<li><a href="#sec-1-5">1.5. Steps can be combined from multiple declarations using `gwt.combine(&#x2026;)`</a></li>
<li><a href="#sec-1-6">1.6. Insert a custom function call without a step declaration (debugging)</a></li>
<li><a href="#orgheadline20">1.5. Steps can be combined from multiple declarations using `gwt.combine(&#x2026;)`</a></li>
<li><a href="#orgheadline21">1.6. Insert a custom function call without a step declaration (debugging)</a></li>
</ul>

@@ -47,9 +45,9 @@ </li>

# Given, when, then<a id="sec-1" name="sec-1"></a>
# Given, when, then V1.3.1-alpha<a id="orgheadline22"></a>
Behaviour driven development for nodejs.
## Declaring steps, building a test, and running the test<a id="sec-1-1" name="sec-1-1"></a>
## Declaring steps, building a test, and running the test<a id="orgheadline8"></a>
### Steps declaration<a id="sec-1-1-1" name="sec-1-1-1"></a>
### Steps declaration<a id="orgheadline1"></a>

@@ -76,8 +74,4 @@ The available dictionary of steps has to be created before

NOTE: context is a new object in each step, with each key/value pair copied
across to the new context, which is provided to the subsequent
step. (See below)
### Building a test<a id="orgheadline2"></a>
### Building a test<a id="sec-1-1-2" name="sec-1-1-2"></a>
The order of execution is declared using the dictionary of

@@ -94,3 +88,3 @@ steps. Steps can be used multiple times and in any order.

### Running a test directly<a id="sec-1-1-3" name="sec-1-1-3"></a>
### Running a test directly<a id="orgheadline5"></a>

@@ -107,3 +101,3 @@ 1. Using a callback

### Running a test using mocha<a id="sec-1-1-4" name="sec-1-1-4"></a>
### Running a test using mocha<a id="orgheadline6"></a>

@@ -113,3 +107,3 @@ # `done()` registers with `it`

### Override \`it\`<a id="sec-1-1-5" name="sec-1-1-5"></a>
### Override \`it\`<a id="orgheadline7"></a>

@@ -119,24 +113,16 @@ # `done()` registers with `it`

## Context<a id="sec-1-2" name="sec-1-2"></a>
## Context<a id="orgheadline10"></a>
Each step has access to a context object, via \`this\`, which is copied
from step to step.
Each step has access to a context object, via \`this\`, which is shared
between all steps attached to a runner.
CAVEAT: Each step has its own context object, with values from
previous contexts copied across. This creates unexpected behaviour
when trying to set values inside the context from inside a closure.
### Normal use of context, without lexical closures:<a id="orgheadline9"></a>
If you create a function within a step, and call it later, its lexical scope points to an old context.
You can retrieve the latest context through the function \`getContext\`
held within each context object.
### Normal use of context, without lexical closures:<a id="sec-1-2-1" name="sec-1-2-1"></a>
steps = gwt.steps
GIVEN: 'a given': ->
@bar = 'x'
WHEN: 'an action is taken': ->
assert.equal @bar, 'x', 'Set in the wrong context' # -> PASS
assert.equal @bar, 'x', 'Context not shared' # -> PASS
steps

@@ -147,43 +133,6 @@ .given 'a given'

### Writing to a previous context object does not carry over to the rest:<a id="sec-1-2-2" name="sec-1-2-2"></a>
## Asynchronous steps<a id="orgheadline13"></a>
steps = gwt.steps
GIVEN: 'a given': ->
context = this
@setBar = ->
context.bar = 'x'
### Steps can return promises:<a id="orgheadline11"></a>
WHEN: 'an action is taken': ->
@setBar()
assert.equal @bar, 'x', 'Set in the wrong context' # -> ERROR
steps
.given 'a given'
.when 'an action is taken'
.run (err) -> ...
### Using \`getContext\` from within closures to retrieve the current context<a id="sec-1-2-3" name="sec-1-2-3"></a>
To get this to work, use getContext, which returns the current
context.
steps = gwt.steps
GIVEN: 'a given': ->
context = this
@setBar = ->
context.getContext().bar = 'x'
WHEN: 'an action is taken': ->
@setBar()
assert.equal @bar, 'x', 'Set in the wrong context' # -> PASS
steps
.given 'a given'
.when 'an action is taken'
.run (err) -> ...
## Asynchronous steps<a id="sec-1-3" name="sec-1-3"></a>
### Steps can return promises:<a id="sec-1-3-1" name="sec-1-3-1"></a>
If the return value of a step is a promise, it will

@@ -198,6 +147,6 @@ be used to chain onto the following steps.

return deferred.promise
steps.run()
### Steps can use callbacks:<a id="sec-1-3-2" name="sec-1-3-2"></a>
### Steps can use callbacks:<a id="orgheadline12"></a>

@@ -211,6 +160,6 @@ If the return value of a step is a function, it is assumed

setTimeout (-> cb()), 1000
steps.run()
## Results can be retrieved from and passed back into steps<a id="sec-1-4" name="sec-1-4"></a>
## Results can be retrieved from and passed back into steps<a id="orgheadline19"></a>

@@ -220,13 +169,13 @@ \`gwt.result()\` produces a placeholder that carries information via

### Single results can be returned from and passed into steps<a id="sec-1-4-1" name="sec-1-4-1"></a>
### Single results can be returned from and passed into steps<a id="orgheadline14"></a>
baz = gwt.result()
steps = gwt.steps
WHEN: 'baz is created': ->
return baz: 'xyz'
THEN: 'baz can be used': ({baz}) ->
assert.deepEqual baz, baz: 'xyz'
steps

@@ -237,7 +186,7 @@ .when('baz is created').resultTo(baz)

### Multiple results can be passed into steps<a id="sec-1-4-2" name="sec-1-4-2"></a>
### Multiple results can be passed into steps<a id="orgheadline15"></a>
baz = gwt.result()
foo = gwt.result()
steps = gwt.steps

@@ -247,10 +196,10 @@ WHEN:

return 'xyz'
'foo is created': -> (cb) ->
cb null, 'foo'
THEN: 'results can be used': ({baz, foo}) ->
assert.equal baz, 'xyz'
assert.equal foo, 'foo'
steps

@@ -261,7 +210,7 @@ .when('baz is created').resultTo(baz)

### Multiple results can be returned from steps<a id="sec-1-4-3" name="sec-1-4-3"></a>
### Multiple results can be returned from steps<a id="orgheadline16"></a>
baz = gwt.result()
foo = gwt.result()
steps = gwt.steps

@@ -271,7 +220,7 @@ WHEN:

return foo: 'foo', baz: 'xyz'
THEN: 'results can be used': ({baz, foo}) ->
assert.equal baz, 'xyz'
assert.equal foo, 'foo'
steps

@@ -282,3 +231,3 @@ .when('foo and baz are created').resultTo({baz, foo})

### Results can be permanently overriden with \`set\`<a id="sec-1-4-4" name="sec-1-4-4"></a>
### Results can be permanently overriden with \`set\`<a id="orgheadline17"></a>

@@ -292,7 +241,7 @@ If you call \`result.set\` with a value, any time it is passed

value.set 'xyz'
steps = gwt.steps
THEN: 'result can be used': ({value}) ->
assert.equal baz, 'xyz'
steps

@@ -302,3 +251,3 @@ .then('result can be used', {value})

### Use \`tap()\` instead of \`result.set\`<a id="sec-1-4-5" name="sec-1-4-5"></a>
### Use \`tap()\` instead of \`result.set\`<a id="orgheadline18"></a>

@@ -309,3 +258,3 @@ Using \`tap()\` provides a less permanent way of setting a result

baz = gwt.result()
steps = gwt.steps

@@ -315,3 +264,3 @@ THEN:

assert.equal baz, 'xyz'
steps

@@ -322,3 +271,3 @@ .tap(({baz} -> return 'xyz'), {baz})

## Steps can be combined from multiple declarations using \`gwt.combine(&#x2026;)\`<a id="sec-1-5" name="sec-1-5"></a>
## Steps can be combined from multiple declarations using \`gwt.combine(&#x2026;)\`<a id="orgheadline20"></a>

@@ -337,3 +286,3 @@ Calls to \`gwt.steps(&#x2026;).given().when().then()\` produce a runner,

THEN: 'two': ->
steps2 = gwt.steps

@@ -343,3 +292,3 @@ GIVEN: 'three': ->

THEN: 'five': ->
gwt.combine(

@@ -349,3 +298,3 @@ steps1

.then 'two'
steps2

@@ -357,3 +306,3 @@ .given 'three'

## Insert a custom function call without a step declaration (debugging)<a id="sec-1-6" name="sec-1-6"></a>
## Insert a custom function call without a step declaration (debugging)<a id="orgheadline21"></a>

@@ -364,3 +313,3 @@ You can access context and result values by providing a function

baz = gwt.result()
steps = gwt.steps

@@ -370,3 +319,3 @@ WHEN:

return 'xyz'
steps

@@ -373,0 +322,0 @@ .when('baz is created').resultTo(baz)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc