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

Bug: uvu is not detecting a thrown string #130

Open
jasikpark opened this issue Aug 9, 2021 · 13 comments
Open

Bug: uvu is not detecting a thrown string #130

jasikpark opened this issue Aug 9, 2021 · 13 comments

Comments

@jasikpark
Copy link

jasikpark commented Aug 9, 2021

In #129 there's a function call that is only detected by uvu if i catch and rethrow it, it just fails to complete the test suite otherwise.

by altering the test to this:

Prettier('can format a basic Astro file', async () => {

  const [src, out] = await getFiles('basic');

  assert.not.fixture(src, out);

  console.log('before format');

  try {

    const formatted = format(src);

  } catch (e) {

    if (typeof e == 'Error') {

      throw e;

    }

    throw new Error(e);

  }

  console.log('after format');

  assert.fixture(formatted, out);

});

I can get the suite to run correctly.. I'm not sure why it's failing there.

#129 (comment)

@jasikpark
Copy link
Author

jasikpark commented Aug 9, 2021

I'm not sure that it's a thrown string that is being missed, but if you clone the prettier-plugin-astro repo, and run yarn test the first test "basic" is the culprit. I'll see if I can print out more diagnostics for why it's not detected when I get back to my laptop

@jasikpark
Copy link
Author

minimal reproduction of the bug:
https://codesandbox.io/s/gallant-lederberg-9rmoy?file=/package.json:158-171 open the link and open a new terminal and run yarn test.

@jasikpark jasikpark changed the title uvu is not detecting a thrown string? Bug: uvu is not detecting a thrown string? Aug 10, 2021
@jasikpark jasikpark changed the title Bug: uvu is not detecting a thrown string? Bug: uvu is not detecting a thrown string Aug 10, 2021
@jasikpark
Copy link
Author

ah, this is a duplicate of #60

@lukeed
Copy link
Owner

lukeed commented Aug 10, 2021

It wouldnt be the same cause as 60. Your CSB doesn't work btw, 502 bad gateway

@jasikpark
Copy link
Author

jasikpark commented Aug 10, 2021

It wouldnt be the same cause as 60. Your CSB doesn't work btw, 502 bad gateway

yeah, i don't think that was the right way to write my reproduction - you need to open a new terminal and run 'yarn test' to see the output.

codesandbox is expecting yarn start to exist and create an http server, but i don't want one 😅

@jasikpark
Copy link
Author

Here's a better link for the reproduction: https://playground.denobr.com/801f4d5301

@lukeed
Copy link
Owner

lukeed commented Aug 10, 2021

Think you forgot to save 😆 – that's just a "hello world" for a Deno playground.

No need to fiddle with this. Saving your CSB file contents here & will repro locally soon:

import { test } from "uvu";

test("this test should fail", () => {
  console.log("before throw of string");
  throw "not detected by uvu";
  console.log("after throw of string");
  throw Error("hmmmm");
});

test.run();

@jasikpark
Copy link
Author

basically the console.logs are meant to demonstrate where the async function stops executing

@lukeed
Copy link
Owner

lukeed commented Aug 10, 2021

Yup, understood.

@jasikpark
Copy link
Author

The error is caused by format() accessing nonexistent properties on the string, causing an uncaught error.

@jasikpark
Copy link
Author

function format(name, err, suite = '') {
	let { details, operator='' } = err;
	let idx = err.stack && err.stack.indexOf('\n');
	if (err.name.startsWith('AssertionError') && !operator.includes('not')) details = compare(err.actual, err.expected); // TODO?
	let str = '  ' + FAILURE + (suite ? kleur.red(SUITE(` ${suite} `)) : '') + ' ' + QUOTE + kleur.red().bold(name) + QUOTE;
	str += '\n    ' + err.message + (operator ? kleur.italic().dim(`  (${operator})`) : '') + '\n';
	if (details) str += GUTTER + details.split('\n').join(GUTTER);
	if (!!~idx) str += stack(err.stack, idx);
	return str + '\n';
}

when err is a string, it doesn't have a err.name property, and so it throws

@lukeed
Copy link
Owner

lukeed commented Aug 10, 2021

Yes I know. I'm keeping this open as an exploration to see if it even makes sense to throw non-Errors.

The entirety of uvu operates with Error primitives. That's how assertion control is orchestrated. Throwing things that are not Errors is not the same thing as throwing an Error. In other words, throw on its own is not exclusively a "this is an error" -- it's a way to abort and bubble up any value, kinda the same way goto works in other languages. Errors are Errors.

Tbh my inclination is to close this as unsupported, but going to play with it first.

@jasikpark
Copy link
Author

I would say that if you end up not supporting detecting non-Error() throws, that should be added to the documentation with a recommendation for how to correctly catch and rethrow them for uvu.

Because I agree that encouraging Error() use is a good idea, but I haven't encountered many examples of using throw for things other than error reporting, even if it's via a raw string - maybe it could catch other values and print a warning that they aren't true errors, but are strings, so they should be changed

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

No branches or pull requests

2 participants