Mocha with Azure Mobile App: Getting Started

十年热恋 提交于 2020-01-24 20:05:26

问题


tl;dr;

Because of the close vote, I wanted to clarify what I'm asking. I want to know how to go from a functional, local Azure Mobile App with a Node.js backend to one that can be tested successfully with Mocha and Chai. The 3 issues listed below are all hurdles that I see that are keeping me from testing my App Service in this way. There seems to be something extra that is required that makes an Azure App Service different from a standard Node.js web application. I am trying to figure out what those differences are and how to work around them or fix them.


Intro

I have been working to get a copy of my Azure Mobile App with Node.js backend running locally.

Here are some of the resources I've been using:

  • 30 Days of Zumo.v2 (Azure Mobile Apps): Day 2
  • Testing Node.js with Mocha and Chai
  • Ensure Express App has started before running Mocha/Supertest tests

I have finally gotten that to work, but I am encountering issues with running Mocha tests against my code.

All is well when I run this:

node app.js

I get all of the expected outputs (specifically, the API routes added), and my browser can browse my site/API.

When I run mocha, however (or npm test if I have added that to my scripts property), nothing seems to work properly. My initial tests are failing, but they're failing because of:

  1. timeout issues, or
  2. API not created errors (404 errors)

Just for fun, I changed my test.js file to only run the following test:

describe('Addition', function () {
    it('should add two numbers correctly', function(done) {
        (2 + 4).should.equal(6);
        done();
    });
});

It worked exactly as expected, so the issue appears to be with loading up my custom API in my app.js file, or some other, Azure-related, non-Node.js-specific issue/error.


Issue 1:

As I believe is normal for an Azure Mobile App, my app.js file loads all of my API (.js/.json file pairs). Here are the pertinent lines:

var mobile = azureMobileApps({
    homePage: true
});
mobile.api.import('./api');
app.use(mobile);

When I run the mocha command in Terminal, I consistently get the following warning:

warn: Requested configuration path 
      (/{path to repo}/node_modules/mocha/bin/api) 
      does not exist

This seems to me to be trying to use the './api' reference from the app.js file and apply it to the mocha "whatever" (script? executable?) that is found here: /{path to repo}/node_modules/mocha/bin.

Now, I'm pretty sure I could apple a band-aid fix to this problem by changing this line:

mobile.api.import('./api');

to this:

mobile.api.import('../../../api');

So, when I do that, I no longer get the Requested configuration path does not exist error, but I also don't get the printout in the console telling me that the API methods were created. Trying with other depths (e.g., '../api' or '../../api') gives me the Requested configuration path does not exist error, so I think the '../../../api' path is the correct one, but it doesn't appear to do anything with that path now (when using mocha).


Issue 2:

I'm also not seeing the verbose: SQL Azure database detected - setting connection encryption line printed out. This seems to be telling me that my azureMobile.js file isn't being loaded when calling mocha as it appears to be when calling node app.js.

Is there something different that happens for an Azure Mobile App when calling node app.js that isn't happening when calling mocha? If so, what is it, and how can I get mocha to actually act like node?

One thought I had was that my app.js file has these declarations at the top:

var express = require('express'),
    azureMobileApps = require('azure-mobile-apps');

I'm obviously using azureMobileApps to create the mobile variable, which is what is supposed to .import() the API files. Is there another pathing issue with how mocha loads app.js for testing? express seems to get loaded OK, so I am doubting that's the issue, but I'm grasping for any and all straws at this point.


Issue 3

I am trying to make sure that my "site" has time to load everything up before attempting to run tests. To that end, I have attempted to install an EventEmitter that will wait for my API to load before beginning my tests.

This assumes that my mobile.api.import() call is asynchronous, which might be a faulty assumption. Also, that call might be failing not due to timing, but due to the azureMobile.js issue discussed above.

Here's the pertinent code:

app.js:

app.listen(port, function () {
    console.log('App has started');
    app.emit('appStarted');
});

module.exports = app;

test.js:

var app = require('../app');
before(function (done) {
    app.on('appStarted', function(){
        console.log('Got appStarted event');
        done();
    });
});

Although I see the 'App has started' message in the console, the app.on('Started'... code is never called.

Summary

So, in short, while I am able to facilitate local development, I have not yet been able to figure out how to use Mocha to test my local development. Any pointers or fixes for problems shown above are much appreciated!

来源:https://stackoverflow.com/questions/46815167/mocha-with-azure-mobile-app-getting-started

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!