2016-09-17 24 views
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) 
    ... 

爲什麼這個依賴不能被正確解析?

+0

@Gothdo:Package位於'/ Users/jbertoldi/dev/joel/xethya-dice/node_modules/xethya-random-mtw /'中。它在'dependencies'中列出,也是用ES6編寫的,它使用與這個相同的生成器('yo javascript')。 –

回答

2

看起來像xethya-random-mtw包中的問題。在這個包的package.json中,你有"main": "index.js",而index.js文件不存在,所以無法解析。 main應該包含一些真實路徑,例如"main": "dist/index.js"

+0

這一直是我的臉。 * facepalm *謝謝@atma! –

相關問題