2015-03-01 04:54:58 +01:00
## How does Bootstrap's test suite work?
2018-09-14 16:49:59 +02:00
Bootstrap uses [QUnit ](https://qunitjs.com/ ) and [Sinon ](https://sinonjs.org/ ). Each plugin has a file dedicated to its tests in `unit/<plugin-name>.js` .
2015-03-01 04:54:58 +01:00
* `unit/` contains the unit test files for each Bootstrap plugin.
2018-03-09 23:17:57 +01:00
* `vendor/` contains third-party testing-related code (QUnit, jQuery and Sinon).
2015-03-01 04:54:58 +01:00
* `visual/` contains "visual" tests which are run interactively in real browsers and require manual verification by humans.
2018-04-01 09:35:51 +02:00
To run the unit test suite via [Karma ](https://karma-runner.github.io/ ), run `npm run js-test` .
2015-03-01 04:54:58 +01:00
To run the unit test suite via a real web browser, open `index.html` in the browser.
## How do I add a new unit test?
1. Locate and open the file dedicated to the plugin which you need to add tests to (`unit/< plugin-name > .js`).
2016-10-03 18:55:59 +02:00
2. Review the [QUnit API Documentation ](https://api.qunitjs.com/ ) and use the existing tests as references for how to structure your new tests.
2015-03-01 04:54:58 +01:00
3. Write the necessary unit test(s) for the new or revised functionality.
2017-04-20 12:33:51 +02:00
4. Run `npm run js-test` to see the results of your newly-added test(s).
2015-03-01 04:54:58 +01:00
**Note:** Your new unit tests should fail before your changes are applied to the plugin, and should pass after your changes are applied to the plugin.
## What should a unit test look like?
* Each test should have a unique name clearly stating what unit is being tested.
* Each test should test only one unit per test, although one test can include several assertions. Create multiple tests for multiple units of functionality.
2017-10-07 00:04:21 +02:00
* Each test should begin with [`assert.expect` ](https://api.qunitjs.com/assert/expect/ ) to ensure that the expected assertions are run.
2015-03-01 04:54:58 +01:00
* Each test should follow the project's [JavaScript Code Guidelines ](https://github.com/twbs/bootstrap/blob/master/CONTRIBUTING.md#js )
2018-03-06 06:35:28 +01:00
## Code coverage
Currently we're aiming for at least 80% test coverage for our code. To ensure your changes meet or exceed this limit, run `npm run js-compile && npm run js-test` and open the file in `js/coverage/lcov-report/index.html` to see the code coverage for each plugin. See more details when you select a plugin and ensure your change is fully covered by unit tests.
2015-03-01 04:54:58 +01:00
### Example tests
2018-03-06 06:35:28 +01:00
```js
2015-03-01 04:54:58 +01:00
// Synchronous test
QUnit.test('should describe the unit being tested', function (assert) {
assert.expect(1)
2018-03-06 06:35:28 +01:00
var templateHTML = '< div class = "alert alert-danger fade show" > ' +
'< a class = "close" href = "#" data-dismiss = "alert" > × < / a > ' +
'< p > < strong > Template necessary for the test.< / p > ' +
'< / div > '
2015-03-01 04:54:58 +01:00
var $alert = $(templateHTML).appendTo('#qunit-fixture').bootstrapAlert()
2017-10-07 00:04:21 +02:00
$alert.find('.close').trigger('click')
2015-03-01 04:54:58 +01:00
// Make assertion
2017-10-07 00:04:21 +02:00
assert.strictEqual($alert.hasClass('show'), false, 'remove .show class on .close click')
2015-03-01 04:54:58 +01:00
})
// Asynchronous test
QUnit.test('should describe the unit being tested', function (assert) {
2018-03-09 23:17:57 +01:00
assert.expect(2)
2015-03-01 04:54:58 +01:00
var done = assert.async()
2018-03-09 23:17:57 +01:00
var $tooltip = $('< div title = "tooltip title" > < / div > ').bootstrapTooltip()
var tooltipInstance = $tooltip.data('bs.tooltip')
var spyShow = sinon.spy(tooltipInstance, 'show')
$tooltip.appendTo('#qunit-fixture')
2015-03-01 04:54:58 +01:00
.on('shown.bs.tooltip', function () {
assert.ok(true, '"shown" event was fired after calling "show"')
2018-03-09 23:17:57 +01:00
assert.ok(spyShow.called, 'show called')
2015-03-01 04:54:58 +01:00
done()
})
.bootstrapTooltip('show')
})
```