Skip to content

Commit 34480f3

Browse files
committed
loadContext tests
1 parent d1c3ad6 commit 34480f3

10 files changed

+122
-6
lines changed

test/context/addOne.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function addOne(x) {
2+
return x + 1;
3+
}

test/context/data.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"a": 42
3+
}

test/context/subtractOne.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function subtractOne(x) {
2+
return x - 1;
3+
}

test/load-context.spec.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
var chai = require('chai');
2+
var chaiAsPromised = require("chai-as-promised");
3+
4+
chai.use(chaiAsPromised);
5+
var expect = chai.expect;
6+
7+
var getRunner = require('./utils/runner').getRunner;
8+
9+
describe('loadContext', function() {
10+
11+
it('should load another files context', function() {
12+
var files = [
13+
['load-context-01.js']
14+
];
15+
var run = getRunner(files);
16+
var expected = {
17+
pass: true,
18+
taskPosition: 1,
19+
failedAtFile: null,
20+
msg: 'load-context-01 should pass'
21+
};
22+
23+
return expect(run).to.eventually.deep.equal(expected);
24+
});
25+
26+
it('should load multiple file contexts', function() {
27+
var files = [
28+
['load-context-02.js']
29+
];
30+
var run = getRunner(files);
31+
var expected = {
32+
pass: true,
33+
taskPosition: 1,
34+
failedAtFile: null,
35+
msg: 'load-context-02 should pass'
36+
};
37+
38+
return expect(run).to.eventually.deep.equal(expected);
39+
});
40+
41+
it('should allowing requiring files', function() {
42+
var files = [
43+
['load-context-03.js']
44+
];
45+
var run = getRunner(files);
46+
var expected = {
47+
pass: true,
48+
taskPosition: 1,
49+
failedAtFile: null,
50+
msg: 'load-context-03 should pass'
51+
};
52+
53+
return expect(run).to.eventually.deep.equal(expected);
54+
});
55+
56+
});

test/runner.spec.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
var chai = require('chai');
22
var chaiAsPromised = require("chai-as-promised");
3-
var spies = require('chai-spies');
43

5-
chai.use(spies);
64
chai.use(chaiAsPromised);
75
var expect = chai.expect;
86

test/tests/load-context-01.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var expect = require('chai').expect;
2+
var loadContext = require('../utils/loadContext').default;
3+
loadContext('./context/addOne.js');
4+
5+
describe('load-context-01', function () {
6+
7+
it('should pass', function () {
8+
expect(addOne(41)).to.equal(42);
9+
});
10+
11+
});

test/tests/load-context-02.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var expect = require('chai').expect;
2+
var path = require('path');
3+
var loadContext = require('../utils/loadContext').default;
4+
loadContext('./context/addOne.js');
5+
loadContext('./context/subtractOne.js');
6+
7+
describe('load-context-02', function () {
8+
9+
it('should pass', function () {
10+
expect(addOne(41)).to.equal(42);
11+
expect(subtractOne(43)).to.equal(42);
12+
});
13+
14+
});

test/tests/load-context-03.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var expect = require('chai').expect;
2+
var path = require('path');
3+
var loadContext = require('../utils/loadContext').default;
4+
loadContext('./context/addOne.js');
5+
loadContext('./context/subtractOne.js');
6+
var a = JSON.parse(JSON.stringify(require('../context/data.json'))).a;
7+
8+
describe('load-context-03', function () {
9+
10+
it('should pass', function () {
11+
expect(addOne(a)).to.equal(43);
12+
expect(subtractOne(a)).to.equal(41);
13+
});
14+
15+
});

test/utils/loadContext.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
"use strict";
12
var vm = require('vm');
23
var fs = require('fs');
3-
module.exports = function loadContext(pathToContext) {
4-
var context = fs.readFileSync(pathToContext, 'utf8');
5-
vm.runInThisContext(context);
6-
};
4+
var path = require('path');
5+
function loadContext(pathToContext) {
6+
var absPath = path.join(process.env.DIR, pathToContext);
7+
var context = fs.readFileSync(absPath, 'utf8');
8+
vm.runInThisContext(context);
9+
}
10+
Object.defineProperty(exports, "__esModule", { value: true });
11+
exports.default = loadContext;

test/utils/loadContext.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var vm = require('vm');
2+
var fs = require('fs');
3+
var path = require('path');
4+
export default function loadContext(pathToContext) {
5+
var absPath = path.join(process.env.DIR, pathToContext);
6+
var context = fs.readFileSync(absPath, 'utf8');
7+
vm.runInThisContext(context);
8+
}

0 commit comments

Comments
 (0)