繁体   English   中英

测试代码为es6编译

[英]testing code transpiled for es6

我准备用Qunit为一个为ES6编写的Backbone应用程序编写一些测试,并将babel.js应用于它,以便它可以在当代浏览器中运行。 为了确保我已正确设置qunit并正确指定所有路径,我首先测试了用ES5编写的Backbone模型,一切都按预期工作。 但是,然后我将bundle.js (包含应用了babel.js的ES6代码的结果)包含在我的tests/index.html ,并写了

 test ( "Code transformed by babel.js contained in bundle.js can be tested", function(){
    expect(1);
    var es6model = new ES6Model();
    equal( es6model.get("defaultproperty"), "defaultstring", "defaultproperty should be defaultstring");
 })

它告诉我ES6Model没有定义。

问题:有什么关于babeljs转换的代码会使使用Qunit进行测试更具挑战性吗?

除了babel在文件顶部写的所有复杂js之外, bundle.js的代码看起来像这样

var Model = Backbone.Model;
var View = Backbone.View;
var Collection = Backbone.Collection;
var Router = Backbone.Router;
var LocalStorage = Backbone.LocalStorage;

var ES6Model = (function (Model) {
    function ES6Model() {
        _classCallCheck(this, ES6Model);

        if (Model != null) {
            Model.apply(this, arguments);
        }
    }

    _inherits(ES6Model, Model);

    _prototypeProperties(Gopher, null, {
        defaults: {
            value: function defaults() {

                return {
                    defaultproperty: "defaultstring"

                };
            },
            writable: true,
            configurable: true
        }
    });

    return ES6Model;
})(Model);

更新

我将babel.js创建的所有代码包含在一个名为bundle.js文件中,并将其包含在我的index.html中,就像我将任何其他js文件一样,并且它运行没有问题,这就是为什么我认为我可以像任何一样测试它其他js代码。 但是,应该注意(正如评论者指出的那样)babel.js创建的代码包含在一个模块中。这就是bundle.js从我试图测试的模型开始的方式

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
"use strict";

更新

我正在使用browserify将babel应用于我的ES6代码的各种文件,这些文件创建了一个包。 为了运行测试,我做了npm run test并编译了bundle,我尝试了这两个(其中一个使用modules --ignore )但是它们都没有工作

“脚本”:{

    "test": "./node_modules/karma/bin/karma start --log-level debug",
    "build-js": "browserify app/app.js app/views.js app/models.js  app/d3charts.js -t babelify > app/bundle.js",
    "t-build": "browserify app/app.js app/views.js app/models.js app/d3charts.js -t [babelify --modules ignore] > app/test/test-bundle.js"
  },

(该应用程序是Backbone.js应用程序)。

这是我的业力配置文件。 我没有任何进一步的配置(所以我猜我包含karma-require是浪费但可能是必要的......)

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['qunit'],
    plugins: ['karma-qunit', 'karma-phantomjs-launcher', 'karma-requirejs'],

    files : [
      'app/test/jquery.js',     
      'app/test/d3.js',
      'app/test/json2.js',
      'app/test/underscore.js',
      'app/test/backbone.js',
      'app/backbone.localStorage.js',

      'app/test/test-bundle.js',
      'app/test/tests.js'

    ],


    reporters: ['progress'],

    // web server port
    port: 8080,

    // 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: false,

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

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

    // See http://stackoverflow.com/a/27873086/1517919
    customLaunchers: {
        Chrome_sandbox: {
            base: 'Chrome',
            flags: ['--no-sandbox']
        }
    }
  });
};

作为参考,他们使用traceur执行此操作的方法是将traceur-runtime.js文件编译到代码中(请参阅https://github.com/google/traceur-compiler/issues/777 - 未定义的类似变量错误)。

例如

traceur --out out/src/yourcode.js --script lib/traceur-runtime.js --script test/yourcode.js

(请参阅编译离线https://github.com/google/traceur-compiler/wiki/Compiling-Offline )。

执行前将Babel生成的模块导入测试(推荐)

您需要包含一个模块加载器(例如SystemJS )来处理导入。 Babel拥有出色的模块系统文档

它看起来像这样:

System.import( 'path/to/ES6Module' )
  .then( function( ES6Module ) {
    // … Run your tests on ES6Module here
  });

注意: System.import()返回一个Promise ,因此您的测试套件需要支持异步操作。


告诉Babel跳过模块生成(更简单)

您可以使用--modules ignore标志告诉Babel不要将代码包装在模块中。 这允许您的代码设置全局变量,可立即用于单元测试。 不建议使用全局变量(特别是在生产系统中),但它们更易于应用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM