Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用grunt在静态服务器上运行Qunit测试时出现问题_Javascript_Node.js_Gruntjs_Qunit_Grunt Contrib Connect - Fatal编程技术网

Javascript 使用grunt在静态服务器上运行Qunit测试时出现问题

Javascript 使用grunt在静态服务器上运行Qunit测试时出现问题,javascript,node.js,gruntjs,qunit,grunt-contrib-connect,Javascript,Node.js,Gruntjs,Qunit,Grunt Contrib Connect,通过web浏览器运行测试工作正常,但使用grunt会给我带来错误。我很难理解我做错了什么咕噜测试失败 $ grunt tests Running "jsonlint:sample" (jsonlint) task >> 4 files lint free. Running "jshint:all" (jshint) task >> 4 files lint free. Running "connect:server" (connect) task Started co

通过web浏览器运行测试工作正常,但使用grunt会给我带来错误。我很难理解我做错了什么<代码>咕噜测试失败

$ grunt tests
Running "jsonlint:sample" (jsonlint) task
>> 4 files lint free.

Running "jshint:all" (jshint) task
>> 4 files lint free.

Running "connect:server" (connect) task
Started connect web server on http://localhost:5000

Running "qunit:all" (qunit) task
Testing http://localhost:5000/tests/tests.html F
>> Settings.json tests - Fetching settings file
>> Message: InvalidStateError: DOM Exception 11: An attempt was made to use an object that is not, or is no longer, usable.
>> Actual: null
>> Expected: undefined
>> http://localhost:5000/tests/tests.js:7

>> Settings.json tests - Fetching settings file
>> Message: Check settings JSON file
>> http://localhost:5000/tests/tests.js:25:24
>> onreadystatechange@http://localhost:5000/tests/tests.js:8:25

>> Settings.json tests - Fetching settings file
>> Message: Assertion after the final `assert.async` was resolved
>> Actual: null
>> Expected: undefined
>> notEqual@http://localhost:5000/tests/qunit/qunit-1.22.0.js:1512:18
>> http://localhost:5000/tests/tests.js:25:24
>> onreadystatechange@http://localhost:5000/tests/tests.js:8:25

>> Settings.json tests - Fetching settings file
>> Message: Check settings JSON file
>> http://localhost:5000/tests/tests.js:25:24
>> onreadystatechange@http://localhost:5000/tests/tests.js:8:25

>> Settings.json tests - Fetching settings file
>> Message: Too many calls to the `assert.async` callback
>> Actual: null
>> Expected: undefined
>> http://localhost:5000/tests/tests.js:26:13
>> onreadystatechange@http://localhost:5000/tests/tests.js:8:25

>> Settings.json tests - Fetching settings file
>> Message: Assertion after the final `assert.async` was resolved
>> Actual: null
>> Expected: undefined
>> notEqual@http://localhost:5000/tests/qunit/qunit-1.22.0.js:1512:18
>> http://localhost:5000/tests/tests.js:25:24
>> onreadystatechange@http://localhost:5000/tests/tests.js:8:25

>> Settings.json tests - Fetching settings file
>> Message: Check settings JSON file
>> http://localhost:5000/tests/tests.js:25:24
>> onreadystatechange@http://localhost:5000/tests/tests.js:8:25

>> Settings.json tests - Fetching settings
>> Message: Too many calls to the `assert.async` callback
>> Actual: null
>> Expected: undefined
>> http://localhost:5000/tests/tests.js:26:13
>> onreadystatechange@http://localhost:5000/tests/tests.js:8:25

Warning: 8/8 assertions failed (54ms) Use --force to continue.

Aborted due to warnings.
Grunfile.js

module.exports = function(grunt) {
  grunt.initConfig({
    jsonlint: {
      sample: {
        src: ['json/*.json', 'api_files/*.json'],
        options: {
          formatter: 'prose'
        }
      }
    },
    jshint: {
      all: ['*.js', 'tests/*.js', 'server/*.js']
    },
    connect: {
      server: {
        options: {
          port: 5000,
          base: '.'
        }
      }
    },
    qunit: {
      all: {
        options: {
          urls: [
            'http://localhost:5000/tests/tests.html'
          ]
        }
      }
    }
  });

  grunt.loadNpmTasks('grunt-jsonlint');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-connect');
  grunt.loadNpmTasks('grunt-contrib-qunit');

  grunt.registerTask('tests', ['jsonlint', 'jshint', 'connect', 'qunit']);
};
tests.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Backend Tests</title>
    <link rel="stylesheet" href="qunit/qunit-1.22.0.css">
    <script src="qunit/qunit-1.22.0.js"></script>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="tests.js"></script>
</body>
</html>
main.js

function serveStaticFiles(router) {
    var app = express();
    app.use('/api', router);                // all of our routes will be prefixed with /api
    app.use('/', express.static(__dirname + '/')); // Makes it possible for html files to fetch images from img
    app.use(express.static('./html'));      // folder 'html' is now considered public and accessible
    app.use(express.static('./xml'));       // folder 'xml' is now considered public and accessible
    app.use(express.static('./img'));       // folder 'img' is now considered public and accessible
    app.use(express.static('./json'));      // folder 'json' is now considered public and accessible
    app.use(express.static('./tests'));     // folder 'tests' is now considered public and accessible

    return app;
}

var bugsnag = require("bugsnag");
bugsnag.register(process.env.BUGSNAG_API_KEY);
bugsnag.releaseStage = process.env.BUGSNAG_RELEASE_STAGE;
bugsnag.notifyReleaseStages = ["stage", "production"];

var express = require('express');
var router = express.Router();
var app = serveStaticFiles(router);
var port = process.env.PORT || 5000;
var server = app.listen(port);
var apiHandler = require("./server/ApiHandler");
console.log("Application created and it listens on port " + port);

apiHandler.initRestApi(router);

您需要运行节点服务器,作为grunt过程中的另一个步骤。具体来说,有一项艰巨的任务,我建议从那里开始。下面是grunt配置的外观:

grunt.initConfig({
    // ...
    express: {  // this is the new task...
      dev: {
        options: {
          script: 'path/to/main.js'
        }
      }
    },
    connect: {
      server: {
        options: {
          port: 3000,  // I changed this so it doesn't conflict with your express app
          base: '.'
        }
      }
    },
    qunit: {
      all: {
        options: {
          urls: [
            'http://localhost:3000/tests/tests.html'  // changed this as well
          ]
        }
      }
    }
});
然后,您将希望在“测试”运行时运行这三个任务。您可以这样创建别名:


grunt.registerTask('tests',['jsonlint','jshint','express','connect','qunit'])

如果状态代码不是200或404怎么办?在这种情况下,
回调
将不会被触发。我会确保您的
onreadystatechange
事件中有一个catchall:
否则如果(anhtprequest.readyState>=4){callback(null)}
@jakerella您当然是对的,谢谢。然而,这并不是我所得到的错误的原因。您了解错误及其原因吗?由于是404错误,我假设grunt contrib connect未按预期工作?或者节点应用程序从未启动。我想就是这样。服务器运行正常,但与运行heroku local不同。是的,我认为问题可能在于
http://localhost:5000/api/settings
不是实际的URL。。。服务器在哪里?因为它与grunt connect服务器的端口相同。。。应该是另一个端口/url/应用程序吗?啊,谢谢。我必须在expressdevoptions中将端口显式设置为5000,否则它将被分配为3000,这与测试中的rootUrl不匹配。我还必须将qunit url端口更改为与express(5000)相同的端口,否则它将超时。但是,在尝试获取数据时,我仍然收到404。它看起来清除了所有错误,除了
InvalidStateError:DOM异常11:试图使用不可用或不再可用的对象。
。朝着正确的方向迈出了一大步。谢谢。:)
grunt.initConfig({
    // ...
    express: {  // this is the new task...
      dev: {
        options: {
          script: 'path/to/main.js'
        }
      }
    },
    connect: {
      server: {
        options: {
          port: 3000,  // I changed this so it doesn't conflict with your express app
          base: '.'
        }
      }
    },
    qunit: {
      all: {
        options: {
          urls: [
            'http://localhost:3000/tests/tests.html'  // changed this as well
          ]
        }
      }
    }
});