JavaScript test (mocha) with 'import' js file

前端 未结 2 1263
深忆病人
深忆病人 2020-12-30 05:49

I understand module.export and require mannner:

Requiring external js file for mocha testing

Although it\'s pretty usable as long a

2条回答
  •  星月不相逢
    2020-12-30 06:39

    I usually include a _test object containing references to all my "private" internal variables and functions and expose it on exports. In your case:

    ./app.js

    var INFINITY = 'infinity';
    
    function foo() {
        return 'bar';
    }
    
    exports._test = {
        INFINITY: INFINITY,
        foo: foo
    }
    

    ./test/app-test.js

    var app = require('../app.js')
    /* ... */
    it('should equal bar', function() {
        expect(app._test.foo()).to.equal('bar');
    });
    it('should equal infinity', function() {
        expect(app._test.INFINITY).to.equal('infinity');
    });
    

提交回复
热议问题