Skip to content

assert.throwsError not recognizing thrown error #1

@bennettscience

Description

@bennettscience

When testing that a constructor throws an error, the test fails with the message, No error was thrown, expecting error of type 'Error'. Checking err instanceof Error returns true in the console during function execution.

class MyClass {
  constructor(url) {
    let notAllowed = "api/v1";

    if(url.includes(notAllowed)) {
      let error = new Error('You can't do that');
      console.log(error instanceof Error) // => true
      throw error;
    }
  }
}

// Throws an Error in the console
function simple_test() {
  new MyClass("https://www.example.com/api/v1")
}


// Fails the unit test with "No error was thrown, expecting error of type 'Error'"
function utgsTest() {
  const { describe, it, assert } = Utgs.module()
  describe('A unit test', function() {
    it('will fail with a bad structure', function() {
      assert.throwsError("api/v1 in the URL", function() {
        new MyClass("https://www.example.com/api/v1")
      })
    })
  })
} 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions