1

I'm trying to run JS unit tests using Windows Script Host. But the issue that I'm stuck with is how to mock Browser Objects and DOM objects without opening a browser.

I know I can use mocking libraries to mock something for each instance, but I want know if there are libraries that in general can mock DOM Objects like Document, Event etc. same goes with Browser Objects like Window, Screen.

Someone pointed me to MochiKit I'm trying to see if it is possible to use this for the intended task, but it will be a great help if anyone can point me to mocking libraries.

1 Answer 1

4

jsdom is a reasonably full DOM emulation.

Just run your scripts in it and they should work with the server-side DOM.

This should allow you to unit test your code.

Alternatively inject your dependencies into your units and mock out any dependencies your units should have. The units should be small and mocking should be easy

Sign up to request clarification or add additional context in comments.

2 Comments

Ohh nice!! That looks like a solution to all my problems, thanks for a quick reply.
So I tried this, but here's the issue I'm facing. I can use JSDOM when accessing this from command prompt. But what I'm trying to do is something different. - I'm trying to load a bunch of JS files in WSH and executing them. This code "var jsdom = require("jsdom").jsdom, document = jsdom("<html><head></head><body>hello world</body></html>"), window = document.createWindow();" loads up fine but looks for node environment to execute, do you know how to call node environment from WSH?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.