You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+8-92
Original file line number
Diff line number
Diff line change
@@ -29,114 +29,30 @@ it('isn\'t a function')
29
29
30
30
### Loaders
31
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.
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.
33
33
34
34
```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 */
38
37
```
39
38
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
-
functionloadJS(pathToContext) {
56
-
var absPath =path.join(process.env.DIR, pathToContext);
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
-
functionloadBabel(pathToContext) {
79
-
var absPath =path.join(process.env.DIR, pathToContext);
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 = {...}`.
93
40
94
41
```js
95
-
var ts =require('typescript'),
96
-
options = {
97
-
module:'commonjs',
98
-
target:'ES5'
99
-
};
100
-
functionloadTS(pathToContext) {
101
-
var absPath =path.join(process.env.DIR, pathToContext);
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.
0 commit comments