用Mocha测试NodeJS—“不需要定义”

时间:2021-12-05 20:27:25

I've been having trouble setting up a test framework for a NodeJS + Backbone app with the constant "require is not defined" error. I finally got it working using an in-browser test framework which picks up all of the dependencies I need and running a test.js file.

我在为NodeJS +主干应用程序设置测试框架时遇到了麻烦,该应用程序的常量“require is not defined”错误。我最终使用了一个浏览器内测试框架使它能够工作,该框架获取我需要的所有依赖项并运行一个测试。js文件。

Currently, I'm only doing basic testing of my Backbone models, views, and collections. Now, I want to add in API testing but I'm back to the same "require is not defined" error. What is causing this? It's clear that I'm missing something fundamental here. I just want to add:

目前,我只对我的主干模型、视图和集合进行基本的测试。现在,我想添加API测试,但又回到了相同的“require is not defined”错误。是什么原因导致了晒伤的?很明显,我漏掉了一些基本的东西。我想补充一点

var request = require('supertest')
  , express = require('express');

var app = express();

Snippet of test.js:

. js代码片段:

describe('Application', function(){
  it("creates a global variable for the namespace", function() {
    should.exist(App);
  })
});


describe('Models', function() {

    describe('SearchFormModel', function() {
        beforeEach(function() {
            this.SearchFormModel = new App.Model.SearchFormModel();
            this.defaultFields = this.SearchFormModel.attributes;
        })

        it("created a SearchFormModel", function() {
            should.exist(this.SearchFormModel);
        })

        it("should have 7 default fields", function() {
            Object.keys(this.SearchFormModel).length.should.equal(7);
        })

        it("should default all fields to empty string", function() {
            for (var key in this.defaultFields) {
                this.defaultFields[key].should.equal("");
            }
        })
    });

});

test-runner.html:

test-runner.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <!-- Title &amp; Meta -->
    <title>Frontend tests</title>
    <meta charset="utf-8">

    <!-- Stylesheets -->
    <link rel="stylesheet" href="../node_modules/mocha/mocha.css">
</head>

<body>

    <div id="mocha"></div>

    <!-- Testing Libraries -->
    <script src="../node_modules/mocha/mocha.js"></script>
    <script src="../node_modules/chai/chai.js"></script>
    <script>
        // Use the expect version of chai assertions - http://chaijs.com/api/bdd
        var should = chai.should();

        // Tell mocha we want TDD syntax
        mocha.setup('tdd');
    </script>

    <!-- Libs -->
    <script src="../public/lib/jquery-1.8.2.min.js"></script>
    <script src="../public/lib/underscore-min.js"></script>
    <script src="../public/lib/backbone-min.js"></script>
    <script src="../public/lib/bootstrap.min.js"></script>
    <script src="../public/lib/highcharts.js"></script>
    <script src="../public/lib/bootstrap-datepicker.js"></script>
    <script src="../public/js/modules/exporting.js"></script>

    <!-- Source files -->
    <script src="../public/js/namespace.js"></script>
    <script src="../public/js/jst.js"></script>            
    <script src="../public/js/utils.js"></script>
    <script src="../public/js/models/models.js"></script>
    <script src="../public/js/models/search.js"></script>
    <script src="../public/js/models/plot.js"></script>
    <script src="../public/js/models/search_result.js"></script>
    <script src="../public/js/views/header.js"></script>
    <script src="../public/js/views/plot.js"></script>
    <script src="../public/js/views/list.js"></script>
    <script src="../public/js/views/search.js"></script>
    <script src="../public/js/router.js"></script>   
    <script src="../public/js/app.js"></script>    

    <!-- Test -->
    <script src="test.js"></script>

    <script>
        mocha.run();
    </script>

</body>
</html>

1 个解决方案

#1


3  

require and commonjs only works in Node.js

require和commonjs只在Node.js中工作

If you run Browser test, then you need to code it like you'll run it in the browser. Also note that Unit Test should be done in isolation, you shouldn't need to load you app server (express) to run your test.

如果运行浏览器测试,那么需要像在浏览器中运行一样对其进行编码。还要注意,单元测试应该独立完成,不需要加载应用程序服务器(express)来运行测试。

I'd like to point you to an easy solution from there, but there's just too many choices. Very basically, you should start running browser test in the browser by loading an html file.

我想指出一个简单的解决方案,但是有太多的选择。基本上,您应该通过加载html文件开始在浏览器中运行浏览器测试。

Then, you'll want to automatize this and run browser test from the terminal. That's when you want to run test in PhantomJs and the likes and output browser results on the terminal. Around this, you can checkout Karma and Testem who're two browser test runner (remember here Mocha alone won't run browser test via command line).

然后,您需要自动化这个,并从终端运行浏览器测试。这时,您需要在PhantomJs中运行测试,并在终端上运行like和output浏览器结果。在这个过程中,您可以检查两个浏览器测试运行器的Karma和Testem(请记住,仅Mocha就无法通过命令行运行浏览器测试)。

As you're using Backbone, you might be interested in the Backbone-Boilerplate Karma + Grunt test setup as a starting point. See more on this here: https://github.com/backbone-boilerplate/backbone-boilerplate

当您使用主干时,您可能会对Backbone- boilerplate Karma + Grunt测试设置感兴趣。在这里可以看到更多:https://github.com/backbone-boilerplate/backbone-boilerplate。

#1


3  

require and commonjs only works in Node.js

require和commonjs只在Node.js中工作

If you run Browser test, then you need to code it like you'll run it in the browser. Also note that Unit Test should be done in isolation, you shouldn't need to load you app server (express) to run your test.

如果运行浏览器测试,那么需要像在浏览器中运行一样对其进行编码。还要注意,单元测试应该独立完成,不需要加载应用程序服务器(express)来运行测试。

I'd like to point you to an easy solution from there, but there's just too many choices. Very basically, you should start running browser test in the browser by loading an html file.

我想指出一个简单的解决方案,但是有太多的选择。基本上,您应该通过加载html文件开始在浏览器中运行浏览器测试。

Then, you'll want to automatize this and run browser test from the terminal. That's when you want to run test in PhantomJs and the likes and output browser results on the terminal. Around this, you can checkout Karma and Testem who're two browser test runner (remember here Mocha alone won't run browser test via command line).

然后,您需要自动化这个,并从终端运行浏览器测试。这时,您需要在PhantomJs中运行测试,并在终端上运行like和output浏览器结果。在这个过程中,您可以检查两个浏览器测试运行器的Karma和Testem(请记住,仅Mocha就无法通过命令行运行浏览器测试)。

As you're using Backbone, you might be interested in the Backbone-Boilerplate Karma + Grunt test setup as a starting point. See more on this here: https://github.com/backbone-boilerplate/backbone-boilerplate

当您使用主干时,您可能会对Backbone- boilerplate Karma + Grunt测试设置感兴趣。在这里可以看到更多:https://github.com/backbone-boilerplate/backbone-boilerplate。