How to start running test files on my application in Javascript? - javascript

I finished developing an application ,and I have a ready test.js files for the client side and for server side. The tests are in Mocha ,Chai and Sinon. I don't know how to connect the application files with the test files and how should I run them.
I appreciate any help/guidelines.

My suggestion is to have a tests folders in root directory(where your main js file is) and put test.js in there. Then just execute: mocha tests --recursive
This command will run all the tests.

Related

Deploy node.js / express / mongoDB ( with typescript ) in heroku

Before, in the backend i would not use typescript, i would just use pure javascript, but know, this is my first backend app with typescript.
I want to deploy of course this app to heroku, but, i wonder if in the procfile component i have to tell heroku to get the server running in the server.ts, or in the server.js in the dist folder.
What i'm trying to say, is that, should i compile my code to translate it into pure javascript in the dist folder in order to deplot, or is it okay just to just run the server in the server.ts file ?
This is how my project looks like
TypeScript is ultimately just there to help you structure your code properly, as it is meant to be compiled to JavaScript. While you can run your server via ts-node on a VM, it is simpler to just compile it to JS and run it on your VM, as at that point it is no different from a typical NodeJS application. Other advantages are smaller build files since your js files will take up less space and startup time.
That being said there is no stopping you from using ts-node on heroku by putting ts-node index.ts inside your package.json start script, (also ts-node should be in dependencies not devDependencies), but personally I'd go down the compilation route as that is pretty much just another NodeJS application.
See Heroku can't find ts-node

Code coverage of Meteor application after deployment

Meteor application has built with 'meteor build' command and then deployed. After deployment application runs using plain node command 'node bundle/main.js'.
Running nyc using command 'nyc node main.js' only give the partial results from the meteor generate files.
Code coverage result
Is there anyway to get the full code coverage results? I am only interested to get result about javascript files.

Environment variable in Javascript (Gulp)

I have a local development machine and a test server.
Now I have an APP_ID that's being used in Javascript. I've been looking into ways that they kan differ on my local machine and on the test server.
Using Gulp
With gulp it's possible to add a flag on the command line:
gulp build --env=production
That way I can get the correct APP_ID from a file.
The only issue is with this approach I need to run my build on the server, at this moment I run gulp locally and upload the changes to my server
Is it okay to build on the server? Are there other ways to use environment variables in Javascript?
My suggestion is to not build on the server but build locally and then deploy to the server using one of the many deploy solution (es. deployer.org for php). Normally javascript NPM packages put build output even in GIT repository ready for use in other projects or for deploy.
For more info on how to use env variable in node (gulp run over node) see this page
For example in linux you can set env variable with export
app.js :
console.log(console.log(process.env.foo))
Then try
> export foo=app1
> node app.js
Res:
app1
Then try
> export foo=app2
> node app.js
Res:
app2
This is valid only if you run your code server side on node (ex on gulp).
If you are developing a client side library and you want to create different builds that targets different enviroments you have to instruct gulp to do so. In this case this is a guide that can help you

How does testing framework mocha knows, which source file I am testing?

talking about --> http://mochajs.org/
my app is not a node app, it's a web app, I am trying to use tdd approach using mocha and chai.
I am confused, on how does mocha understand, that this x test case file is for x source file?
e.g.
source file is userArea.js
test file will be in /test folder --> userArea.spec.js (this will have test cases)
When I run test cases through mocha, how does it identify, for what source files, I am running tests?
Mocha knows what test files it is loading (your *.spec.js files) but it does not specifically know or track what files the tests are testing. When you get a stack trace upon failure, that's not Mocha computing the trace but the JavaScript virtual machine. When you run Mocha in a browser with the HTML interface, you can click to see source code, but that's the source of the test, not the source of the code under test.

How to I configure TeamCity build with Mocha?

I have a project that I am setting up through teamcity for CI.
The project itself is a nodejs application and it includes test written in mocha, which we cover through jscoverage. In the build configuration I'm setting up I have 3 build steps
which occur on checkin.
call jscoverage.exe against the folders in my project that I'm
covering.
call mocha to run the test against the jscovered files from step 1
and output to the html-cov reporter
move the generated coverage.html report into a public web directory
to browse later.
The build currently fails on step 2:
mocha" is not present in directory C:\NodeJS\MeasuresAPI
I've made sure to include mocha and all my node packages in the system environment paths and I am able to access them in the command prompt, but TeamCity doesnt appear to see them.
for the jscoverage.exe, I had to include the full path. With mocha, I tried including the path to my node global installation where mocha installed to but it gives me an error:
"..\node_modules\mocha\bin\mocha" (in directory "C:\NodeJS\MeasuresAPI"): CreateProcess error=193, %1 is not a valid Win32 application
Anyone had any experience with Teamcity and Mocha and how to get them to play nice?
or any ideas for continuous integration with a nodejs, mocha stack?
Yes , this happened to me too, when I was setting up TeamCity to run mocha on Windows Server. The solution was to call mocha by specifying path to the mocha.cmd bat file. For example , if you have folder C:\mocha and you have executed
npm install mocha
in that directory , than path to the bat file will be
C:\mocha\node_modules.bin\mocha.cmd
And you can tell Teamcity to execute mocha command by giving it next instruction :
C:\mocha\node_modules.bin\mocha --ui tdd --reporter html-cov test\measureDBTests.js > coverage.html

Categories