ts-essentials#assert TypeScript Examples

The following examples show how to use ts-essentials#assert. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: extract.ts    From earl with MIT License 6 votes vote down vote up
export function extractTsDocCommentsFromString(source: string): MethodComment[] {
  // @todo use TypeScript's parser to extract signatures and comments
  // We have a few bugs already — see skipped tests in extract.test.ts
  const DOC_COMMENT_REGEX = /\/\*\*([\s\S]*?)\*\/[\n\r]+([\s\S]*?)[{|\n\r]+/gm

  const methodComments: MethodComment[] = []

  let rawMethodComment = DOC_COMMENT_REGEX.exec(source)
  assert(rawMethodComment, `Couldn't find any block comments in source:\n\`${source}\``)
  while (rawMethodComment != null) {
    const comment = `/** ${rawMethodComment[1].trim()} */`
    let signature = removeGetterKeyword(rawMethodComment[2].trim())

    if (signature.endsWith(';')) signature = signature.slice(0, -1)

    methodComments.push({ signature, comment })

    rawMethodComment = DOC_COMMENT_REGEX.exec(source)
  }

  return methodComments
}
Example #2
Source File: parse.ts    From earl with MIT License 6 votes vote down vote up
function abbreviateSignature(signature: string, project: TSProject): string {
  const isClassMethod = !signature.includes('function ')
  const sourceCode = isClassMethod ? `function ${signature} {}` : signature

  const sourceFile = project.createSourceFile('temp.ts', sourceCode, { overwrite: true })
  const functionDeclaration = sourceFile.getChildAtIndex(0).getChildAtIndex(0)
  assert(Node.isFunctionDeclaration(functionDeclaration))

  if (signature.includes('this:')) {
    functionDeclaration.getParameter('this')!.remove()
  }

  functionDeclaration.removeReturnType()

  let text = functionDeclaration.getText()

  if (isClassMethod) {
    text = text.slice('function '.length, -' {}'.length)
  }

  return text
}
Example #3
Source File: mocha.ts    From earl with MIT License 6 votes vote down vote up
constructor(private readonly _hooks: MochaHooks) {
    const self = this

    d('Installing beforeEach hook to get testInfo before each test')
    _hooks.beforeEach(function () {
      assert(this.currentTest, "Current test not set by mocha. This shouldn't happen.")
      assert(this.currentTest.file, "Current test file path not set by mocha. This shouldn't happen.")
      assert(this.currentTest.parent, "Current test has no parent set by mocha. This shouldn't happen.")

      self.testInfo = {
        suitName: makeSuiteName(this.currentTest.parent),
        testName: this.currentTest.title,
        testFilePath: this.currentTest.file,
      }
    })
  }
Example #4
Source File: toThrow.ts    From earl with MIT License 5 votes vote down vote up
export function toThrow(control: Control<() => void>, expected: any) {
  assert(control.actual instanceof Function, 'Actual has to be a function to check if threw')

  let actualThrownValue: any | undefined
  let threwAnything = false
  try {
    control.actual()
  } catch (e) {
    threwAnything = true
    actualThrownValue = e
  }

  // we need special handling for this case otherwise we end up with really dummy error message
  if (!threwAnything) {
    return control.assert({
      success: false,
      reason: `Expected to throw but didn't`,
      negatedReason: '-',
    })
  }

  const actualFmt = formatCompact(actualThrownValue)
  const expectedFmt =
    expected instanceof AnythingMatcher
      ? 'anything'
      : expected instanceof ErrorMatcher
      ? expected.format()
      : formatCompact(expected)

  const reason = `Expected to throw ${expectedFmt} but threw ${actualFmt}`
  const negatedReason = `Expected not to throw ${expectedFmt} but threw ${actualFmt}`

  if (!isEqual(actualThrownValue, expected)) {
    control.assert({
      success: false,
      reason,
      negatedReason,
      actual: format(actualThrownValue, null),
      expected: format(expected, actualThrownValue),
    })
  } else {
    control.assert({
      success: true,
      reason,
      negatedReason,
    })
  }
}
Example #5
Source File: deploy.ts    From arbitrum-dai-bridge with GNU Affero General Public License v3.0 5 votes vote down vote up
function throwIfUndefined(val: any): any {
  assert(val !== undefined, 'val is undefined! Static config incorrect!')

  return val
}