简体   繁体   English

用es6 + jspm + systemjs + reactJS开玩笑

[英]jest testing with es6 + jspm + systemjs + reactJS

I'm trying to figure out how to make my unit tests in my reactJS ES6 application. 我试图找出如何在我的reactJS ES6应用程序中进行单元测试。 My application is already using es6 module system, transpiled with jspm/babel to systemJs. 我的应用程序已经在使用es6模块系统,并通过jspm / babel转换为systemJs。

I found babel-jest as preprocessor but even with it, I can't run my tests because jest can't find SystemJs. 我发现babel-jest作为预处理器,但是即使有了它,我也无法运行测试,因为jest无法找到SystemJ。 ( "System is not defined" error is shown in the console) (控制台中显示“未定义系统”错误)

In the browser, as explained in jspm documentation, SystemJs is loaded globally. 如jspm文档中所述,在浏览器中,SystemJs是全局加载的。 I guess I should load SystemJs inside my preprocessor, but How can I make systemJs available for loading additional modules in my tests? 我想应该在预处理器中加载SystemJ,但是如何使systemJ可用于加载测试中的其他模块?

Thanks in advance 提前致谢

Unfortunately, Jest does not support SystemJS ES6 modules at the moment. 不幸的是,Jest目前不支持SystemJS ES6模块。

See the following comments: 请参阅以下注释:

So it sounds like jest assumes that your modules resolve based on the Node resolution algorithm. 因此,听起来好像开玩笑地假设您的模块基于节点解析算法进行解析。

Unfortunately this isn't compatible with SystemJS's resolution algorithm. 不幸的是,这与SystemJS的解析算法不兼容。

If there was a way in jest to set a "custom resolver" algorithm through an API then we could plug jspm into jest, but I'm not sure if this is currently possible. 如果在jest中有一种方法可以通过API设置“自定义解析器”算法,那么我们可以将jspm插入到jest中,但是我不确定目前是否可行。

-- Comment by Guy Bedford , creator of SystemJS, Jun 2015 -SystemJS创始人Guy Bedford的评论,2015年6月


It is unlikely there'll be official support for [SystemJS] unless it is a community contribution. 除非它是社区的贡献,否则不太可能获得[SystemJS]的官方支持。

-- Comment by @cpojer , Jest Collaborator, Jan 2016 -Jest合作者@cpojer的评论 ,2016年1月


Also see the following issue: Invalid URL is thrown when requiring systemjs in jest test cases 另请参阅以下问题: 在简单的测试案例中要求使用systemjs时,将抛出无效的URL

in essence to get Jest to play nice with an app running on JSPM/SystemJS you need to "teach" it about all the mapping it holds in the config.js file (or however you call System.config()) 从本质上讲,要使Jest与运行在JSPM / SystemJS上的应用程序搭配使用,您需要“教”它有关config.js文件中包含的所有映射的信息(或者您调用System.config())

the long answer is that you need to create an entry for each dependency you have installed with JSPM like this: 长答案是,您需要为使用JSPM安装的每个依赖项创建一个条目,如下所示:

//jest configuration 
moduleNameMapper: {     
   ...
   "^redux$": "redux@3.6.0",
   ...
}

for each alias you have, you need at least one entry: 对于您拥有的每个别名,您至少需要一个条目:

moduleNameMapper: {     
        ...
        "^common\\/(.*)": "<rootDir>/src/common/$1", //for a dir alias
       "^actions$": "<rootDir>/src/actions/index", //for a file alias
       ...
}

you also need to have these mappings in your nodeNameMapper : 您还需要在nodeNameMapper中具有以下映射:

moduleNameMapper: {     
    ...
         "^npm:(.*)": "<rootDir>/jspm_packages/npm/$1",
            "^github:(.*)": "<rootDir>/jspm_packages/github/$1",
    ...
}

and finally, you need to have this moduleDirectories config: 最后,您需要具有以下moduleDirectories配置:

moduleDirectories: ["node_modules", "jspm_packages/npm", "jspm_packages/github"]

this isnt really practical because you dont want to keep two copies of all your dependencies registry and have to sync them when they change... 这真的不切实际,因为您不想保留所有依赖项注册表的两个副本,并且在更改时必须同步它们...

so the short and better answer, you use my gulp-jest-jspm plugin :) 所以简短更好的答案是,您使用我的gulp-jest-jspm插件:)

even if you dont use gulp, you can use its getJestConfig() method to generate the config for you before you run Jest. 即使您不使用gulp ,也可以在运行Jest之前使用其getJestConfig()方法为您生成配置。

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

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