Skip to content

Commit 51e3297

Browse files
committed
further docs
1 parent 85dc484 commit 51e3297

7 files changed

+268
-30
lines changed

_posts/2016-01-05-tests.md

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,54 @@ file: 2016-01-05-tests.md
77

88
Unit tests are used to determine:
99

10-
* if a task passes
11-
* which feedback a user gets when a test fails
10+
* if a task passes or fails
11+
* feedback a user gets when a test fails
1212

13-
### Loading the User File
13+
Tests are loaded in their order from the tutorial markdown files, and concat together into one temporary file. This means that any import or require statements placed at the top of the first file will be shared by all tests for that page.
1414

15-
### Data Files
15+
Tests should indicate which task they are, in order to quickly determine which task index has failed. For the *mocha-coderoad* test runner, this is as easy as adding the task number to describe block.
1616

17-
### Test Examples
17+
describe('01 first task', function () { ... });
18+
describe('04 fourth task', function () { ... });
1819

19-
* exists
20-
* type
21-
* params
22-
* equals
23-
* deep equals
24-
* regex
25-
* property
26-
* spies
2720

28-
### Dynamic Tests
21+
### Test Statements
2922

30-
* task position
23+
It makes sense to write test statements using 'should', 'must' or negative statements. Remember, the failing test message will be delivered as feedback to the user.
3124

32-
### Future Testing Tool
25+
it('should be a function')
26+
it('must be a function')
27+
it('isn\'t a function')
28+
29+
30+
### Loaders
31+
32+
Use a **loader** to run the user saved file in the context of your file. Think of a loader as a way to place the file your testing inside of your test file. Import your loader and run it on a specific user file.
33+
34+
var loadJS = require('path/to/loadJS').default;
35+
loadJS('user-file.js');
36+
// adds file contents here
37+
38+
You'll have to roll your own loader to fit your project, but there are example [loaders](#loaders) included later in the docs.
39+
40+
*Note: When using spies, stubs or mocks, initiate them above your loader call.*
41+
42+
43+
44+
### Loading Data Files
45+
46+
Data can be loaded in the user's file by setting it as a global within the test. Remember, the top of the test file (above the loader), acts as the top of the user's page.
47+
48+
Although bad practice, it can be easiest to set data to the global scope.
49+
50+
if (!global.data) {
51+
global.data = 43;
52+
}
53+
54+
if (!global.data2) {
55+
global.data2 = JSON.parse(JSON.stringify(require('./data2.json')));
56+
}
57+
58+
Users can access global data by name in their file.
59+
60+
var secret = data - 1;

_posts/2016-01-06-loaders.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
layout: docs
3+
title: Loaders
4+
id: loaders
5+
file: 2016-01-06-loading.md
6+
---
7+
8+
Tutorials may be written in different programming languages or for different compilers, so there isn't yet a standard way to load data from user created files. Instead, you'll have to load your own solution into your tutorial and link to them from your test file. Rolling your own solution allows you to load data in a way that fits your project.
9+
10+
These snippets should help:
11+
12+
13+
#### loadJS (JavaScript)
14+
15+
var vm = require('vm'),
16+
fs = require('fs'),
17+
path = require('path');
18+
function loadJS(pathToContext) {
19+
var absPath = path.join(process.env.DIR, pathToContext);
20+
var code = fs.readFileSync(absPath, 'utf8');
21+
vm.runInThisContext(code);
22+
}
23+
Object.defineProperty(exports, "__esModule", { value: true });
24+
exports.default = loadJS;
25+
26+
#### loadBabel (Babel)
27+
28+
See the [Babel documentation](https://babeljs.io/docs/setup/#node) on how to install Babel & the selected [presets](http://babeljs.io/docs/plugins/#presets). Set the [options](http://babeljs.io/docs/usage/options/) you need.
29+
30+
`> npm i -s babel-core` + presets
31+
32+
var vm = require('vm'),
33+
fs = require('fs'),
34+
path = require('path'),
35+
babel = require('babel'),
36+
options = {
37+
presets: ['es2015']
38+
};
39+
function loadBabel(pathToContext) {
40+
var absPath = path.join(process.env.DIR, pathToContext);
41+
var code = fs.readFileSync(absPath, 'utf8');
42+
var js = babel.transform(code, options);
43+
vm.runInThisContext(js);
44+
}
45+
Object.defineProperty(exports, "__esModule", { value: true });
46+
exports.default = loadBabel;
47+
48+
#### loadTS (TypeScript)
49+
50+
To use TypeScript, install the package as a tutorial dependency.
51+
52+
`> npm i -s typescript`
53+
54+
var ts = require('typescript'),
55+
options = {
56+
module: 'commonjs',
57+
target: 'ES5'
58+
};
59+
function loadTS(pathToContext) {
60+
var absPath = path.join(process.env.DIR, pathToContext);
61+
var code = fs.readFileSync(absPath, 'utf8');
62+
var js = ts.transpile(code, options);
63+
vm.runInThisContext(js);
64+
}
65+
Object.defineProperty(exports, "__esModule", { value: true });
66+
exports.default = loadTS;

_posts/2016-01-07-publish.md

Lines changed: 0 additions & 13 deletions
This file was deleted.

_posts/2016-01-07-test-examples.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
layout: docs
3+
title: Test Examples
4+
id: test-examples
5+
file: 2016-01-07-test-examples.md
6+
---
7+
8+
Here are examples using *mocha* with *chai*'s *expect*. See the [docs](http://chaijs.com/api/bdd/).
9+
10+
##### exists
11+
12+
it('doesn\'t exist', function() {
13+
expect(target).to.not.be.undefined;
14+
});
15+
16+
##### type
17+
18+
it('should be a function', function() {
19+
expect(target).to.be.a('function');
20+
});
21+
22+
##### function params
23+
24+
it('should have two parameters', function() {
25+
expect(target).to.have.length(2);
26+
});
27+
28+
##### function returns
29+
30+
it('should add one to the number', function () {
31+
expect(addOne(1)).to.equal(2);
32+
});
33+
34+
##### equals
35+
36+
it('should be 42', function () {
37+
expect(target).to.equal(42);
38+
});
39+
40+
##### deep equals (with objects or arrays)
41+
42+
it('should be {a: 42}', function () {
43+
expect(target).to.deep.equal({a: 42});
44+
});
45+
46+
##### regex
47+
48+
it('should access the property "prop"', function () {
49+
var regex1 = /\.prop/; // dot notation
50+
var regex2 = /\[["']prop["']\]/; // bracket notation
51+
var string = target.toString();
52+
var result = !!string.match(regex1) && !!string.match(regex2);
53+
expect(result).to.be.true;
54+
});
55+
56+
##### spies
57+
58+
You can use [*sinon*](http://sinonjs.org/docs/) or [*chai-spies*](https://github.com/chaijs/chai-spies) to create a spy. See an example below:
59+
60+
`> npm i -s chai-spies`
61+
62+
var chai = require('chai');
63+
var spies = require('chai-spies');
64+
var expect = chai.expect;
65+
chai.use(spies);
66+
67+
var spy = chai.spy.on(console, 'log');
68+
loadJS('04-forEach.js');
69+
70+
it('should write "hello world" to the console', function () {
71+
expect(spy).to.have.been.called.with('hello world');
72+
});
73+
74+
75+
### Dynamic Tests
76+
77+
There are situations where you might want to change data based on the current task. In this case, be careful, as all earlier tests must continue to pass.
78+
79+
Some variables are passed into the test runner through the node environment *process.env*.
80+
81+
See an example of dynamic data based on the task position below:
82+
83+
var data = [1, 2, 3];
84+
85+
if (process.env.TASK_POSITION === '4') {
86+
data = [1, 2, 3, 4];
87+
}
88+
89+
Tests can also change based on the task position.
90+
91+
if (process.env.TASK_POSITION !== '4') {
92+
it('should do this', function () { ... });
93+
} else {
94+
it('should to that', function () { ... });
95+
}
96+
97+
See a full [example](https://github.com/coderoad/coderoad-functional-school/blob/master/tutorial/1/04/01-forEach.spec.js).
98+
99+
### Test Writing Tool
100+
101+
It's entirely possible to create a simplified testing tool that could make writing tests faster & easier.
102+
103+
The easiest solution would be to use editor snippets to generate a page of tests from a simple configuration object. This does not yet exist.

_posts/2016-01-08-config.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
layout: docs
3+
title: Config
4+
id: config
5+
file: 2016-01-08-config.md
6+
---
7+
coming soon

_posts/2016-01-09-publish.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
layout: docs
3+
title: Publishing
4+
id: publish
5+
file: 2016-01-09-publish.md
6+
---
7+
Make sure your tutorial is ready and user tested before publishing to NPM.
8+
9+
In the future this process will be further simplified by `> coderoad publish [version]`.
10+
11+
### Completing your *package.json*
12+
13+
* "name" - beginning with 'coderoad-'
14+
* "description" - about your tutorial
15+
* "author" or "authors" information
16+
* "keywords" - about your tutorial
17+
* "repository" - if added, *Atom-CodeRoad* can allow edits
18+
* "bugs" - if added, *Atom-CodeRoad* can provide a link to your github issues
19+
* "dependencies" - including your test runner, and test framework tools
20+
* "config" - see [config](#config)
21+
22+
See an [example](https://github.com/coderoad/coderoad-functional-school/blob/master/package.json) of a complete *package.json* file.
23+
24+
### Versioning
25+
26+
Versioning is handled by:
27+
28+
* the *package.json* declared "version"
29+
* the [git tag](https://git-scm.com/book/en/v2/Git-Basics-Tagging)
30+
31+
> git tag 0.1.0
32+
> git push --tags
33+
34+
Try to make sure the two specified versions match.
35+
36+
### Publishing to NPM
37+
38+
Publishing to NPM is easy, which may be one of the reasons why the NPM registry is so crowded with packages. Please ensure your package name is prefixed with `coderoad-` to limit package registry cluttering.
39+
40+
Have an [NPM account](https://www.npmjs.com/signup) before running publish.
41+
42+
> npm publish
43+
44+
45+
### Promoting your Tutorial
46+
47+
As CodeRoad is a new project, there is not yet a place to display tutorials. If you create something for CodeRoad please send an email to [email protected], or tweet at @coderoadapp.

_posts/2016-01-06-testRunner.md renamed to _posts/2016-01-10-testRunner.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
layout: docs
33
title: Test Runners
44
id: test-runner
5-
file: 2016-01-06-testRunner.md
5+
file: 2016-01-10-testRunner.md
66
---
77
coming soon
88

0 commit comments

Comments
 (0)