fluentforward / data-driven Goto Github PK
View Code? Open in Web Editor NEWData driven testing for mocha.js
License: MIT License
Data driven testing for mocha.js
License: MIT License
Async tests:
it('should ...', function (done) {
});
Data-driven tests:
it('should ...', function (ctx) {
});
What do you need to do to combine both?
A question, really.
We've started using promises for a lot of our tests in order to get a cleaner, more readable structure.
We're using mocha-as-promised (previously just used co), which gives us tests which look like this:
it('some stuff', function * () {
const value = yield someService.somePromise()
expect(value).to.equal(5)
})
I can't seem to get data-driven
to wrap this, as much as I'd like to. How difficult would it be (or is it already possible) to use data-driven along with generator functions?
Happy to do a PR if it's something that you think could be accomplished.
When using data-driven, the mocha functions like it.only() and it.skip() don't work.
it.skip("Add an offer", function(data, done){
^
TypeError: Object function (title, f) {
for (var key in testData) {
title = title.replace('{'+key+'}',testData[key])
}
var testFn = f.length < 2 ?
function() {
f(testData)
} :
function(done) {
f(testData,done)
}
mochaIt(title, testFn)
} has no method 'skip'
Mocha allows returning promises for async data handling.
Are there any plans to add support for this?
See the following for a test case where the test will fail as expected using mocha, but will pass using data driven.
var data_driven = require('data-driven')
var makeRejectPromise = function() {
return new Promise(function (fulfill, reject){
reject();
});
};
describe('Array', function() {
describe('#indexOf()', function(){
data_driven([{value: 0},{value: 5},{value: -2}], function() {
it('should return -1 when the value is not present when searching for {value}', function(ctx){
return makeRejectPromise();
})
})
})
})
describe('Array', function() {
describe('#indexOf()', function () {
it('should return -1 when the value is not present', function () {
return makeRejectPromise();
});
});
});
See https://github.com/mochajs/mocha/blob/2.3.0/mocha.js#L4225 for where the mocha code looks at the result to evaluate the promise.
If I want to build data dynamically, such as a series of db insertions where i later grab the database id, or the result of a promise, it isn't possible with this library.
It seems that because data has to be defined outside the 'it' clause, it is solidified before any before methods can modify the data.
Data-driven is very useful for testing a bunch of data, but if the tests can be running in parallel will save a lot of time. Is there a way to do that?
First, thanks for writing this module. I would hope to use it if this issue can be resolved.
It looks like, if you need to use "before" to set up your test data, the data_driven method executes before "before" such that the test data is empty for the wrapped tests. I'm not an expert on how mocha executes so I'm not sure if this is something you can address or not.
Please let me know. If I get time I will try and address it and submit a pull request.
There is an open PR for this. Would be excellent to have this. Please ship. Thanks!!!
Hello,
In the source, the dataDriven function is exported using the CommonJS way.
But I need to use either requireJS or a script tag. As Mocha support browser environments, what do you think of that enhancement?
Florent
The keywords within the describe() string are not replaced with the test data {width} and {height}
data_driven(viewportSizes, function () {
describe('Browser Viewport for width {width} and height {height}', function () {
before(function (ctx) {
browser.setViewportSize({width: ctx.width, height: ctx.height});
});
Hi,
Thank you for this cool lib!
One question. I need to get my test data array in a async before function. How can I do it with this library?
var data_driven = require('data-driven')
var assert = require('assert');
describe('Array', function() {
describe('#indexOf()', function(){
this.devices = [];
var self = this;
before(function(done) {
getAllConnectedDevices(function(devices) {
self.devices = devices;
done();
});
})
data_driven(self.devices, function() {
it('should return -1 when the value is not present when searching for {os}', function(ctx, done){
console.log('asasd');
assert.equal(-1, [1,2,3].indexOf(ctx.value));
done();
})
})
})
})
self.devices is always empty like it is above.
Thanks!
Regards,
Riaan
tried to install
sudo yarn add data-driven
yarn add v0.23.4
[1/4] Resolving packages...
[2/4] Fetching packages...
error [email protected]: The engine "node" is incompatible with this module. Expected version ">= 6.9.0".
error Found incompatible module
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.
checked my node.js version
node --version
v7.8.0
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.