Skip to content

Expand testing with negation and error handling #458

@bbugh

Description

@bbugh

Raised from trying to add tests for #453. I need to check a != 0 or > 0, which does not seem to be available in the testing framework. Since it's written in JavaScript and fails based on exceptions, I have tried to think of the least impactful change to get negation if the expectation is to keep the testing framework using inline JavaScript and not Mint.

I could add an assertNotEqual or an assertNotTextOf (which is a weird name), but it seems like adding negation like other test frameworks do would be a better result.

<Test.Provider.AnimationFrame/>
|> start()
|> not(assertTextOf("something", "value"))

/* or */

|> assertTextOf.not("something", "value")

/* or */

|> Assert.not.textOf("something", "value")

/* or */

|> not(() : { assertTextOf("something", "value") })

The trouble with this is that not finding a selector is not the same as the test passing. This could be handled with custom Error types, rather than catching any exception. You could leave the regular exception errors for test implementation failures, and then raise an AssertionFailure (or something) that gets handled differently when caught. For example, is assertTextOf with only a change to throw an AssertionFailure.

fun assertTextOf (
  selector : String,
  value : String,
  context : Test.Context(Dom.Element)
) : Test.Context(Dom.Element) {
  `
  #{context}.step((element) => {
    let text = "";

    try {
      text = element.querySelector(#{selector}).textContent
    } catch (error) {
      throw \`Could not find element with selector: ${#{selector}}\`
    }

    if (text == #{value}) {
      return element
    } else {
      throw new AssertionFailure(\`"${text}" != "${#{value}}"\`) 🆕
    }
  })
  `
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussionstdlibStandard library relatedtoolingTooling related feature (formatter, documentation, production builder)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions