捆绑后,在浏览器中测试JavaScript/Typescript的一种方法

发布于 2025-02-06 10:17:56 字数 169 浏览 1 评论 0原文

我正在和我的朋友一起写网络应用。他写了前面的书,我写了后端。问题是,如果我的朋友还没有编写集成,我无法测试代码,因为我们将代码捆绑在Web Pack中并在Dev Server上运行。我的问题是,是否有一种方法可以在Chrome控制台中测试我的打字稿代码(带有参数的调用功能),就像我可以测试未捆绑的JavaScript一样?

I'm writing a web app with my friend. He writes the Fronted, I write the Backend. Problem is that there is no way of me testing my code if my friend haven't wrote the integration yet, because we bundle the code in web pack and run it on a dev server. My question is, is there a way that I could test my typescript code (call functions with parameters) in chrome console, like I could test unbundled javascript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〗斷ホ乔殘χμё〖 2025-02-13 10:18:01

专业答案是:不要那样做!

  • 使用 Postman 测试实际服务器。
  • 为您的API处理客户端代码编写单元测试,该测试通过JEST或其他一些单元测试库拦截对服务器的调用(因为这些不是单位测试)。
  • 为前端编写单元测试,该测试实际上并未触及您的中间码。
  • 将单位测试转换为可以在可能的地方进行断言,因为案例覆盖范围比代码覆盖范围,您在单位测试中无法想到所有内容。
  • 编写以用户为中心的集成测试,而不是以小部件为中心。

这很难强大的大“如果”!

因此,现实答案可能是:写...或让您的朋友写...一个丑陋但功能的页面,您可以使用一些值来输入一些值,然后单击按钮提交它,然后然后添加调试器作为按钮代码开始附近的语句。 debugger是一个硬码断点,开发工具将暂停。

一旦编写和工作,您应该能够根据需要对其进行修改。 ...只记得不要让该页面逃脱到生产中。

The professional answer is: Don't do that!

  • Test the actual server using Postman.
  • Write unit tests for your api-handling client code that intercepts calls to the server (because those aren't unit tests), via jest or some other unit testing library.
  • Write unit tests for the front end that doesn't actually touch your middle-code.
  • Convert unit tests to assertions where you can, because case coverage beats code coverage and you can't possible think of everything during unit tests.
  • Write integration tests that are user-centric, not widget centric.

And that's hard but you'll be glad you did it... if you can do it without the project dying because of it! For a project that is just you and a friend, that may be a mighty big "if"!

So the realistic answer is probably: Write... or get your friend to write... an ugly-but-functional page you can use to enter some values and click a button to submit it, and then add debugger as a statement near the start of the button code. debugger is a hardcode breakpoint that dev tools will pause at.

Once that's written and working you should be able to modify it as needed. ...just remember not to let that page escape into production.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文