简体   繁体   English

测试同构javascript应用程序

[英]Testing for isomorphic javascript application

I have a Javascript application that consists of client-side code and server-side (Node/Express) code. 我有一个Javascript应用程序,包括客户端代码和服务器端(Node / Express)代码。 Is there a testing solution that covers both client and server, or do you have to run seperate test frameworks for each? 是否存在涵盖客户端和服务器的测试解决方案,或者您是否必须为每个测试框架运行单独的测试框架?

Reason I ask is because I am trying to share code between client and server, eg a helper function that generates a unique id. 我问的原因是因为我试图在客户端和服务器之间共享代码,例如生成唯一ID的辅助函数。 Is this a good idea, or is it better to separate the concerns? 这是一个好主意,还是分开关注点更好?

This is a complicated question with multiple answers. 这是一个复杂的问题,有多个答案。 tl;dr You can do a lot of testing server side, but usually at some point you end up wanting client side tests. tl; dr你可以做很多测试服务器端,但通常在某些时候你最终需要客户端测试。

In many respects, even though javascript applications can be isomorphic, the actual execution environment of client side vs. server side is very different when it comes to globals and language features. 在许多方面,即使javascript应用程序可以是同构的,当涉及全局和语言功能时,客户端与服务器端的实际执行环境也是非常不同的。 To be a completionist on testing-- one would need to have both client side and server side testing. 要成为测试的完成者 - 需要同时进行客户端测试和服务器端测试。

From a more pragmatic standpoint-- depending on the implementation and modularity of the javascript application one can choose to minimize or eliminate entirely the client side testing with the following methods: - Use jsdom to replace the DOM api in node - For isomorphic React applications use jest + react-addons-test-utils and/or enzyme as test harnesses into component instances - Stub out browser globals as needed and test modules in isolation 从更实用的角度来看 - 根据javascript应用程序的实现和模块化,可以选择使用以下方法最小化或完全消除客户端测试: - 使用jsdom替换节点中的DOM api - 对于同构React应用程序使用jest + react-addons-test-utils和/或enzyme作为组件实例的测试工具 - 根据需要存储浏览器全局变量并单独测试模块

At some point though, you almost always want to spin up a browser for "real world" testing. 但是,在某些时候,你几乎总是希望通过浏览器来进行“真实世界”的测试。 This could come in the form of holistic solutions like karma or rolling your own with mocha + browserify / webpack for units tests or nightwatch for integration / acceptance tests. 这可以派上像整体解决方案的形式, karma或滚动自己mocha + browserify / webpack为单位测试或nightwatch集成/验收测试。

Besides the browser globals, the other important distinction between server side and client side tests is that server side tests never have any dynamic behavior. 除了浏览器全局,服务器端和客户端测试之间的另一个重要区别是服务器端测试从不具有任何动态行为。 That lives entirely in the client. 这完全存在于客户中。 To the amount your application depends on browser features, rather than isomorphic library features (eg pure React) then you will find a greater and greater need for client side testing. 对于应用程序依赖于浏览器功能的数量而不是同构库功能(例如纯React),您会发现对客户端测试的需求越来越大。

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

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