未捕获的ReferenceError:未定义必需

时间:2017-12-26 14:25:06

标签: javascript node.js gulp jasmine karma-jasmine

我是jasmine-karma测试的新手,并且遇到了一些配置问题。我已经完成了一些教程,到目前为止我已经创建了package.json,karma.conf.js和gulpfile.js。

当我尝试运行测试时,它会打开chrome并且karma开始运行。但它给出了如下错误;

Uncaught ReferenceError: require is not defined

这是我的gulpfile.js文件

'use strict'
var gulp = require('gulp');
var Server = require('karma').Server;


//Run test once and exit
gulp.task('test', function (done){
    new Server(
        {
            configFile: __dirname + '/karma.conf.js',
            singleRun: false
        }, done).start();
});

// Watch for file changes and re-run tests on each change
gulp.task('tdd', function (done) {
    new Server({
        configFile: __dirname + '/karma.conf.js'
    }, done).start();
});

gulp.task('default', ['tdd']);

这是我的karma.conf.js文件。

// Karma configuration
// Generated on Fri Dec 22 2017 16:57:52 GMT+0530 (Sri Lanka Standard Time)

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine'],


    // list of files / patterns to load in the browser
    files: [
        '*.js'
    ],


    // list of files / patterns to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity
  })
}

这是packages.json

{
  "name": "package",
  "version": "1.0.0",
  "description": "THis version of the webdriver works with Windows 10 post fall 2005 update",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "gulp": "^3.9.1",
    "jasmine-core": "^2.8.0",
    "karma": "2.0.0",
    "karma-chrome-launcher": "^2.2.0",
    "karma-jasmine": "^1.1.1"
  }
}

我面临的另一个问题是,这些测试将首先在本地运行,然后这些测试将签入服务器。我如何在服务器端测试这些茉莉花测试?我需要任何附加设置吗?

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试对服务器端代码(例如您的gulpfile)运行客户端测试。

Node.js在V8引擎上运行,该引擎也被谷歌使用,但是具有包含不同属性的不同全局对象。

在浏览器中,标准全局对象是“窗口”,例如包含“文档”属性。因此,documentwindow.documentthis.document都是相同的。

如果您尝试在nodejs环境中使用'document',则会遇到相同的类型错误。

Buttom line - 谷歌浏览器不知道“需要”是什么,因为它不是全局“窗口”对象中的属性。