2016-09-14 15 views
-1

私は失敗した単体テストを持っています。私はカスタムジャスミンレポータを作成しています。私はそれが提供するスタックトレースからファイルの名前を取得する必要があります。スタックトレースからファイル名を取得

Verify the most recent consumer review is showing all information. (0.683 sec) - Expected true to be false, 'test title'. at Object.<anonymous> (/Users/xyz/Documents/tests/somefolder/xyz.test.js:25:72) at /Users/xyz/Documents/node_modules/jasminewd2/index.js:96:23 at new Promise (/Users/xyz/Documentsc/node_modules/selenium-webdriver/lib/promise.js:1043:7.....

真上からファイルxyz.test.jsの名前を取得するための最良の方法は何ですか?

+0

の可能性のある重複した[私は例外を投げるとき、私はJavascriptのスタックトレースを取得できますか?]( http://stackoverflow.com/questions/591857/how-can-i-get-a-javascript-stack-trace-when-i-throw-an-exception) –

答えて

0

トレースバックを文字列に格納されている、とあなたは(例外は、すべてのタイプのために動作しない場合があります)正規表現を使ってファイル名を抽出したいされている場合:

var traceback = "Verify the most recent consumer review is showing all information. (0.683 sec) \ 
 
     - Expected true to be false, 'test title'. \ 
 
      at Object.<anonymous> (/Users/xyz/Documents/tests/somefolder/xyz.test.js:25:72) \ 
 
      at /Users/xyz/Documents/node_modules/jasminewd2/index.js:96:23 \ 
 
      at new Promise (/Users/xyz/Documentsc/node_modules/selenium-webdriver/lib/promise.js:1043:7....." 
 
      
 
console.log(/\(.*\/([^\/]*.js).*\)/.exec(traceback)[1])

\(.*/([^/]*.js).*\) 

Regular expression visualization

Debuggex Demo

1

おそらく、このための正規表現を使用することができますが、あなたは少しより多くの機能が必要な場合は、stacktrace-parserに便利です:

const parse = require('stacktrace-parser').parse; 
const path = require('path'); 

let trace = ` 
     - Expected true to be false, 'test title'. 
      at Object.<anonymous> (/Users/xyz/Documents/tests/somefolder/xyz.test.js:25:72) 
      at /Users/xyz/Documents/node_modules/jasminewd2/index.js:96:23 
      at new Promise (/Users/xyz/Documentsc/node_modules/selenium-webdriver/lib/promise.js:1043:7 
`; 

console.log(path.basename(parse(trace)[0].file)) 
// outputs: xyz.test.js 
関連する問題