Reputation: 1934
This may be a broad and controversial question... but here we go.
I have never done unit testing in JavaScript before and I'd like to know what the best practice is with regards to its deployment.
Unit testing itself is quite straightforward (it's simple to create my own unit test or use existing frameworks such as QUnit).
My question is around deployment best practice; should unit test code be deployed into production?
For example: I have an object 'person' with the function 'getname()'; then I have the corresponding unit test for this to assert that the name is what I expect.
Quite simple, right?... but I have never seen an example of a JavaScript unit test "in the wild". So when people deploy their work into productions environments:
I've never unit tested JavaScript before, so I'm not sure I'm asking the right question.
Upvotes: 1
Views: 1581
Reputation: 35884
Tests should always be run locally prior to production deployment.
Test code should not be run during a production deploy - instead, it should be used to pass builds in test deployment environments like continuous integration (CI) or QA (have a look at Jenkins for continuous integration servers, and karma for running tests, perfect for these scenarios). Having a separate QA environment will allow you to privately mirror production and check for bugs encountered post release.
This is obvious, but tests should also be run locally.
Use a continuous integration server to manage your deployments. Common practice is to fail the build if a test failure is encountered, indicating the application is not ready for production release.
You should then take the tagged application (assuming you are using Git tags for versioning your snapshots) matching your CI or QA environment (assuming all tests pass), and use that as a metric for signing off on your production application, indicating it is ready for production release and deployment.
You should not be running tests during production deploys, rather running them in environments preceding production.
This of course is an over simplification and your situation may vary depending on your applications needs.
Upvotes: 0
Reputation: 9541
You would never have unit tests near production. Units of code should not be environment dependent.
If a unit of code works one way in a local development environment, it should work the same way in a test or production environment.
Upvotes: 0
Reputation: 5715
should unit test code be deployed into production?
No.
Are they stripping unit test to save on bandwidth?
Yes. There's also no benefit of deploying tests to production.
Is there a particular way to handle the unit tests so that they don’t go into production?
Write them in separate files. Don't deploy those files. A clear naming convention helps, e.g. all test files end in .spec.js
.
Is there a tool available that will enable me to unit test locally, but strip it for production?
Yes. You can use Grunt, Gulp.js, Make, etc. to skip test files when preparing a distribution package.
Upvotes: 4