Skip to content

Commit 32fd3f1

Browse files
testing (docs) preserve stack trace
1 parent 6a63948 commit 32fd3f1

1 file changed

Lines changed: 27 additions & 13 deletions

File tree

test/docs.js

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var fs = require('fs');
2121
var gcloud = require('../');
2222
var glob = require('glob');
2323
var mitm = require('mitm');
24+
var prop = require('propprop');
2425
var vm = require('vm');
2526

2627
var util = require('../lib/common/util.js');
@@ -33,8 +34,23 @@ function runCodeInSandbox(code, sandbox) {
3334
timeout: 1000
3435
});
3536
} catch(err) {
37+
var lineCol = err.stack.match('assert-code\.vm:(.+):(.+)');
38+
lineCol.line = lineCol[1];
39+
lineCol.col = lineCol[2];
40+
41+
var lines = code.split('\n')
42+
.filter(function (line, index) {
43+
if (index < lineCol.line) {
44+
return line;
45+
}
46+
})
47+
.join('\n');
48+
49+
err.message = '\n' + lines + '\n\n' + err.message;
50+
51+
throw err;
3652
// rethrow the error with code for context and resolving issues faster.
37-
throw new Error('\n' + code + '\n\n' + err.message);
53+
// throw new Error('\n' + code + '\n\n' + err.message);
3854
}
3955
}
4056

@@ -96,20 +112,18 @@ describe('documentation', function() {
96112
global: global
97113
};
98114

99-
var examples = fileDocBlocks.methods.map(function(method) {
100-
return method.examples.map(function(example) {
101-
return example.code;
102-
}).join('\n');
103-
});
115+
fileDocBlocks.methods.forEach(function(method) {
116+
var code = method.examples.map(prop('code')).join('\n')
117+
.replace(/require\(\'gcloud\'\)/g, 'require(\'..\/\')')
118+
.replace(/require\(\'gcloud/g, 'require(\'..');
104119

105-
var code = examples
106-
.join('\n')
107-
.replace(/require\(\'gcloud\'\)/g, 'require(\'..\/\')')
108-
.replace(/require\(\'gcloud/g, 'require(\'..');
120+
var displayName = filename
121+
.replace('docs/json/master/', '')
122+
.replace('.json', '.js');
109123

110-
it('should run tests for ' + filename + ' without errors', function() {
111-
this.timeout(5000);
112-
assert.doesNotThrow(runCodeInSandbox.bind(null, code, sandbox));
124+
it('should run tests for ' + displayName + '#' + method.id + ' without errors', function() {
125+
assert.doesNotThrow(runCodeInSandbox.bind(null, code, sandbox));
126+
});
113127
});
114128
});
115129
});

0 commit comments

Comments
 (0)