2017-10-11 83 views
1

这是我的模块和测试我想要的功能:摩卡嘲讽进口变量

import aVariable from 'aModule' 

export function afunction() { 
    //do something with the imported aVariable 
    //calculates result 
    return result 
}); 

我想嘲笑aVariable我摩卡单元测试。

import {afunction} from 'aModule.js' 

describe('Tests',() => { 
    it('should return expected',() => { 
    expect(afunction()).to.equal(expected); 
    }); 

这可能吗?

UPDATE:

使用的巴别塔,联控,插件我.babelrc:

{ 
    "env": { 
    "dev": { 
     "presets": ["es2015"] 
    }, 
    "test": { 
     "plugins": ["rewire"] 
    } 
    } 
} 

当我运行我的测试:

meteor test --meteortesting:mocha 

我得到这个错误:

TypeError: _getServiceUrl(...).__Rewire__ is not a function 

当我使用:

BABEL_ENV=test meteor test --meteortesting:mocha 

我得到:

While processing files with ecmascript (for target web.browser): 
/node_modules/rewire/lib/rewire.js:19:15: Filename must be a string 
+0

你看过[重新连接](https://www.npmjs.com/package/babel-plugin-rewire)babel的插件?它可以让你轻松做到这样的事情。另外还有[另一个rewire library](https://www.npmjs.com/package/rewire),如果你不使用babel,你可以使用它。 – robbymurphy

+0

我已经看到rewire,但我并没有意识到你可以使用我和我的样机进口,并使用Babel作为一个转译器。 – Gobliins

回答

0

鉴于你已经安装了巴贝尔,插件,ReWire的节点模块,你可以做你的测试如下:

describe('Tests',() => { 
    it('should return expected',() => { 
    afunction.__Rewire__('aVariable', 'myStubVariableValue'); 
    expect(afunction()).to.equal(expected); 
    afunction.__ResetDependency__('aVariable'); 
}); 
+0

将尽快尝试这个 – Gobliins

+0

@Gobliins为您做了这项工作? – robbymurphy

+0

我试过了,但是在访问'x .__ Rewire__'时出现了未定义的错误。我需要一些进口吗? – Gobliins