This example code is based on my "Agile Engineering for the Web" talk, first presented at Øredev in Malmö Sweden on 4 Nov 2015. The talk demonstrates test-driven development of front-end JavaScript and CSS. You can see it online here:
The Quixote portion starts at 21:50.
There have been some changes to the example since the video was recorded. The biggest change is to Quixote's assertion API. The assertions in the video look like this (see 27:18):
figure.assert({
left: frame.body().left
});
This assertion checks that the left edge of the 'figure' element is the same as the left edge of the page's body element.
The current version of Quixote provides a more natural API. The same assertion now looks like this:
figure.left.should.equal(frame.body().left);
The code in this example uses this style.
This code demonstrates CSS and JavaScript tests. It uses:
- Karma for cross-browser testing.
- Mocha for running tests.
- Chai for assertions.
- Quixote for testing CSS.
The sample application uses Nicole Sullivan's media object to display an icon with some text. Clicking the icon causes the text to appear and disappear.
Important files:
-
src/_media_css_test.js
: CSS tests -
src/screen.css
: CSS code -
build/config/karma.conf.js
: Karma configuration. Look for the// QUIXOTE
comment to see how to make Karma serve CSS files.
Before running the tests:
- Install Node.js.
- Install Quixote:
npm install quixote
- Change to the example directory:
cd node_modules/quixote/example
To run the tests:
- Start the Karma server:
./jake.sh karma
(Unix/Mac) orjake karma
(Windows) - Open
http://localhost:9876
in one or more browsers. - Run
./jake.sh loose=true
(Unix/Mac) orjake loose=true
(Windows) every time you want to build and test. Alternatively, use./watch.sh loose=true
(Unix/Mac) orwatch loose=true
(Windows) to automatically runjake
whenever files change.
Remove the loose=true
parameter for strict Node and browser version checking.
To run the app:
- Run
./jake.sh run
(Unix/Mac) orjake run
(Windows). - Open
http://localhost:8080
in a browser. - Click the coffee cup icon to see the text appear and disappear.
This repository consists of the following directories:
build
: Build automation.build/config
: Build configuration.build/scripts
: Build scripts. Don't run them directly.build/util
: Modules used by the build scripts.
node_modules
: npm dependencies (used by the build).src
: Front-end code.vendor
: Client code dependencies.
In the repository root, you'll find the following scripts. For each script, there's a .sh
version for Unix and Mac and a .bat
version for Windows:
jake
: Build and test automation.watch
: Automatically runsjake
when any files change. Any arguments are passed through to jake.
For all these scripts, use -T
to see the available build targets and their documentation. If no target is provided, the script will run default
. Use --help
for additional options.
The scripts have these additional options:
loose=true
: Disable strict browser and version checks.capture=Firefox,Safari,etc
: Automatically launch, use, and quit the requested browsers. You can use this instead of running./jake.sh karma
and manually starting the browsers yourself. Note that the browser name is case-sensitive. The Firefox launcher is included; if you need additional launchers, you'll need to install them; e.g.,npm install karma-safari-launcher
.
MIT License. See LICENSE.TXT
.