gpt4 book ai didi

node.js - buster.js 测试无法加载我的 js 文件

转载 作者:搜寻专家 更新时间:2023-10-31 23:55:27 26 4
gpt4 key购买 nike

从昨天开始我就一直在用头撞墙(只是被昨晚一场很棒的 Bruce Springsteen 音乐会打断了......;-)

我正在尝试向现有 (Rails) 项目添加一些 JS 测试。基本上文件夹结构是这样的:

.
├── app
│   └── assets
│   └── javascripts
│   └── strftime.js
└── spec
├── buster.js
└── javascripts
└── strftime.test.js

在这个非常简单的示例中,我使用了此处的 buster 示例 https://gist.github.com/cjohansen/1904218在移动文件之前确保一切正常。

我的 buster.js 看起来像这样:

var config = exports;

config["Server tests"] = {
// sources: ["../app/assets/javascripts/strftime.js"],
tests: ["javascripts/strftime.test.js"],

env: "node"
};

我尝试将 strftime.test.js 的前 5 行修改为:

if (typeof require == "function" && typeof module == "object") {
buster = require("buster");

require("../app/assets/javascripts/strftime.js");
}

目前,当我运行 buster 测试时,我得到了这个:

$ buster test
Failed requiring ./spec/javascripts/strftime.test.js: Cannot find module '../app/assets/javascripts/lib/strftime.js'

如果我尝试取消对 sources 行的注释,buster test 会无提示地失败 - 即使我尝试以高调试级别运行它也是如此。

我已经压缩了整个示例 (2 KB) 并把它放在这里,如果有人想试一试:http://gehling.dk/b2.zip

更新:我通过提供相对于我的测试 js 文件的路径成功地要求 js 文件:

if (typeof require == "function" && typeof module == "object") {
buster = require("buster");

require("../../app/assets/javascripts/strftime.js");
}

但如果我添加 sources 参数,我仍然会遇到 buster 静默失败的问题。

TIA

/卡斯滕

最佳答案

在压缩的 buster.js 中,您在“sources”元素中引用了一个不存在的 lib 目录:

var config = exports;

config["Server tests"] = {
// sources: ["../app/assets/javascripts/lib/strftime.js"],
tests: ["javascripts/strftime.test.js"],

env: "node"
};

您还在 strftime.test.js 中引用了相同的 lib 目录。

通过将 buster.js 更改为如下所示:

var config = exports;

config["Server tests"] = {
rootPath: "../",
environment: "node",
sources: ["app/assets/javascripts/strftime.js"],
tests: ["spec/javascripts/strftime.test.js"],
};

然后将 strftime.test.js 中的 require 语句更改为:

require("../../app/assets/javascripts/strftime.js");

然后使用 spec 目录中的 buster-test 运行测试,您将让 buster 尝试运行一些测试:

$ spec  buster-test
Failure: Date strftime tests %j should return the day of the year
...

另外,不要忘记使用 npm link buster 将 buster 链接到您的项目。

下载修复文件: http://speedyshare.com/FGvCe/b2.tar.gz

关于node.js - buster.js 测试无法加载我的 js 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16604342/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com