@travetto/test
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -40,3 +40,3 @@ #!/usr/bin/env node | ||
const { Runner } = require('../src/exec/runner'); | ||
new Runner().runWorker(data, done); | ||
new Runner().runWorker(data).then(done, done); | ||
}); |
@@ -28,3 +28,3 @@ { | ||
}, | ||
"version": "0.0.8" | ||
"version": "0.0.9" | ||
} |
@@ -8,3 +8,15 @@ module.exports = { | ||
} else if (data.type === 'run') { | ||
run(data, e => process.send({ type: 'runComplete', error: e })); | ||
run(data, e => { | ||
let error = undefined; | ||
if (e) { | ||
error = {}; | ||
for (let k of Object.keys(e)) { | ||
error[k] = e[k]; | ||
} | ||
error.message = e.message; | ||
error.stack = e.stack; | ||
error.name = e.name; | ||
} | ||
process.send({ type: 'runComplete', error }); | ||
}); | ||
} | ||
@@ -11,0 +23,0 @@ }); |
@@ -48,2 +48,3 @@ import * as os from 'os'; | ||
let position = 0; | ||
let errors: Error[] = []; | ||
@@ -56,4 +57,15 @@ while (position < inputs.length) { | ||
agent.completion = new Promise<Agent>((resolve, reject) => { | ||
agent.listenOnce('runComplete', (e: any) => { | ||
!e.error ? resolve(agent) : reject(e.error); | ||
agent.listenOnce('runComplete', ({ error: e }) => { | ||
if (e) { | ||
let err: any = new Error(); | ||
for (let k of Object.keys(e)) { | ||
err[k] = e[k]; | ||
} | ||
err.message = e.message; | ||
err.stack = e.stack; | ||
err.name = e.name; | ||
errors.push(err); | ||
} | ||
resolve(agent); | ||
}); | ||
@@ -74,2 +86,4 @@ }); | ||
await Promise.all(Array.from(this.pendingAgents).map(x => x.completion)); | ||
return errors; | ||
} | ||
@@ -76,0 +90,0 @@ |
@@ -20,8 +20,16 @@ import { AppEnv } from '@travetto/base'; | ||
static readFilePosition(err: Error) { | ||
let base = process.cwd() + '/test/'; | ||
let sub: string = err.stack!; | ||
let lines = sub.split('\n').filter(x => x.includes(base)); | ||
static readFilePosition(err: Error, filename: string) { | ||
let base = process.cwd(); | ||
let lines = (err.stack || new Error().stack).split('\n').filter(x => !x.includes('/node_modules/') && x.includes(base)); | ||
let best = lines.filter(x => x.includes(filename))[0]; | ||
let [fn, path] = lines[0].trim().split(/\s+/g).slice(1); | ||
if (!best) { | ||
best = lines.filter(x => x.includes(`${base}/test`))[0]; | ||
} | ||
if (!best) { | ||
return { file: filename, line: 1 }; | ||
} | ||
let [fn, path] = best.trim().split(/\s+/g).slice(1); | ||
let [file, lineNo, col] = path.replace(/[()]/g, '').split(':') | ||
@@ -31,3 +39,5 @@ | ||
return { file, line: parseInt(lineNo, 10) }; | ||
let res = { file, line: parseInt(lineNo, 10) }; | ||
return res; | ||
} | ||
@@ -39,4 +49,4 @@ | ||
static check(text: string, name: string, ...args: any[]) { | ||
let { file, line } = this.readFilePosition(new Error()); | ||
static check(filename: string, text: string, name: string, ...args: any[]) { | ||
let { file, line } = this.readFilePosition(new Error(), filename.replace(/[.][tj]s$/, '')); | ||
@@ -43,0 +53,0 @@ let assertion: Assertion = { file, line, text, operator: ASSERT_FN_OPERATOR[name] }; |
@@ -10,2 +10,4 @@ import { AllSuitesResult, TestResult, SuiteResult } from '../../model'; | ||
errors: Error[] = []; | ||
allSuites: AllSuitesResult = { | ||
@@ -12,0 +14,0 @@ success: 0, |
@@ -71,3 +71,9 @@ import * as yaml from 'js-yaml'; | ||
this.log(`1..${collector.allSuites.total}`); | ||
if (collector.errors.length) { | ||
this.log('---\n'); | ||
for (let err of collector.errors) { | ||
this.log(err.toString()); | ||
} | ||
} | ||
} | ||
} |
@@ -32,3 +32,3 @@ import * as minimist from 'minimist'; | ||
async runWorker(data: { file: string }, done: (err?: any) => void) { | ||
async runWorker(data: { file: string }) { | ||
if (!process.send) { | ||
@@ -38,10 +38,5 @@ return; | ||
try { | ||
await TestUtil.executeFile(data.file, { | ||
emit: process.send.bind(process) | ||
}); | ||
done(); | ||
} catch (e) { | ||
done(e); | ||
} | ||
await TestUtil.executeFile(data.file, { | ||
emit: process.send.bind(process) | ||
}); | ||
} | ||
@@ -76,3 +71,3 @@ | ||
await agentPool.process(files, async (file, run, agent) => { | ||
collector.errors = await agentPool.process(files, async (file, run, agent) => { | ||
if (agent) { | ||
@@ -79,0 +74,0 @@ for (let l of listeners) { |
@@ -105,3 +105,3 @@ import * as fs from 'fs'; | ||
if (!(err instanceof assert.AssertionError)) { | ||
let { file, line } = AssertUtil.readFilePosition(err); | ||
let { file, line } = AssertUtil.readFilePosition(err, test.file); | ||
const assertion: Assertion = { file, line, operator: 'throws', text: '', error: err, message: `Error thrown: ${err.message}` }; | ||
@@ -121,3 +121,3 @@ result.assertions.push(assertion); | ||
} catch (error) { | ||
let { line, file } = AssertUtil.readFilePosition(error); | ||
let { line, file } = AssertUtil.readFilePosition(error, suite.class.__filename); | ||
result.tests.push({ | ||
@@ -139,2 +139,39 @@ status: 'fail', | ||
static async stubSuiteFailure(suite: SuiteConfig, e: Error, emitter?: TestEmitter) { | ||
if (!emitter) { | ||
return; | ||
} | ||
let test = { | ||
line: suite.line, | ||
lineEnd: suite.lineEnd, | ||
suiteName: suite.name, | ||
status: 'fail', | ||
method: 'all', | ||
error: e, | ||
output: { error: e.stack }, | ||
assertions: [{ | ||
error: e, | ||
line: suite.line, | ||
message: e.message, | ||
file: suite.class.__filename, | ||
operator: 'throws', | ||
text: '(init)' | ||
}], | ||
class: suite.class.name, | ||
description: '', | ||
file: suite.class.__filename | ||
} as TestResult; | ||
emitter.emit({ phase: 'after', type: 'test', test }); | ||
emitter.emit({ | ||
phase: 'after', type: 'suite', suite: { | ||
success: 0, | ||
fail: 1, | ||
skip: 0, | ||
total: 1 | ||
} as SuiteResult | ||
}); | ||
} | ||
static async executeSuite(suite: SuiteConfig, emitter?: TestEmitter) { | ||
@@ -190,13 +227,3 @@ let result: SuiteResult = { | ||
static async executeFile(file: string, emitter?: TestEmitter) { | ||
try { | ||
require(`${process.cwd()}/${file}`); | ||
} catch (e) { | ||
if (emitter) { | ||
emitter.emit({ | ||
phase: 'before', type: 'suite', suite: {} as SuiteConfig | ||
}); | ||
} | ||
return; | ||
} | ||
require(`${process.cwd()}/${file}`); | ||
await TestRegistry.init(); | ||
@@ -209,13 +236,21 @@ | ||
if (emitter) { | ||
emitter.emit({ phase: 'before', type: 'suite', suite }); | ||
} | ||
try { | ||
if (emitter) { | ||
emitter.emit({ phase: 'before', type: 'suite', suite }); | ||
} | ||
let result = await this.executeSuite(suite, emitter); | ||
let result = await this.executeSuite(suite, emitter); | ||
if (emitter) { | ||
emitter.emit({ phase: 'after', type: 'suite', suite: result }); | ||
if (emitter) { | ||
emitter.emit({ phase: 'after', type: 'suite', suite: result }); | ||
} | ||
} catch (e) { | ||
if (emitter) { | ||
this.stubSuiteFailure(suite, e, emitter); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
@@ -46,2 +46,3 @@ import * as ts from 'typescript'; | ||
let check = ts.createCall(state.assertCheck, undefined, ts.createNodeArray([ | ||
ts.createLiteral('__filename'), | ||
ts.createLiteral(TransformUtil.getPrimaryArgument(node)!.getText()), | ||
@@ -48,0 +49,0 @@ ts.createLiteral(name), |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
257577
1148