node.js - 如何将 jasmine 与服务器端 typescript 项目一起使用?

标签 node.js typescript testing jasmine

我有一个项目,其中包含我的应用程序的服务器和客户端模块,每个模块都是使用 webpack 构建的。我正在使用 Karma 和 Jasmine 来测试我的客户端(使用 Angular),我想使用 Jasmine 来测试服务器,它也是用 typescript 编写的。

不幸的是,我在网上能找到的唯一指南使用的是 jasmine-node(过去几年未被触及)而不是 jasmine-npm。谁能建议我可以使用 Jasmine 或替代方法在我的项目中进行测试?

我试过编写一个 jasmine.json 文件,或使用 init cli 命令编辑 jasmine 生成的文件,但这似乎不适用于 typescript 文件。

目前,我的项目结构是这样的:

├── client
│   ├── karma.conf.js
│   ├── protractor.conf.js
│   ├── src
│   ├── tsconfig.json
│   └── webpack.config.js
├── server
│   ├── src
│   ├── tsconfig.json
│   └── webpack.config.js
└── node_modules

最佳答案

绝对可以使用 jasmine 进行服务器端测试。请按照以下步骤操作,一切都会好起来的:

1) 在您的 package.json 中添加以下开发依赖项:

"jasmine": "latest",
"jasmine-core": "latest",

2) 设置你的 jasmine.json 以便它包含你想要运行测试的所有文件,等等。

{
  "spec_dir": "dist/dev/server/spec",
  "spec_files": [
    "unit/server/**/*[sS]pec.js"    
  ],
  "helpers": []
}

3) 添加 unit.ts 来引导您的测试过程:

const Jasmine = require("jasmine");

let j = new Jasmine();

j.loadConfigFile("./jasmine.json");
j.configureDefaultReporter({
    showColors: true
});
j.execute();

现在您所要做的就是在 nodejs 中编译并运行生成的 unit.js

关于node.js - 如何将 jasmine 与服务器端 typescript 项目一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43992119/

相关文章:

javascript - 需要了解幕后 | Node .js

javascript - 在 ejs 部分中迭代 JSON 对象

node.js - 洞察两种 REST API 设计的可行性

json - Typescript 提示导入的 json 模块缺少分号

testing - Selenium Webdriver 异常 : waitForPageToLoad is not a valid webdriver command?

php - 预览 PHP 中的电子邮件开发

node.js - ExpressJS View 中的变量

Angular 8 : TypeError: Found non-callable @@iterator

angular - 使用 ionic/cordova 时出现意外的 token 错误

angular - 为什么预定义变量在 Cypress 中的行为不符合预期?