README
Soda -test
This package is easiest way to write unit-test.
We have gathered all the features you need from various packages, enhance them, and put them all into one, easy to use package.
You can learn to write unit test using soda-test pacakge by going over soda-test documenation or the soda-test videos.
Write your unit-tests using typescript, with classes, decorators and promises.
Use the "spies" and "stubs" of the sinon pacakges, but with soda-test easy to use syntax.
No need to worry about cleanups, soda-test pacakge do this for you.
Your tests are defined as functions in a class, and not as endless nested call-backs.
When writing unit-tests you may encounter the need to access or stub private or hidden function, stub middleware, avoid your code from accessing external packages that may access the web or databases.
Doing that and other cases, with the traditional test packages, may be very difficult, it possible at all.
Soda-test solves those issues the easy way, in some cases with you even knowing about those difficulties, and without the need to change your product code.
The syntax of unit test code with soda-test is easy to write and easy to understand.
Soda-test supports unit-test for frontend as well as for backend, with the same syntax.
Furthermore, soda-test gives you a framework for writing your API test.
Installation
via npm
$ npm install soda-test --save-dev
Usage
In your project write your test files under test folder, or side by side with your project files, name them *.spec.tsIn your test file, define a class with describe decorator, and in it write your test methods with it decorator
use spies and stubs by defining member variables or arguments with decorators.
@describe('demo')
class TestDemoTest {
@stub(User, "findById").returns("dummy-user")
findStub: SinonStub
@it('should get user by id')
GetById(): TR {
const user = getUser({id:123})
expect(user).to.equal("dummy-user")
expect(this.findStub).to.have.been.calledOnce.calledWith(123)
}
}
For full documentation and many sample test, see soda-test wiki
Define API test-cases by using the testCase decorator on a method, and define its steps in it
@testCase("sample case", SampleTestStepsTest)
checkGetApi(step: stepMethod<SampleTestStepsTest>): void {
step("define dummy REST server").StartRestServer({expect: "GET /", return: "dummy"})
step("send get request").SendRequest({method:"GET", url: "/", expectresponse: "dummy"})
step("validate get request").ValidateRequest({method: "GET", url: "/"})
step("stop dummy Rest server").StopRestServer()
}
For more details about API test case see Soda-test test-cases documenation
Goals
- Write Your unit test in a type-script way (e.g. classes, decorators, promises)
- Your test may be run under mocha, jest or Karma
- All relevant packages are included and exported from one place (chai, sinon, super-test, etc...)
- Using sinon library to create spies and stubs
- Define sinon as class member or method argument
- Sinons clean-up are done automatically, don't need to worry about it
- Divide your test method to different contexts in the same test class
- Stub and spy cases in an easy way the difficult cases
- "rewire" is used under the hood. you don't need to worry about it
- "rewire" works also when testing angular (using Jest or Karam) as well as when testing NodeJS projects
- easy access to private methods in a module for testing and stubbing
- can call/stub abstract and/or private methods in class for unit-testing
- Use sinon fake timers to simulate time features without waiting the time during the unit tests
- Also support API tests by using test-cases and test-steps method
- Test steps can be reused in the same test-case or in different test-case with different arguments
Summary
Soda-Test helps you write good unit and API tests with high coverage.
Documentation
Soda-test has full documentation with sample code.To learn using soda-test, you don't need to have previous knowledge of mocha, chai, or jasmine.
You do need knowledge of typescript.
It is recommended to go over the documentation in GitHub wiki, by its order
Following are all the pages available in GitHub wiki:
- Table of Content (Home)
- About Soda Test
- Getting Started with soda-test using Mocha (for testing NodeJS projects)
- Getting started with soda-test using Jest (for testing Angular projects)
- Getting started with soda-test using Karma (for testing Angular projects)
- using the describe and it decorators for building simple unit testing
- Testing Async methods that use Call-backs
- Testing Async methods that return Promises
- Divide your Test Class into Contexts
- Set a test-method as Pending (until it is ready to be used, or if you what to temporary don't run it)
- Use the Control Methods to define what to run before/after each test method, or before/after all tests/context
- Spy a method to find out if it was called, how many times, and with which arguments
- Stub a method to make it do something else, so it will not be really called during your unit-tests
- Run your test code in a Sandbox
- Use Soda-Test Rewire implementation to access non-exported functions/variables
- Import Private method, so you can call it to test it.
- Use Super Test to test express based REST application without doing any real web actions
- You can Create Stub when importing a module to avoid it from doing external actions
- Use sinon Fake-Timers to simulate time advancing without waiting it to pass
- Creatable Class feature allows you create an instance of an abstract or private class, so you can test it
- You can use Aggregation of method during import, so when you stub them during the test it will take effect, even if the function was saved in advanced
- Learn how to use the expect method (originally defined in chai library) for validating values during unit-tests
- Use Global Sinons if you want your spy/stubs to keep running during all context/class without resetting
- Use Soda-Test for doing API testing by using Test Cases
- Use Soda-Test to generate a JSON document about your tests
- Use Soda-Test configuration file to costumize soda-test abilities