Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript: How do you test your client-side code?

When I write tests for my in-browser TS code, I hit the following problem. My "test" code files are located in a separate folder from the "application" code files (an arrangement that I am not willing to give up). Therefore, in order to import my "app" modules, I have to do this:

    // tests/TS/SubComponent/Module.Test.ts
    import m = module("../../Web/Scripts/SubComponent/Module");

This compiles just fine. But when loaded in browser, it will obviously not work, because from the standpoint of RequireJS running in the browser, the module is located at "app/SubComponent/Module" (after being remapped through web server and RequireJS config).

With TS 0.8.3 I was able to pull off this clever trick, but in 0.9.0 it no longer works, because now the compiler doesn't let me treat a module as an interface.

So the question is: how do you test your client-side code? Clearly, I can't be the only person to be doing it, can I? :-)

like image 377
Fyodor Soikin Avatar asked Oct 03 '22 18:10

Fyodor Soikin


1 Answers

I can't tell if you are using Visual Studio - this next bit is Visual Studio specific...

This is how I do it:

In my test project, I created a folder named "ReferencedScripts" and referenced the scripts from the project being tested (add existing item > add as link). Set the file to copy to the output folder.

Source: Include JavaScript and TypeScript tests in Visual Studio.

Using add-as-link makes the scripts available in your test project.

Not using Visual Studio? I recommend creating a task / job / batch file to copy the files into the test folder. You could even use tsc to do this task for you.

like image 104
Fenton Avatar answered Oct 08 '22 14:10

Fenton