我正试图在meteor项目中使用anyNpmPackage
进行测试。
我已完成meteor npm install anyNpmPackage
在我的package.js中我有
Package.onTest(function(api) {
api.use([
'anyNpmPackage'
]);
api.addFiles('tests/server/methods.js', 'server');
});
现在anyNpmPackage
内部使用anyOtherNpmPackage
anyOtherNpmPackage
的行是
myModule = fs.readFileSync(require.resolve("../include/module.js"));
当我进行流星测试时
我收到错误Error: Cannot find module '../include/module.js'
当我在该行上方进行调试时
console.log(fs.readdirSync('../../'))
结果为[ 'server', 'web.browser' ]
,console.log(__dirname)
产生/node_modules/jsonpath/lib
其中jsonpath
为anyOtherNpmPackage
此问题的解决方法是什么?
答案 0 :(得分:0)
当Meteor套餐依赖于 npm 套餐时,您应该使用Npm.depends。
修改强> 我在Meteor应用程序中创建了以下包,它可以工作:
package.js
Package.describe({
name: 'npmtest',
version: '0.0.1',
// Brief, one-line summary of the package.
summary: '',
// URL to the Git repository containing the source code for this package.
git: '',
// By default, Meteor will default to using README.md for documentation.
// To avoid submitting documentation, set this field to null.
documentation: 'README.md'
});
Package.onUse(function(api) {
api.versionsFrom('1.5.1');
api.use('ecmascript');
api.mainModule('npmtest.js');
});
Package.onTest(function(api) {
api.use('ecmascript');
api.use('tinytest');
api.use('npmtest');
api.mainModule('npmtest-tests.js');
});
Npm.depends({
'jsonpath':'1.0.0'
})
npmtest-tests.js
// Import Tinytest from the tinytest Meteor package.
import { Tinytest } from "meteor/tinytest";
// Import and rename a variable exported by npmtest.js.
import { name as packageName } from "meteor/npmtest";
const jp = Npm.require("jsonpath");
// Write your tests here!
// Here is an example.
Tinytest.add('npmtest - example', function (test) {
console.log("here")
const cities = [
{ name: "London", "population": 8615246 },
{ name: "Berlin", "population": 3517424 },
{ name: "Madrid", "population": 3165235 },
{ name: "Rome", "population": 2870528 }
];
const names = jp.query(cities, '$..name');
test.equal(names[0], "London");
});
<强> EDIT2:强>
注意:我从未使用npm install