Skip to content

Commit

Permalink
yield assert into the beforeEach/afterEach callbacks if they contain …
Browse files Browse the repository at this point in the history
…assertions, fixes #4
  • Loading branch information
kamal committed Mar 22, 2015
1 parent 28b5a67 commit bb0a321
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
63 changes: 55 additions & 8 deletions lib/formulas/qunit-transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ function isModule(node) {
node.expression.callee.name === 'module';
}

function isSetup(path) {
var node = path.node;
var parent = path.parent.node;

return parent.type === 'Property' &&
parent.key.name === 'setup' &&
node.type === 'FunctionExpression';
}

function isTeardown(path) {
var node = path.node;
var parent = path.parent.node;

return parent.type === 'Property' &&
parent.key.name === 'teardown' &&
node.type === 'FunctionExpression';
}

function isTest(node) {
return node.expression.type === 'CallExpression' &&
node.expression.callee.name === 'test';
Expand All @@ -42,29 +60,37 @@ function isImportFor(module, node) {
node.source.value === module;
}

function transformModule(node) {
function transformModule(node, withSetupAssert, withTeardownAssert) {
var callExpression = node.expression;
if (callExpression.arguments.length > 1 &&
callExpression.arguments[1].type === 'ObjectExpression') {
callExpression.arguments[1].properties.forEach(function(node) {
if (node.key.name === 'setup') {
node.key.name = 'beforeEach';
if (withSetupAssert) {
transformTestCallback(node.value);
}
}
if (node.key.name === 'teardown') {
node.key.name = 'afterEach';
if (withTeardownAssert) {
transformTestCallback(node.value);
}
}
});
}
}

function transformTestStatement(node) {
if (node.expression.arguments.length > 1) {
var callback = node.expression.arguments[1];
transformTestCallback(node.expression.arguments[1]);
}
}

if (callback.type === 'FunctionExpression') {
if (callback.params.length === 0) {
callback.params.push(builders.identifier('assert'));
}
function transformTestCallback(callback){
if (callback.type === 'FunctionExpression') {
if (callback.params.length === 0) {
callback.params.push(builders.identifier('assert'));
}
}
}
Expand Down Expand Up @@ -98,19 +124,23 @@ module.exports = function transform(source) {
var sections = {
addQUnitImport: true,
modules: [],
moduleCallbacks: [],
tests: [],
skips: [],
assertions: []
};

var ast = recast.parse(source);
var isInSetup = false;
var isInTeardown = false;

recast.visit(ast, {
visitExpressionStatement: function(path) {
var node = path.node;

if (isModule(node)) {
sections.modules.push(node);
sections.moduleCallbacks.push({});
}

if (isTest(node)) {
Expand All @@ -123,6 +153,14 @@ module.exports = function transform(source) {

if (isAssertion(node)) {
sections.assertions.push(node);

if (isInSetup) {
sections.moduleCallbacks[sections.moduleCallbacks.length - 1].withSetupAssert = true;
}

if (isInTeardown) {
sections.moduleCallbacks[sections.moduleCallbacks.length - 1].withTeardownAssert = true;
}
}
this.traverse(path);
},
Expand All @@ -137,11 +175,20 @@ module.exports = function transform(source) {
}

this.traverse(path);
},
visitFunctionExpression: function(path) {
isInSetup = isSetup(path);
isInTeardown = isTeardown(path);

this.traverse(path);

isInSetup = false;
isInTeardown = false;
}
});

sections.modules.forEach(function(qunitModule) {
transformModule(qunitModule);
sections.modules.forEach(function(qunitModule, index) {
transformModule(qunitModule, sections.moduleCallbacks[index].withSetupAssert, sections.moduleCallbacks[index].withTeardownAssert);
});

sections.tests.forEach(function(node) {
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/qunit-files/new-with-qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ var App;
var server;

module('Acceptance: FriendsNew', {
beforeEach: function() {
beforeEach: function(assert) {
App = startApp();
assert.ok(true, 'app started');
},
afterEach: function() {
Ember.run(App, 'destroy');
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/qunit-files/old-with-qunit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var server;
module('Acceptance: FriendsNew', {
setup: function() {
App = startApp();
ok(true, 'app started');
},
teardown: function() {
Ember.run(App, 'destroy');
Expand Down

0 comments on commit bb0a321

Please sign in to comment.