简体   繁体   English

使用Typescript和Ava设置测试:不好的要求

[英]Set up tests with Typescript and Ava : bad require

I'm setting up AVA with Typescript to tests my JS code. 我正在使用Typescript设置AVA以测试我的JS代码。 Internally, I'm first calling TSC to compile my files, then call AVA with babel-register to test them (Babel register allowing require to be resolved). 在内部,我首先调用TSC来编译我的文件,然后使用babel-register调用AVA对其进行测试(Babel注册允许需要解决)。

"ava": "tsc && ava testJs/**/*.spec.js"

testJs being the output folder of Ts. testJs是Ts的输出文件夹。 My problem is that, even thought everything pretty much work, I have this kind of statement, usually picked up by Webpack : 我的问题是,即使认为一切都可以正常工作,我还是有这种陈述,通常是Webpack提出的:

import "./index.page.css";

Webpack gently require it, but babel-register doesn't. Webpack轻轻地要求它,但是不需要babel-register。 I had to change the behavior to accept .css file as noop. 我不得不更改行为以将.css文件接受为noop。 Here is the issue : because I'm using tsc as a compiler, those files are not copied at all in testJs, meaning they are not available in the first place. 这是问题所在:因为我将tsc用作编译器,所以这些文件根本不会在testJs中复制,这意味着它们最初不可用。

I wanted to know what would be the best way to solve this, as I think copy-pasting the whole folder (to have all files available) just to execute tests is a bit of an overkill. 我想知道什么是解决此问题的最佳方法,因为我认为复制粘贴整个文件夹(以使所有文件可用)只是为了执行测试有点过头。 Especially since, if I suddenly import a .json file (for example)I will have new problems. 特别是因为,例如,如果我突然导入.json文件,我将遇到新的问题。

For example, is there a way to tell babel-register to ignore require it cant resolve instead of breaking ? 例如,是否有一种方法可以告诉babel-register忽略它不能解析而不是破坏的要求?

Thanks ! 谢谢 !

You can use ignore-styles to ignore certain types of requires. 您可以使用忽略样式来忽略某些类型的需求。 By default it ignores all kinds of CSS and images ( full list ) and you can customise it to ignore other extensions as well. 默认情况下,它忽略所有CSS和图像( 完整列表 ),并且您也可以对其进行自定义以忽略其他扩展名。

You simply require it before babel-register in your AVA config. 您只需在AVA配置中进行babel-register之前就需要它。

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

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