首页 文章

从Node使用Jasmine的正确方法是什么?

提问于
浏览
9

经过多次黑客攻击后,我设法通过Node运行简单的Jasmine测试 .

但是,有一些奇怪的东西我不明白......茉莉花文件导出函数似乎需要引用自己传回工作(这适用于Jasmine和ConsoleReporter) .

我确定这不是正确的方法(尽管我很高兴我终于做了一些测试运行:)),那么有人可以解释更好的方法来做到这一点吗?

(注意:我不想引入更多第三方代码,我不理解像node-jasmine;我想了解我现在拥有的东西;不要添加更多!)

// Include stuff
jasmine = require('../../../Apps/Jasmine/jasmine-standalone-2.0.0/lib/jasmine-2.0.0/jasmine.js');
jasmineConsole = require('../../../Apps/Jasmine/jasmine-standalone-2.0.0/lib/jasmine-2.0.0/console.js')


// WHAT THE? I DON'T EVEN KNOW WHAT THIS MEANS
jasmine = jasmine.core(jasmine);
jasmineConsole.console(jasmineConsole, jasmine)


// Set up the console logger
jasmine.getEnv().addReporter(new jasmine.ConsoleReporter({ print: console.log }));


// Create some global functions to avoid putting jasmine.getEnv() everywhere
describe = jasmine.getEnv().describe;
it = jasmine.getEnv().it;
expect = jasmine.getEnv().expect;


// Dummy tests

describe("A suite", function() {
    it("contains spec with an expectation", function() {
        expect(true).toBe(true);
    });
    it("contains spec with a failing expectation", function() {
        expect(true).toBe(false);
    });
});


// Kick off execution

jasmine.getEnv().execute();

Jasmine "working"

Edit: 在发货 bootstrap.js 中注意到这一点,这基本上是相同的(除了不同的命名)...所以也许这是正常的?!

It's not just me doing bonkers stuff!

2 回答

  • 3

    Pivatol最近在2.0版中添加了better node.js support到Jasmine,并计划发布官方NPM包 . 现在,您可以通过遵循自己的node test suite中使用的实现从节点使用它 .

    以下是您编写的代码中正在发生的事情的简要说明:

    jasmine = jasmine.core(jasmine); 当您最初需要('jasmine')时,您将返回单个函数getJasmineRequiredObj();通过调用jasmine.core(jasmine),您告诉jasmine使用node exports 语句返回它的行为方法,而不是将它们添加到window.jasmineRequire对象上 .

    https://github.com/pivotal/jasmine/blob/master/src/core/requireCore.js

    function getJasmineRequireObj() {
      if (typeof module !== 'undefined' && module.exports) {
        return exports;  
      } else {
        window.jasmineRequire = window.jasmineRequire || {};
        return window.jasmineRequire;
      }
    }
    
    // jRequire is window.jasmineRequire in a browser or exports in node.
    getJasmineRequireObj().core = function(jRequire) {
      var j$ = {};
    
      jRequire.base(j$);
      j$.util = jRequire.util();
      j$.Any = jRequire.Any();
      ...
      return j$; // here is our jasmine object with all the functions we want.
    };
    

    jasmineConsole.console(jasmineConsole, jasmine) Jasmine将其初始化为's core functions separately from it'的记者 . 这个语句与jasmine.core(jasmine)基本相同,仅适用于控制台记者 .

    https://github.com/pivotal/jasmine/blob/master/src/console/requireConsole.js

  • 6

    还有jasmine-node(仍然使用jasmine 1.3并且测试版带有jasmine 2.0 - 2015年2月)和jasmine-npm(来自茉莉维护者本身,最新版本) .

    它们都很容易从命令行使用,不需要代码(当然除了测试!) .

相关问题