我的代码完全独立于系统,很少有依赖于系统的代码行在node.js或浏览器上运行。我已经成功地设置了基于mocha的测试,以便它能够在这两个环境中工作,使用相同的测试和相同的代码模块。我还管理浏览器测试环境,将调用自动交换给那些很少依赖于系统的包,这些包总是为浏览器版本调用node.js版本。
所以一切都在运转,包括摩卡和西农以及所有的测试。
直到我决定使用SystemJS,不仅用于运行测试,而且也用于代码本身。
node.js部分仍然工作,包括测试。当我运行浏览器测试时,问题就出现了。
对于那些人,我使用一个运行测试的index.html文件,下面是它的全部。
请注意,这与以后的任何生产浏览器环境无关,在这个早期阶段,我只想测试我已经针对浏览器使用的代码(纯粹是基于网络和存储的代码,而不是任何基于GUI的东西,在浏览器上使用IndexedDB,在node.js和浏览器上都使用websockets )。
关键是这条线??唯一新的行,其他的东西都用了好几个月了。
SystemJS.set('systemjs', SystemJS.newModule(SystemJS));一个类似的sinon已经运行了几个月,我希望我可以使用完全相同的方法来使模块使用require('systemjs')时已经可用的SystemJS可用。但不起作用。
这就是模块在其require之后所看到的

实际的SystemJS模块内容位于该Symbol(): tt下面。
我相信答案非常简单,但是SystemJS的文档对我没有任何帮助,当然这完全是我自己的错,但事实就是这样,两个小时后我不得不问,没有结果。我也花了一段时间来完成当前的设置,包括必须阅读SystemJS源代码,我自己解决了这一切,但我认为这个微小而又非常具体的问题对于这样的问题来说可能是合理的吗?
为了完成对设置的描述(对于那些好奇的人):我从WebStorm运行它,目录设置是一个通常的node.js项目,包括./test/__、./src/__、./lib/__,当然还有./node_modules/__。html文件是./test/index.html,我告诉WebStorm“运行”它,所以WS充当测试的WS服务器。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Browser Tests</title>
<link href="https://cdn.rawgit.com/mochajs/mocha/v3.4.1/mocha.css" rel="stylesheet"/>
<script src="https://cdn.rawgit.com/mochajs/mocha/v3.4.1/mocha.js"></script>
<script src="http://sinonjs.org/releases/sinon-2.2.0.js"></script>
<script src="../node_modules/systemjs/dist/system.js"></script>
<style>
#errors pre {
width: 50em;
margin: 2em 4em;
padding: 1em;
border: 1px solid red;
}
</style>
</head>
<body>
<div id="errors"></div>
<div id="mocha"></div>
<script>
/*global SystemJS */
mocha.setup('bdd');
// This hack switches the system-dependent components so that require()
// statements for modules with node.js specific code are exchanged for their
// browser versions.
const resolveOrig = SystemJS.constructor.prototype.resolve;
SystemJS.constructor.prototype.resolve = function (key, parents) {
return resolveOrig.call(SystemJS, key.replace('-nodejs.js', '-browser.js'), parents);
};
// ======================================================
// RELEVANT SECTION (what I tried)
// THIS WORKS:
SystemJS.set('sinon', SystemJS.newModule(sinon));
// THIS DOES NOT WORK:
SystemJS.set('systemjs', SystemJS.newModule(SystemJS));
// ======================================================
// These are the test scripts in ./test/ that I want to run in the browser
const mochaTestScripts = [
// ... ABRIDGED LIST ....
'crypto-helpers',
'map-query',
'object-helpers',
'storage'
];
SystemJS.config({
map: {
'chai': '../node_modules/chai/chai.js',
'chai-as-promised': '../node_modules/chai-as-promised/lib/chai-as-promised.js',
// dependency of chai-as-promised
'check-error': '../node_modules/check-error/check-error.js',
'js-sha256': '../node_modules/js-sha256/build/sha256.min.js'
}
});
Promise.all(
mochaTestScripts.map(testScript =>
SystemJS.import('./' + testScript + '-test.js')
.catch(err => {
const div = document.getElementById('errors');
const pre = document.createElement('pre');
pre.appendChild(document.createTextNode('Test: ' + testScript + '\n\n' + err));
div.appendChild(pre);
})
)
)
.then(() => {
mocha.checkLeaks();
mocha.globals([]);
mocha.run();
})
.catch(err => {
const div = document.getElementById('errors');
const pre = document.createElement('pre');
pre.appendChild(document.createTextNode('GLOBAL ERROR\n' + err));
div.appendChild(pre);
});
</script>
</body>
</html>
发布于 2017-05-15 20:02:19
我自己解决了:
为了找到答案,我不得不访问SystemJS的源代码。问题似乎是,SystemJS符号不只是一个普通的对象,方法直接在这个对象中。相反,SystemJS是一个实例,方法在原型上。
最终起作用的是SystemJS如何在内部使用newModule,最后一个有效的命令是
SystemJS.registry.set(
'systemjs',
SystemJS.newModule({default: SystemJS, __useDefault: true})
);这将替换上面的// THIS DOES NOT WORK:测试运行程序文件中的index.html行。
https://stackoverflow.com/questions/43983850
复制相似问题