Can before[Each] and after[Each] be async when using the BDD interface? - intern

Am I missing something or is it not possible for before[Each] and after[Each] to be async when using the BDD interface? I can get async test to run fine just not the setup parts.

Yes; return a promise from any of the setup methods in order to make them asynchronous.

Related

How to create nested nodejs async hooks

Is there a recommended library or a good way to create nested nodejs async hooks?
I found "cls-hooked" library but it looks unmaintained and seems to have memory leaks.
basically what i want to have is the ability to wrap code (async or sync) with context, mainly for logs, but for other stuff too.

Unit testing for loopback model

I have a Loopback API with a model Student.
How do I write unit tests for the node API methods of the Student model without calling the REST API? I can't find any documentation or examples for testing the model through node API itself.
Can anyone please help?
Example with testing the count method
// With this test file located in ./test/thistest.js
var app = require('../server');
describe('Student node api', function(){
it('counts initially 0 student', function(cb){
app.models.Student.count({}, function(err, count){
assert.deepEqual(count, 0);
});
});
});
This way you can test the node API, without calling the REST API.
However, for built-in methods, this stuff is already tested by strongloop so should pretty useless to test the node API. But for remote (=custom) methods it can still be interesting.
EDIT:
The reason why this way of doing things is not explicited is because ultimately, you will need to test your complete REST API to ensure that not only the node API works as expected, but also that ACLs are properly configured, return codes, etc. So in the end, you end up writing 2 different tests for the same thing, which is a waste of time. (Unless you like to write tests :)

Adding a default before() function for all the test cases (Mocha)

I'm writing functions for my node.js server using TDD(Mocha). For connecting to the database I'm doing
before(function(done){
db.connect(function(){
done();
});
});
and I'm running the test cases using make test and have configured my makefile to run all the js files in that particular folder using mocha *.js
But for each js file I'll have to make a separate connection to the database, otherwise my test cases fail since they do not share common scope with other test files.
So the question is, Is there anything like beforeAll() that would just simply connect once to the database and then run all the test cases? Any help/suggestion appreciated.
You can setup your db connection as a module that each of the Mocha test modules imports.
var db = require('./db');
A good database interface will queue commands you send to it before it has finished connecting. You can use that to your advantage here.
In your before call, simply do something that amounts to a no op. In SQL that would be something simple like a raw query of SELECT 1. You don't care about the result. The return of the query just signifies that the database is ready.
Since each Mocha module uses the same database module, it'll only connect once.
Use this in each of your test modules:
before(function(done) {
db.no_op(done);
});
Then define db.no_op to be a function that performs the no op and takes a callback function.

Test environment in Node.js / Express application

I've just starting working with Node, and I've been following along with various tutorials.
I've created an Express app, and setup Mongoose and Jasmine.
How can I configure my specs so that I can:
create models, automatically clean them up after each spec
use a different database for creating test objects (say myapp_test)
do this in a way that is as DRY as possible, i.e. not creating a before / after block with the teardown for each describe block
?
I'll try to answer you.
Create models, automatically clean them up after each spec.
To do that I'll assume you use Mocha as the testing framework you can simply use the function beforeEach like this :
describe('POST /api/users', function() {
beforeEach(function(done) {
User.remove({}, function (err) {
if (err) throw err;
done();
});
});
});
Basicly what I'm doing here is cleanning up my database before each it but you can make it do anything you want.
Use a different database for creating test objects
Here, you should use the node process.env method to setting your env. Here is a article to understand a little how it works. Take a lot to GRUNT projects, it helps a lot with your workflow and the configurations stuff.
do this in a way that is as DRY as possible, i.e. not creating a
before / after block with the teardown for each describe block
I'm not sure I got what you want but take a look at the doc for the hooks before, after, beforeEach, afterEach. I think you will find what you want here.

Is there a capybara for Node.js?

Does anyone know whether there is anything similar to capybara for Node.js?
How about Zombie?
Zombie.js
Insanely fast, headless full-stack testing using Node.js
The Bite
If you're going to write an insanely fast, headless browser, how can you not call it Zombie Zombie it is.
Zombie.js is a lightweight framework for testing client-side JavaScript code in a simulated environment. No browser required.
I'm a rails-turned-node developer and I've always been looking for a analogy in the JS world for the winning RSpec/Capybara combination. Here is what I've found what I like best.
Mocha - for running asynchronous tests
Chai - for assertions
Request - for handling HTTP request/response
Cheerio - for selecting HTML elements from responses
Here is a more involved post on how to set up this stack including continuous test running.
Happy Testing!
Cucumber-JS is the closest you will get in Javascript:
https://github.com/cucumber/cucumber-js
You can use the library to drive JS, headless and Selenium, however it is missing the 'capybara' shared API between each of the different 'worlds'. There is a good talk at http://skillsmatter.com/podcast/agile-testing/cucumber-js-cuke-up-your-javascript and supporting github code at https://github.com/jbpros/cukecipes
I'm hoping that when the phantomjs webdriver wire protocol is finished (https://github.com/detro/ghostdriver) and if Soda/Selenium (https://github.com/LearnBoost/soda) is able to drive it then the headless tests will be able to be automated via Selenium thus bypassing the need for Capybara.
See jelly.io:
Jellyfish is a Node project that aims to make it easy to launch different JavaScript environments and run your code.
Hy!
I've created a library to help out with cucumber-js. It gives you callable steps and parsed arguments. This works with cucumber and doesn't replace it.
Quick features:
Sync step definitions, no more callbacks;
Call other step from step definitions;
Parse values such as arrays, objects and decimals;
https://github.com/hackhat/cucumberry
Hope you find it useful (:
We created a complete acceptance testing solution in JS
http://xolv.io/products/chimp
It uses CucumberJS (Mocha/Jasmine soon) to drive your acceptance tests
Downloads and starts Phantom / Selenium
Injects a preconfigured Webdriver.IO instance into the testing context
Includes the request npm library (for REST)
the assertion library of your choice (Chai / Jasmine-expect)
Uses fibers for synchronous testing (no callback hell / promise confusion)
Works with SauceLabs / BrowserStacks etc
Support outside in testing with a watch mode that continuously runs the scenarios you tag with #focus
Supports CI out of the box and tested on Circle/Codeship/Travis (using headless Chrome/Firefox)
It doesn't make you coffee, yet
In terms of acceptance testing, I have heavily used Capybara for Rails. And I am unhappy with the alternatives listed here for NodeJS. You will want a popular web automation utility combined with the ability to make assertions against scenarios of a particular feature.
When you think of web automation in Node, Phantom and, thus, Casper is dead. So what is in the rise as of now? Puppeteer. I started using Puppeteer a few years ago when it was in beta and few articles and SO posts about it. But now Puppeteer is becoming the leader of web automation in Node. However, you cannot assert things in Puppeteer which is what you would expect from a testing framework.
But that doesn't stop us from integrating a testing tool into Puppeteer web automation. I found a few solutions using jest as the testing tool.
const puppeteer = require('puppeteer');
describe('Open Website', () => {
var browser, page;
var url = 'https://website.io'
beforeEach (async () => {
browser = await puppeteer.launch({ headless: false });
page = await browser.newPage();
})
afterEach (() => {
browser.close()
})
test('Title == Website Tools', async () => {
await page.goto(url);
const title = await page.title();
expect(title).toBe("Website Tools");
});
Yes, see expresso and more here - Unit testing framework for node.js that specifically supports testing async code?

Resources