
Testing Patterns
- Unit: Small tests covering individual functions.
- Integration: Integrated modules(combined functionality).
- Functional: Focused on end result of larger actions.
- Acceptance: Typically client-side, end-to-end testing.
JavaScript Testing Frameworks
- jasmine
- karma
- expresso
- mocha
- node-tap
- intern
- node-unit
- vows
- node-qunit
- More...
MOCHA
MOCHA Setup
-install mocha as a development dependency for project
-Update package.json
Asynchronous Testing
Basic Tests and Assertions
assert.equal: check more or less equal
assert.strictEqual: exactly equal in type and value
Running the Test
Assertion Methods
- equal: Asserts non-strict equality (==) of actual and expected.
- strictEqual: Asserts strict equality (===) of actual and expected.
- deepEqual: Asserts that actual is deeply equal to expected.
- isOk: Asserts that object is truthy.
- isNull: Asserts that value is null.
- isTrue: Asserts that value is true.
- fail: Throw a failure. Node.js assert module-compatible.
- match: Asserts that value matches the regular expression regexp.
- throws: Asserts that function will throw an error that is an instance of constructor, or alternately that it will throw an error with message matching regexp.
Testing Tips
- Keep your modules small.
- Be sure to test success AND error cases.
- Test expected AND unexpected data.
- Don't forget that modules are cached.
Testing with Mocks
- Use mocha to test javascript code. Invoke the command mocha to start the tests.
- Use chai for assertions.
- If we find any problematic code then use sinon to spy or stub them to see/ control the behaviour of the functions.
- Use nock to mock servers to prevent making any http calls to the outside environment.
- Use rewire to set/ get variables/external dependencies in the javascript file.
- Use istanbul to calculate the code coverage done by these unit tests.
No comments:
Post a Comment