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

Commit

Permalink
promises support in steps
Browse files Browse the repository at this point in the history
closes #6
  • Loading branch information
just-boris committed Sep 15, 2015
1 parent d7686d0 commit 8d91900
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
18 changes: 15 additions & 3 deletions runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,33 @@ var Allure = function(allure) {
this._allure = allure;
};

Allure.prototype.isPromise = function(obj) {
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
};

Allure.prototype.createStep = function(name, stepFunc) {
var that = this;
return function() {
var stepName = that._format(name, Array.prototype.slice.call(arguments, 0)),
status = 'passed';
status = 'passed',
result;
that._allure.startStep(stepName);
try {
var result = stepFunc.apply(this, arguments);
result = stepFunc.apply(this, arguments);
}
catch(error) {
status = 'broken';
throw error;
}
finally {
that._allure.endStep(status);
if(that.isPromise(result)) {
result.then(
that._allure.endStep.bind(that._allure, 'passed'),
that._allure.endStep.bind(that._allure, 'broken')
);
} else {
that._allure.endStep(status);
}
}
return result;
};
Expand Down
48 changes: 42 additions & 6 deletions test/runtime.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ var AllureRuntime = require('../runtime');
var Allure = require('../index');
mockery.disable();

var joc = jasmine.objectContaining.bind(jasmine);

describe('allure-runtime', function() {
var runtime, allure;
beforeEach(function() {
Expand All @@ -21,7 +23,7 @@ describe('allure-runtime', function() {
var stepFn = runtime.createStep('demo step [{0}]', stepSpy);
stepFn('param');
expect(allure.getCurrentSuite().currentTest.steps).toEqual([
jasmine.objectContaining({
joc({
name: 'demo step [param]',
status: 'passed',
stop: jasmine.any(Number)
Expand All @@ -36,7 +38,7 @@ describe('allure-runtime', function() {
var brokenStep = runtime.createStep('step 1', brokenSpy);
expect(brokenStep).toThrow();
expect(allure.getCurrentSuite().currentTest.steps).toEqual([
jasmine.objectContaining({
joc({
name: 'step 1',
status: 'broken',
stop: jasmine.any(Number)
Expand All @@ -50,7 +52,7 @@ describe('allure-runtime', function() {
});
attachmentFunction('test');
expect(allure.getCurrentSuite().currentTest.attachments).toEqual([
jasmine.objectContaining({
joc({
title: 'file [test]',
type: 'text/plain'
})
Expand All @@ -60,7 +62,7 @@ describe('allure-runtime', function() {
it('should create arbitrary attachements', function() {
runtime.createAttachment('note', 'I want to save it');
expect(allure.getCurrentSuite().currentTest.attachments).toEqual([
jasmine.objectContaining({
joc({
title: 'note'
})
]);
Expand All @@ -72,8 +74,8 @@ describe('allure-runtime', function() {
});
stepFn('test', 'test content');
expect(allure.getCurrentSuite().currentTest.attachments).toEqual([]);
expect(allure.getCurrentSuite().currentTest.steps).toEqual([jasmine.objectContaining({
attachments: [jasmine.objectContaining({title: 'test', type: 'text/plain'})]
expect(allure.getCurrentSuite().currentTest.steps).toEqual([joc({
attachments: [joc({title: 'test', type: 'text/plain'})]
})]);
});

Expand All @@ -85,4 +87,38 @@ describe('allure-runtime', function() {
{name: 'story', value: 'add from runtime'}
]);
});

it('should await promises and can create asynchronous step tree', function(done) {
var rootStep = runtime.createStep('root', function() {
return firstNested().then(function(result) {
expect(result).toBe('ok result');
return secondNested();
});
}),
firstNested = runtime.createStep('passed', function() {
return Promise.resolve('ok result');
}),
secondNested = runtime.createStep('broken', function() {
return Promise.reject('bad result');
});
rootStep().then(done.fail, function(result) {
expect(result).toBe('bad result');
expect(allure.getCurrentSuite().currentTest.steps).toEqual([
joc({
name: 'root',
steps: [
joc({
name: 'passed',
status: 'passed'
}),
joc({
name: 'broken',
status: 'broken'
})
]
})
]);
done();
});
});
});

0 comments on commit 8d91900

Please sign in to comment.