Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assertThrowsAsync inconsistent with assertThrows, error message shows wrong actual error class #967

Closed
KyleJune opened this issue Jun 13, 2021 · 0 comments · Fixed by #1051

Comments

@KyleJune
Copy link
Contributor

When you assert an error class but a different error was thrown, the message for assertThrows ends with ', but was "ExampleError".'. the message for assertThrowsAsync has a similar message except instead of "was" it says "got" and it always shows the actual as being "Error" incorrectly. I would expect all the tests to pass if assertThrowsAsync was working correctly.

I have deno 1.11.0 installed. Below is a minimum reproduction path. The assertThrows tests and the assertThrowsAsync test pass, but the 2 tests for what error assertThrowsAsync should throw fail.

import {
  AssertionError,
  assertThrows,
  assertThrowsAsync,
} from "https://deno.land/[email protected]/testing/asserts.ts";

class ExampleError extends Error {}
class OtherError extends Error {}

Deno.test("assertThrows", () => {
  assertThrows(
    () => {
      throw new ExampleError("failed");
    },
    ExampleError,
    "fail",
  );
});

Deno.test("assertThrows error", () => {
  assertThrows(
    () =>
      assertThrows(
        () => {
          throw new ExampleError("failed");
        },
        OtherError,
        "fail",
      ),
    AssertionError,
    'Expected error to be instance of "OtherError", but was "ExampleError".',
  );
});

Deno.test("assertThrowsAsync", async () => {
  await assertThrowsAsync(
    () => Promise.reject(new ExampleError("failed")),
    ExampleError,
    "fail",
  );
});

Deno.test("assertThrowsAsync promise rejected", async () => {
  await assertThrowsAsync(
    () =>
      assertThrowsAsync(
        () => Promise.reject(new ExampleError("failed")),
        OtherError,
        "fail",
      ),
    AssertionError,
    'Expected error to be instance of "OtherError", but was "ExampleError".',
  );
});

Deno.test("assertThrowsAsync error thrown in async function", async () => {
  await assertThrowsAsync(
    () =>
      assertThrowsAsync(
        async () => {
          await Promise.resolve();
          throw new ExampleError("failed");
        },
        OtherError,
        "fail",
      ),
    AssertionError,
    'Expected error to be instance of "OtherError", but was "ExampleError".',
  );
});
$ deno test example_test.ts
running 5 tests from file:///home/kyle/Projects/deno/mock/example_test.ts
test assertThrows ... ok (2ms)
test assertThrows error ... ok (2ms)
test assertThrowsAsync ... ok (1ms)
test assertThrowsAsync promise rejected ... FAILED (1ms)
test assertThrowsAsync error thrown in async function ... FAILED (2ms)

failures:

assertThrowsAsync promise rejected
AssertionError: Expected error message to include "Expected error to be instance of "OtherError", but was "ExampleError".", but got "Expected error to be instance of "OtherError", but got "Error".".
    at assertThrowsAsync (https://deno.land/[email protected]/testing/asserts.ts:632:13)
    at async file:///home/kyle/Projects/deno/mock/example_test.ts:44:3
    at async asyncOpSanitizer (deno:runtime/js/40_testing.js:21:9)
    at async resourceSanitizer (deno:runtime/js/40_testing.js:58:7)
    at async exitSanitizer (deno:runtime/js/40_testing.js:85:9)
    at async runTest (deno:runtime/js/40_testing.js:199:7)
    at async Object.runTests (deno:runtime/js/40_testing.js:244:7)
    at async file:///home/kyle/Projects/deno/mock/$deno$test.js:1:1

assertThrowsAsync error thrown in async function
AssertionError: Expected error message to include "Expected error to be instance of "OtherError", but was "ExampleError".", but got "Expected error to be instance of "OtherError", but got "Error".".
    at assertThrowsAsync (https://deno.land/[email protected]/testing/asserts.ts:632:13)
    at async file:///home/kyle/Projects/deno/mock/example_test.ts:57:3
    at async asyncOpSanitizer (deno:runtime/js/40_testing.js:21:9)
    at async resourceSanitizer (deno:runtime/js/40_testing.js:58:7)
    at async exitSanitizer (deno:runtime/js/40_testing.js:85:9)
    at async runTest (deno:runtime/js/40_testing.js:199:7)
    at async Object.runTests (deno:runtime/js/40_testing.js:244:7)
    at async file:///home/kyle/Projects/deno/mock/$deno$test.js:1:1

failures:

        assertThrowsAsync promise rejected
        assertThrowsAsync error thrown in async function

test result: FAILED. 3 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out (45ms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant