Skip to content

Commit 8c4fcd7

Browse files
committed
2 parents 61c53f2 + 5958021 commit 8c4fcd7

File tree

1 file changed

+8
-92
lines changed

1 file changed

+8
-92
lines changed

README.md

+8-92
Original file line numberDiff line numberDiff line change
@@ -29,114 +29,30 @@ it('isn\'t a function')
2929

3030
### Loaders
3131

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.
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. Loaders are written inside of comments.
3333

3434
```js
35-
var loadJS = require('path/to/loadJS').default;
36-
loadJS('user-file.js');
37-
// adds file contents here
35+
// load('user-file.js');
36+
/* workspaceDirectory/user-file.js */
3837
```
3938

40-
You'll have to roll your own loader to fit your project, but there are example [loaders](https://coderoad.github.io/docs/#loaders) included later in the docs.
41-
42-
*Note: When using spies, stubs or mocks, initiate them above your loader call.*
43-
44-
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/transpile your files for the test runner. Rolling your own solution allows you to load data in a way that fits your project.
45-
46-
There may be a simpler approach in the future, but for now these snippets should help:
47-
48-
49-
#### loadJS (JavaScript)
50-
51-
```js
52-
var vm = require('vm'),
53-
fs = require('fs'),
54-
path = require('path');
55-
function loadJS(pathToContext) {
56-
var absPath = path.join(process.env.DIR, pathToContext);
57-
var code = fs.readFileSync(absPath, 'utf8');
58-
vm.runInThisContext(code);
59-
}
60-
Object.defineProperty(exports, "__esModule", { value: true });
61-
exports.default = loadJS;
62-
```
63-
64-
#### loadBabel (Babel)
65-
66-
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.
67-
68-
`> npm i -s babel-core` + presets
69-
70-
```js
71-
var vm = require('vm'),
72-
fs = require('fs'),
73-
path = require('path'),
74-
babel = require('babel'),
75-
options = {
76-
presets: ['es2015']
77-
};
78-
function loadBabel(pathToContext) {
79-
var absPath = path.join(process.env.DIR, pathToContext);
80-
var code = fs.readFileSync(absPath, 'utf8');
81-
var js = babel.transform(code, options);
82-
vm.runInThisContext(js);
83-
}
84-
Object.defineProperty(exports, "__esModule", { value: true });
85-
exports.default = loadBabel;
86-
```
87-
88-
#### loadTS (TypeScript)
89-
90-
To use TypeScript, install the package as a tutorial dependency.
91-
92-
`> npm i -s typescript`
39+
When loading files within your tutorial directory, add a second parameter of `true`. This can allow you to low additional data variables or functions, for example: `var data = {...}`.
9340

9441
```js
95-
var ts = require('typescript'),
96-
options = {
97-
module: 'commonjs',
98-
target: 'ES5'
99-
};
100-
function loadTS(pathToContext) {
101-
var absPath = path.join(process.env.DIR, pathToContext);
102-
var code = fs.readFileSync(absPath, 'utf8');
103-
var js = ts.transpile(code, options);
104-
vm.runInThisContext(js);
105-
}
106-
Object.defineProperty(exports, "__esModule", { value: true });
107-
exports.default = loadTS;
42+
// load('data-file.js', true)
43+
/* workspaceDirectory/node_modules/tutorial-name/tutorial/data-file.js */
10844
```
10945

11046

111-
### Loading Data Files
112-
113-
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.
114-
115-
Although bad practice, it can be easiest to set data to the global scope.
116-
117-
```js
118-
if (!global.data) {
119-
global.data = 43;
120-
}
121-
122-
if (!global.data2) {
123-
global.data2 = JSON.parse(JSON.stringify(require('./data2.json')));
124-
}
125-
```
126-
127-
Users can access global data by name in their file.
128-
129-
```js
130-
var secret = data - 1;
131-
```
47+
### Writing Tests
13248

13349
Here are examples using *mocha* with *chai*'s *expect*. See the [docs](http://chaijs.com/api/bdd/).
13450

13551
#### exists
13652

13753
```js
13854
it('doesn\'t exist', function() {
139-
expect(target).to.not.be.undefined;
55+
expect(target).to.be.defined;
14056
});
14157
```
14258

0 commit comments

Comments
 (0)