本文介绍了Require.JS 和 JS 测试驱动程序:Unexpected token <的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试通过 RequireJS 加载的简单主干模型:

I am trying to test a simple Backbone Model loaded via RequireJS:

define ["backbone"], (Backbone)->

    class Todo extends Backbone.Model
        defaults:
            title: ''
            priority: 0
            done: false

        validate: (attrs) ->
            errs = {}
            hasErrors = false

            if (attrs.title is "")
                hasErrors = true
                errs.title = "Please specify a todo"

            if hasErrors
                return errs

        toggleDone: ->
            @save("done", !@get("done"))

    return Todo

我的测试看起来像:

requirejs.config
    baseUrl: "js/"
    paths:
        jquery: "https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min"
        jqueryui: "https://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min"
        json2: "http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2"
        underscore: "http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min"
        backbone: "http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min"
        backboneLocalStorage: "https://raw.github.com/jeromegn/Backbone.localStorage/master/backbone.localStorage-min"
    shim:
        "underscore":
            exports: "_"
        "backbone":
            deps: ["jquery", "underscore", "json2"]
            exports: "Backbone"
        "jqueryui":
            deps: ["jquery"]
        "backboneLocalStorage":
            deps: ["backbone"]
            exports: "Backbone.LocalStorage"

require ["models/todo"], (Todo) ->

    console.log Todo

    TodoTests = TestCase("TodoTests")

    TodoTests::testCreateTodo = ->
        todo = new Todo({ title: "Hello" })
        assertEquals "Hello", todo.get("title")
        assertEquals 0, todo.get("priority")
        assertEquals false, todo.get("done")

JS 测试驱动配置:

server: http://localhost:3001

load:
  - ../public/js/libs/require.js
  - ../public/js/tests.js

serve:
  - ../public/js/models/*
  - ../public/js/collections/*
  - ../public/js/views/*

从 Chrome 控制台上的 JS 测试驱动程序监听页面看到的问题:

Problem seen from the JS Test Driver listened page on Chrome console:

Uncaught SyntaxError: Unexpected token <

从 Chrome 看 Todo.js,

Looking at Todo.js from Chrome,

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>Console Runner</title><script type="text/javascript">var start = new Date().getTime();</script>
Uncaught SyntaxError: Unexpected token <
<script src="/static/lib/json2.js" type="text/javascript"></script><script src="/static/lib/json_sans_eval.js" type="text/javascript"></script><script src="/static/jstestdrivernamespace.js" type="text/javascript"></script><script src="/static/lib/jquery-min.js" type="text/javascript"></script><script src="/static/runner.js" type="text/javascript"></script><script type="text/javascript">jstestdriver.runConfig = {'debug':false};</script>
<script type="text/javascript">jstestdriver.console = new jstestdriver.Console();

注意它是一个 HTML 页面,而不是我的实际 JS.此外,console.log(Todo) 返回 undefined,因为返回的是 HTML 页面而不是 JS.我是不是配置错了?

Notice its a HTML page instead of my actual JS. Also console.log(Todo) returns undefined since a HTML page is returned in place of a JS. Did I configure this wrongly?

推荐答案

我为此苦苦挣扎了好几天,用谷歌无休止地搜索,直到我终于找到了 JsTestDriver 在做什么.在您的 requirejs.config 中,为了让您的测试正常运行,您的 baseUrl 需要:

I struggled with this for days and searched with Google endlessly until I finally found out what JsTestDriver was doing. In your requirejs.config, for your tests to run properly, your baseUrl needs to be:

baseUrl: /test/path/to/your/stuff

原因是当 JsTestDriver 创建它的服务器时,它会将 /test/ 添加到所有目录中.如果 baseUrl 设置不正确,它会开始尝试向本地发回我认为的对窗口的引用.

The reason is when JsTestDriver makes its server, it prepends /test/ to all the directories. If the baseUrl isn't set properly, it starts trying to send back local a reference to window I think.

我看到您可能遇到的唯一其他问题是在第一行使用 require 语句运行测试.我有 Jasmine 并将我的 require() 放在我的测试顶部导致它们永远不会运行,所以我必须在我的 beforeEach 中为我的测试执行它并在它们运行之前获取对象.

The only other problem that I see you may run into is running the tests with the require statement as the first line. I have Jasmine and putting my require() at the top of my tests caused them to never run so I had to do it in my beforeEach for my tests and get the object before they ran.

虽然我看到无数其他人声称 Jasmine 中的 require 语句有效,但我可能做错了什么,所以我的搜索仍在继续.

I'm probably doing something wrong though I see countless other people claiming that the require statement in Jasmine works, so my hunt continues.

这篇关于Require.JS 和 JS 测试驱动程序:Unexpected token <的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 11:21
查看更多