2016-09-14 2 views
-1

실패한 유닛 테스트가 있는데, 스택 트레이스에서 파일 이름을 가져와야하는 커스텀 재 스민 리포터를 작성하고 있습니다.stacktrace에서 파일 이름을 얻으십시오.

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

의 사용 가능한 복제 [I 예외를 던질 때 나는 자바 스크립트 스택 추적을 얻을 수 있는가? (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 
관련 문제