简体   繁体   English

如何在Angular / CLI构建打字稿项目中使用Ava

[英]How to use Ava with Angular/CLI build typescript project

As a part of a project I am building I currently have an angular/cli typescript project I have created. 作为我正在构建的项目的一部分,我目前有一个已创建的angular / cli打字稿项目。 We are currently using the karma testing framework, but are extremely interested in switching to AVA if possible. 我们目前正在使用业力测试框架,但如果可能的话,对转换到AVA极为感兴趣。

I've currently done a npm install --save-dev ava And I've looked at the typescript setup in the ava documentation https://github.com/avajs/ava/blob/master/docs/recipes/typescript.md 我目前已经完成了npm install --save-dev ava并且在ava文档https://github.com/avajs/ava/blob/master/docs/recipes/typescript.md中查看了打字稿设置。

I've also followed the setup on the main page itself and added an "ava" section to my package.json 我还按照主页上的设置进行了设置,并在package.json中添加了“ ava”部分

"ava": {
  "files": [
    "**/*.spec.ts"
  ],
  "source": [
    "**/*.ts"
  ],
  "match": [],
  "concurrency": 5,
  "failFast": true,
  "failWithoutAssertions": false,
  "tap": true,
  "powerAssert": false,
  "require": [
    "babel-register"
  ],
  "babel": "inherit"
}

I've hoped that somehow by default this would pick up our currently written spec.ts files that have been tailored and written for karma, but it would seem that this isn't so as I'm only given 我希望默认情况下,它将以某种方式获取我们目前为业力定制和编写的spec.ts文件,但似乎并非如此,因为我只知道

# Couldn't find any files to test
not ok 1 - Couldn't find any files to test

1..0
# tests 0
# pass 0
# fail 1

I've also tried obliterating the default test.ts file that is currently put in place by Karma and changing it to 我还尝试消除了Karma当前放置的默认test.ts文件,并将其更改为

import test from 'ava';
test('BLA', t => {
  t.pass();
});

In the hopes that I could at least bootstrap something to work with that I could maybe use to add/implement the rest of our test files with later. 希望我至少可以引导一些与之配合使用的东西 ,以便以后可以添加/实现其余测试文件。 So far I've had no luck with this either. 到目前为止,我也没有运气。

Can someone who has taken this path before tell me what needs to be done to get the Ava test runner up and working; 曾经走过这条路的人能告诉我为使Ava测试运行器正常工作需要做什么吗? and furthermore, what may need to be done to adapt our currently existing karma/jasmine based test framework to Ava? 而且,可能需要做些什么来使我们当前现有的基于业力/茉莉的测试框架适应Ava?

Thank you very much in advance! 提前非常感谢您!

I ran into this answer while searching for something else, and realized it might be worth chiming in, even though it's been a bit since the question was asked. 我在寻找其他东西时遇到了这个答案,并且意识到它也许值得一试,尽管自从提出问题以来已经有一段时间了。

There is a path to run AVA + TS via ts-node . 存在经由运行AVA + TS的路径ts-node Additionally, there's a tsconfig-paths package that aids in correctly mapping to tsconfig 's paths property, if being used. 此外,还有一个tsconfig-paths包,如果使用的话,它有助于正确映射到tsconfigpaths属性。

Below is what the AVA config would roughly look like. 以下是AVA配置的大致外观。 Of course, your paths may be different based on your Angular CLI project structure, but the same basic principles apply: 当然,根据Angular CLI项目结构,您的路径可能会有所不同,但是适用相同的基本原则:

"ava": {
  "compileEnhancements": false,
  "extensions": [
    "ts"
  ],
  "require": [
    "ts-node/register",
    "tsconfig-paths/register"
  ],
  "files": [
    "src/**/*.spec.ts"
  ],
  "sources": [
    "src/**/*.ts",
    "src/index.ts"
  ],
  "failWithoutAssertions": true
}

Additionally, you may have a testing-specific tsconfig , in which case you'd need to point to it via a TS_NODE_PROJECT environment variable that ts-node will pick up on. 此外,您可能具有特定于测试的tsconfig ,在这种情况下,您需要通过ts-node将使用的TS_NODE_PROJECT环境变量来指向它。 So, for example, you're test task might look like this: 因此,例如,您正在test任务可能如下所示:

TS_NODE_PROJECT=tsconfig-spec.json node_modules/.bin/ava --verbose

Note that because Node natively consumes only CommonJS (at least at the time of this writing), your tsconfig will need to compile to that module format. 请注意,由于Node本机仅使用CommonJS(至少在撰写本文时),因此您的tsconfig将需要编译为该模块格式。 So, continuing the example above, your tsconfig-spec.json might look like this, among other desired options: 因此,继续上面的示例,您的tsconfig-spec.json可能看起来像这样,以及其他所需的选项:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2015",
    "sourceMap": true
  }
}

The API between AVA and Jasmine is not the same, but it's actually, in my experience, similar enough that the conversion isn't too painful—mostly mechanical. AVA和Jasmine之间的API并不相同,但是根据我的经验,它实际上非常相似,以至于转换并不会太麻烦,主要是机械转换。 At the time of this writing, there's an issue open to create a codemod for the conversion ; 在撰写本文时, 为转换创建一个codemod存在一个问题。 however, it looks like it hasn't had much traction yet. 但是,看起来好像还没有什么吸引力。

AVA currently only supports test files with a .js extension. AVA当前仅支持扩展名为.js测试文件。 You should precompile your TypeScript test files and change the files pattern to match the tests inside your build directory. 您应该预编译TypeScript测试文件,并更改files模式以匹配构建目录中的测试。

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

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