Move runifmain in test
parent
a38c1dd2f1
commit
b8d85b036e
1
index.js
1
index.js
|
@ -1,5 +1,4 @@
|
|||
export * from './reporter/index.js'
|
||||
export { runifmain } from './lib/runifmain.js'
|
||||
export { suite } from './lib/suite.js'
|
||||
|
||||
import { metaltest } from './lib/metaltest.js'
|
||||
|
|
|
@ -1,3 +1,7 @@
|
|||
import { fileURLToPath } from 'node:url'
|
||||
import { stacktrace } from './stacktrace.js'
|
||||
import { errorreporter, summaryreporter, endreporter } from '../index.js'
|
||||
|
||||
const metaltest = (title) => {
|
||||
const suite = []
|
||||
const only = []
|
||||
|
@ -27,6 +31,14 @@ const metaltest = (title) => {
|
|||
return { title, success, fail, skip, total: success + fail, testSuccess, testFail }
|
||||
}
|
||||
|
||||
const getAt = (shiftHowMany = 2) => {
|
||||
const err = new Error()
|
||||
const stack = stacktrace(err, { shiftHowMany })
|
||||
const at = stack.stacktraces[0]
|
||||
|
||||
return at
|
||||
}
|
||||
|
||||
const runner = (name, fn) => {
|
||||
suite.push({ name, fn })
|
||||
}
|
||||
|
@ -89,6 +101,16 @@ const metaltest = (title) => {
|
|||
return r
|
||||
}
|
||||
|
||||
runner.runifmain = async () => {
|
||||
const at = getAt()
|
||||
const path = fileURLToPath(at.callsite.getFileName)
|
||||
const [_, script] = process.argv
|
||||
|
||||
if (path !== script) return
|
||||
|
||||
await runner.run(summaryreporter(), errorreporter(), endreporter())
|
||||
}
|
||||
|
||||
return runner
|
||||
}
|
||||
|
||||
|
|
|
@ -24,5 +24,4 @@ test.end('verify', async (stats) => {
|
|||
assert.equal(stats.testFail.length, 0)
|
||||
})
|
||||
|
||||
import { runifmain } from '../index.js'
|
||||
await runifmain(import.meta, test)
|
||||
await test.runifmain()
|
||||
|
|
|
@ -1,25 +0,0 @@
|
|||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
const isMain = (meta) => {
|
||||
const path = fileURLToPath(meta.url)
|
||||
|
||||
return path.includes(process.argv[1])
|
||||
}
|
||||
|
||||
import { errorreporter, summaryreporter, endreporter } from '../index.js'
|
||||
const runifmain = async (meta, fn, ...args) => {
|
||||
if (!isMain(meta)) return
|
||||
|
||||
if (fn.name === 'runner' && fn.run) {
|
||||
if (args.length == 0) args.push(
|
||||
summaryreporter(),
|
||||
errorreporter(),
|
||||
endreporter(),
|
||||
)
|
||||
await fn.run(...args)
|
||||
} else {
|
||||
await fn(...args)
|
||||
}
|
||||
}
|
||||
|
||||
export { runifmain }
|
|
@ -22,5 +22,4 @@ test('Diff', () => {
|
|||
}
|
||||
})
|
||||
|
||||
import { runifmain } from '../index.js'
|
||||
await runifmain(import.meta, test)
|
||||
await test.runifmain()
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
const LINE_RE = /(?<=^\s*)at.*/gm
|
||||
|
||||
const format = (callsite) => {
|
||||
return {
|
||||
getThis: callsite.getThis(),//: Returns the value of this.
|
||||
getTypeName: callsite.getTypeName(),//: Returns the type of this as a string. This is the name of the function stored in the constructor field of this, if available, otherwise the object's [[Class]] internal property.
|
||||
getFunction: callsite.getFunction(),//: Returns the current function.
|
||||
getFunctionName: callsite.getFunctionName(),//: Returns the name of the current function, typically its name property. If a name property is not available an attempt will be made to try to infer a name from the function's context.
|
||||
getMethodName: callsite.getMethodName(),//: Returns the name of the property of this or one of its prototypes that holds the current function.
|
||||
getFileName: callsite.getFileName(),//: If this function was defined in a script returns the name of the script.
|
||||
getLineNumber: callsite.getLineNumber(),//: If this function was defined in a script returns the current line number.
|
||||
getColumnNumber: callsite.getColumnNumber(),//: If this function was defined in a script returns the current column number
|
||||
getEvalOrigin: callsite.getEvalOrigin(),//: If this function was created using a call to eval returns a string representing the location where eval was called.
|
||||
isToplevel: callsite.isToplevel(),//: Returns true if this is a top-level invocation, that is, if it's a global object.
|
||||
isEval: callsite.isEval(),//: Returns true if this call takes place in code defined by a call to eval.
|
||||
isNative: callsite.isNative(),//: Returns true if this call is in native V8 code.
|
||||
isConstructor: callsite.isConstructor(),//: Returns true if this is a constructor call.
|
||||
isAsync: callsite.isAsync(),//: Returns true if this call is asynchronous (i.e. await, Promise.all(), or Promise.any()).
|
||||
isPromiseAll: callsite.isPromiseAll(),//: Returns true if this is an asynchronous call to Promise.all().
|
||||
getPromiseIndex: callsite.getPromiseIndex(),//: Returns the index of the promise element that was followed in Promise.all() or Promise.any() for async stack traces, or null if the CallSite is not an asynchronous Promise.all() or Promise.any() call.
|
||||
}
|
||||
}
|
||||
|
||||
const getStack = (err, { shiftHowMany = 0 } = {}) => {
|
||||
const old = Error.prepareStackTrace
|
||||
|
||||
Error.prepareStackTrace = (err, callsites) => {
|
||||
return {
|
||||
summary: err.stack.split("\n").toSpliced(1, shiftHowMany).join("\n"),
|
||||
callsites: callsites.toSpliced(0, shiftHowMany).map(format),
|
||||
}
|
||||
}
|
||||
|
||||
const stack = err.stack
|
||||
|
||||
Error.prepareStackTrace = old
|
||||
|
||||
return stack
|
||||
}
|
||||
|
||||
const stacktrace = (err, { shiftHowMany = 0 } = {}) => {
|
||||
// if (position >= Error.stackTraceLimit) {
|
||||
// throw new TypeError('getCallerFile(position) requires position be less then Error.stackTraceLimit but position was: `' + position + '` and Error.stackTraceLimit was: `' + Error.stackTraceLimit + '`')
|
||||
// }
|
||||
|
||||
const { summary, callsites } = getStack(err, { shiftHowMany })
|
||||
const lines = summary.match(LINE_RE)
|
||||
|
||||
const info = {
|
||||
summary,
|
||||
stacktraces: callsites.map((callsite, index) => ({ summary: lines[index], callsite }))
|
||||
}
|
||||
|
||||
return info
|
||||
}
|
||||
|
||||
export { stacktrace }
|
Loading…
Reference in New Issue