What is used for BDD and TDD with node.js?
I'm used to use Cucumber + RSpec. What's a good combo for node.js?
thanks
Update
Mocha gets my vote now!
You could have a look at the testing modules section from the node.js modules page. For example Vows is a pretty popular BDD framework.
Vows is a behavior driven development framework for Node.js.
Check out mocha - (github)
Also mocha-cakes, my attempt for Cucumber syntax on mocha.
If you are used to rspec, Jasmine is pretty nice. I've not used it on Node.js, but I have used it for testing a backbone app. It's syntax is very similar to rspec. Taken from the site above:
describe("Jasmine", function() {
it("makes testing JavaScript awesome!", function() {
expect(yourCode).toBeLotsBetter();
});
});
It's listed in the link provided by Alfred above, but since folks listed Vows as an example, I figured I'd give Jasmine a bump, especially since it's syntactically similar to rspec ;)
There is the 'Vows' project for BDD on Node http://vowsjs.org, looks pretty nice. It's a bit different from RSpec/Cucumber, but it's pretty fun
Maybe a little later, but what you're looking for is Kyuri: https://github.com/nodejitsu/kyuri
"kyuri is a node.js Cucumber implementation with a few extra asynchronous keywords. it supports 160+ languages and exports to VowsJS stubs"
Also, nodejitsu seems to have built a web app for managing a project Kyuri feature specs in a collaborative way, it's named "prenup", I would give it a look.
You could also try yadda. It plugs into other test libraries including mocha, jasmine, casper & webdriver, but also lets you write proper feature files instead of merely annotating you tests in situ. A typical test might look like...
var Yadda = require('yadda');
Yadda.plugins.mocha();
feature('./features/bottles.feature', function(feature) {
var library = require('./bottles-library');
var yadda = new Yadda.Yadda(library);
scenarios(feature.scenarios, function(scenario, done) {
yadda.yadda(scenario.steps, done);
});
});
And the feature file...
Feature: Mocha Asynchronous Example
Scenario: A bottle falls from the wall
Given 100 green bottles are standing on the wall
when 1 green bottle accidentally falls
then there are 99 green bottles standing on the wall
And output...
Mocha Asynchronous Example
✓ A bottle falls from the wall
1 passing (12ms)
Check out Buster.JS. Created by Christian Johansen, who literally wrote the book on javascript testing.
Buster supports both TDD and BDD. It does browser testing with browser automation (think JsTestDriver), QUnit style static HTML page testing, testing in headless browsers (PhantomJS, jsdom), and more.
Package a
(bdd and mocking)
https://npmjs.org/package/a
Very compact syntax, context separated from acts, chainable acts.
Easy Cmd line runner that searches recursively.
Unit tests: Mocha is great for unit tests.
BDD tests If you want a BDD test framework for Node.js then I'd recommend the Cucumber package.
I was going through the same concern last month .
For BDD :
Though Mocha itself provides BDD style by their describe and it statements.
For styles like cucumber , you can try :
mocha-cakes
mocha-gherkin
cucumber.js
kyuri
mocha-cucumber
They all have their own styles. I am sorry I can not provide working snippets now , let me know #Donald which one you select. Would like to know your insight.
I too was looking for a good Gherkin implementation, found mocha-cakes/mocha-cakes-2 which were nice but not very full featured. So I build my own with mocha as the base, which has parity with the gherkin language including Scenario Outlines. It also makes it easy to reference the data in your test. Its different to cucumber.js as its all inline not separate files. You can find the project here:
livedoc-mocha
Related
I'm creating my own tests using Cucumber-js and now I find myself with some step definition that I could reuse.
More specifically, I wanted to create a package with my common steps and then include the library into the different test suites.
I was playing around with
module.exports = function () {
this.Given(`I'm standard`, function(done) {
})
}
but when I use require() in the test suite it doesn't find the steps.
I was looking around but I couldn't find any documentation around this. Is this some bad practice? and If so, how I can avoid to repeat exactly the same code in different test suite packages?
Just create a _shared.spec.ts file within the folder of your tests and cucumber.js is gonna find directly the shared definition to reuse.
I've written a Node.js CLI and would like further development to proceed in a TDD style. I have an ideal workflow in mind and want to know if it is possible with existing frameworks.
When I write a new function, I'd like to document its preconditions in an assertion that will throw an error if e.g. input doesn't validate.
Postconditions should be specified in or near the function
The pre and postconditions should generate tests to be run with npm test
Assertions should only be checked in development mode
Documentation in each function should generate (html|md) documentation for the CLI
If I want to add tests other than precondition / postcondition / invariant tests it should be easy to do so
When mocking tests, there should be a way to specify "the universe before" and "the universe after". For instance to test a command that scaffolds a new project a-la express, I should be able to specify the initial directory structure as empty ({}) and the final directory structure as a JSON object representing the result ({ name: "project", path: "/project", type: "directory", children: { ... } }) <-- or something like that. This seems to require the ability to intercept writes to the file system.
I don't have a candidate library for automated test generation yet. I think a mix of Mocha, rewire and Contractual / Obligations might work for everything else, but I'm interested to hear about other approaches.
I'm a bit stuck with understanding how to write complicated Gruntfile.js and use it with tests. Am I using Grunt in a right way? I would like to ask community for help and contribute in an other way.
I'm writing a new task for Grunt and want to roll it out for wide audience on Github and npm. I want to make automated testing for this task (and I want to learn how to do it properly!).
I want to test different options combinations (about 15 by now). So, I should multiple times:
run cleanup
run my task with next options set
run tests and pass options object to the test
Some non-working code to look at for better understanding:
Gruntfile:
grunt.initConfig({
test_my_task: {
testBasic: {
options: {
//first set
}
},
testIgnore: {
options: {
//another set
}
},
//...
}
clean: {
tests: ['tmp'] // mmm... clean test directory
},
// mmm... unit tests.
nodeunit: {
tests: ['test/*.js'] //tests code is in 'tests/' dir
}
});
grunt.registerTask('test', ['test_my_task']);
I know how to check if tmp/ folder is in desired state when options object given.
The problem is putting things together.
I would ask just for template code as an answer, npo need to put working example.
PS: you can propose another testing tool, nodeunit is not a must.
PPS: crap, I could have written this in plain javascript by now! Maybe I'm doing wrong that I want to put Grunt into the unit tests? But I want to test how my task works in real environment with different options passed from Grunt...
You might want to have a look at the grunt-lintspaces configuration. The tests look like this, and it seems like a good way to do it. grunt-lintspaces uses nodeunit but a lot of plugins these days seem to.
If you don't want to test actual grunt output and instead functionality, you could use grunt-mocha-test - https://github.com/pghalliday/grunt-mocha-test which I am using for the grunt-available-tasks tests. I prefer the describe style of testing personally, it reads very well; the advantage of using this is that you actually test what your plugin does without including a ton of config in your Gruntfile; i.e. test code should be in the tests.
Grunt is well tested already so it doesn't make sense to test that its configuration works. Just test the functionality of your own plugin.
I'm trying to do a DRY cucumber feature and I'm facing a problem of converting a string into an ActiveRecord model name
Given /^the following "(.+)" exist:/ do |mod, table|
table.hashes.each do |t|
mod.create!(t)
end
assert mod.all.count == table.hashes.size
end
that gives
undefined method `create!' for "Balloon":String (NoMethodError)
More elegant solution might be to use a factory, but I'm wondering whether it is possible to use the above approach?
You could look into constantize which turns a String into a constant. Try:
"Balloon".constantize.create!(t)
BUT: Using your app code (models in particular) in a Cucumber step is code smell. Your integration tests shouldn't rely on the code under test at all—think of your app as a black box when you implement Cucumber steps. (Also think of a refactoring of your models that require you to go back and change your Cucumber steps—that's your first clue that you're on the wrong track!)
What you could do to improve this is create the models using an API (if your app implements one).
That way, you only rely on those parts of your app that are public-facing.
On another note: Your Given shouldn't have an assertion, it's more like a before hook in RSpec, setting up a condition for a later assertion...
I am trying to use authlogic's test helpers in Cucumber, calling activate_authlogic.
Our application_controller has a current_user_session method.
When we drop into the debugger mid-story, controller returns a Authlogic::TestCase::MockController.
But when we call controller.current_user_session.
The error occurred while evaluating nil.current_user_session.
How does this mock suddenly become a nil?
And does this mock controller know about our application controllers' code?
I don't know authlogic (and if this answer is helpful at all), but where does that mock object come from in the first place? You shouldn't be using any mocks in you cucumber stories. Cucumber is like an integration test, testing the complete Rails Stack.
I use it, to make sure, that my view, controller and model specs haven't diverged from each other.