I am trying to figure out how to create my own test environment to test the JavaScript classes I use in my fuse project.
I analyzed the environment offered by fuse but this environment does not seem to allow you to test asynchronous calls as Promise
:
https://www.fusetools.com/docs/testing/testing
So I thought I’d configure the test environment Mocha
, Chai
and Chai-promised
.
The difficulty I am having, however, is in being able to test methods that have dependencies with JavaScript methods offered by the fuse framework such as “Fetch”.
In fact, in the example that I leave the test fails because the method fetch
is not available when I invoke the tests with Mocha.
I would like to have suggestions and examples of test environments that you have integrated into your fuse projects.
Thank you
Giuseppe
Mainview.ux
<App>
<JavaScript>
var Post= require('Post');
var Observable = require("FuseJS/Observable");
var title = Observable();
Post.get(1).then(function(response){
response.value=response.title;
});
module.exports={
title:title
}
</JavaScript>
<Text>{title}</Text>
</App>
Post.js
function get(id) {
var status = 0;
var response_ok = false;
return fetch('http://jsonplaceholder.typicode.com/posts/' + id, {
method: 'GET'
}).then(function (response) {
status = response.status; // Get the HTTP status code
response_ok = response.ok; // Is response.status in the 200-range?
return response.json(); // This returns a promise
}).then(function (responseObject) {
console.dir(responseObject);
return responseObject;
}).catch(function (err) {
console.dir(err.message);
});
}
module.exports = {
get: get
}
Test for Module Post.js
var chai = require("chai");
var chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
var expect = chai.expect;
var should = chai.should()
var Post= require ('../Post');
describe("Test the behavior of Post", function () {
it('The post should have the Title property', function () {
expect(Post.get(1)).to.eventually.have.property("title");
});
})
Output failed mocha -> Test.js
mocha
Test the behavior of Post
call method
1) The post should have the Title property
0 passing (6ms)
1 failing
1) Test the behavior of Post
The post should have the Title property:
ReferenceError: fetch is not defined
at Object.get (Post.js:6:5)
at Context.<anonymous> (test/test.js:9:21)