简体   繁体   English

在命令行脚本中包含没有HTML或DOM的JS文件中的Javascript文件

[英]include Javascript file in JS file without HTML or DOM in command line script

I'm trying to write some simple tests on JS code that can be run from the command line to test code that has nothing to do with HTML, documents or user interface. 我正在尝试编写一些关于JS代码的简单测试,这些测试可以从命令行运行,以测试与HTML,文档或用户界面无关的代码。 To do this I need to include one file within another to pull the code being tested into the test script. 为此,我需要在另一个文件中包含一个文件,以将测试的代码拉入测试脚本。 What I've found involved HTML or DOM to do the job; 我发现我需要使用HTML或DOM来完成这项工作; for example, something like document.write( ) or some such. 例如,像document.write()或类似的东西。 Is there a simple way to do this? 有一个简单的方法吗? I'm imaging something like include( "code2test.js" ); 我正在想象像include(“code2test.js”); I'd appreciate any help in solving this. 我很感激你解决这个问题的任何帮助。 Can JQuery help or does it have to be used in a HTML/browser context? JQuery可以帮助还是必须在HTML /浏览器上下文中使用?

Thanks in advance.j 在此先感谢.j

It sounds like you just need two script tags: 听起来你只需要两个脚本标签:

<script type="text/javascript" src="code2test.js"></script>
<script type="text/javascript" src="testscript.js"></script>

If that doesn't work, try reversing the order. 如果这不起作用,请尝试撤消订单。

Sounds like what you need is Require.js . 听起来像你需要的是Require.js It's designed to allow inclusion of javascript in the web page using javascript rather than script tags. 它旨在允许使用javascript而不是脚本标记在网页中包含javascript。 For example, instead of: 例如,而不是:

<script scr="foo.js"></script>
<script>
    use_foo_here();
</script>

using Require.js you can write: 使用Require.js你可以写:

require(["foo.js"],function(foo){
    use_foo_here();
})

The cool thing about Require.js is that it can even be used on Node.js . Require.js的酷炫之处在于它甚至可以在Node.js上使用。 So for command line invocation you can use Node to run your scripts and the require() statements would work just like it does on the web page. 因此,对于命令行调用,您可以使用Node来运行脚本,而require()语句就像在网页上一样。

如果您只想在不使用浏览器的情况下访问javascript引擎(例如Webkit),可以使用nodeJSPhantomJS之类的东西

We are using Chutzpah as our test runner, and are very satisfied with it. 我们使用Chutzpah作为我们的测试运行员,并且非常满意。 We write the tests using Jasmine . 我们使用Jasmine编写测试。 Jasmine does not require a DOM. Jasmine不需要DOM。

At the start of the test file, the references to the JavaScript files under test are added, like this: 在测试文件的开头,添加了对测试中的JavaScript文件的引用,如下所示:

/// <reference path="dependantModule.js" />
/// <reference path="code2test.js" />

and then the test code follows: 然后测试代码如下:

describe("code2test test suite", function () {

    it("should do something"", function () {
        var result;

        // Assuming code2test.js exposes a global called 'code2test'
        result = code2test.doSomething();
        expect(result).toEqual("the expected result");
    });
});

Chutzpah uses the PhantomJS headless browser. Chutzpah使用PhantomJS无头浏览器。 So you can write tests that interact with the DOM if required. 因此,如果需要,您可以编写与DOM交互的测试。

We run the tests through Chutzpah from the command line for continuous integration, but also run them inside Visual Studio 2010 using the Chutzpah Visual Studio Extension. 我们从命令行通过Chutzpah运行测试以进行持续集成,但也使用Chutzpah Visual Studio Extension在Visual Studio 2010中运行它们。 I believe integration of Chutzpah in VS2012 is even easier, but haven't tried it myself. 我相信Chutzpah在VS2012中的集成更容易,但我自己没有尝试过。

The tests can also be ran inside a 'real' browser, which is great for debugging. 测试也可以在“真正的”浏览器中运行,这对于调试非常有用。 My browser of choice for debugging the test code is Chrome - the developer tools are great. 我调试测试代码的首选浏览器是Chrome - 开发人员工具非常棒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM