0
鉴于这种ES6班,在那里我有一个依赖于一个公开注册的包叫做xethya-random-mtw
:摩卡找不到一个模块的依赖性正在测试
// /src/Dice.js
import MersenneTwister from 'xethya-random-mtw';
class Dice {
constructor(faces) {
if (typeof faces !== 'number') {
throw new Error('Dice#constructor: expected `faces` to be a Number.');
}
if (faces < 2) {
throw new Error('Dice#constructor: A dice must have at least two faces.');
}
this.faces = faces;
}
roll() {
const mt = new MersenneTwister();
return Math.ceil(mt.generateRandom() * this.faces);
}
}
export default Dice;
我想运行与摩卡以下测试:
// /test/DiceSpec.js
import { expect } from 'chai';
import Dice from '../src/Dice';
describe('Dice',() => {
describe('#constructor',() => {
it('should instantiate a Dice with the expected input',() => {
const dice = new Dice(2);
expect(dice.faces).to.equal(2);
});
it('should fail if a single-faced Dice is attempted',() => {
expect(() => new Dice(1)).to.throw(/at least two faces/);
});
it('should fail if `faces` is not a number',() => {
expect(() => new Dice('f')).to.throw(/to be a Number/);
});
});
describe('#roll',() => {
it('should roll numbers between 1 and a given number of faces',() => {
const range = new Range(1, 100 * (Math.floor(Math.random()) + 1));
const dice = new Dice(range.upperBound);
for (let _ = 0; _ < 1000; _ += 1) {
const roll = dice.roll();
expect(range.includes(roll)).to.be.true;
}
});
});
});
当我运行npm run test
,这是mocha --compilers js:babel-register
,我得到以下错误:
$ npm run test
> [email protected] test /Users/jbertoldi/dev/joel/xethya-dice
> mocha --compilers js:babel-register
module.js:442
throw err;
^
Error: Cannot find module 'xethya-random-mtw'
at Function.Module._resolveFilename (module.js:440:15)
at Function.Module._load (module.js:388:25)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
at Object. (/Users/jbertoldi/dev/joel/xethya-dice/src/Dice.js:9:1)
...
为什么这个依赖不能被正确解析?
@Gothdo:Package位于'/ Users/jbertoldi/dev/joel/xethya-dice/node_modules/xethya-random-mtw /'中。它在'dependencies'中列出,也是用ES6编写的,它使用与这个相同的生成器('yo javascript')。 –