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

Check assertion message is a string #1125

Closed
creeperyang opened this issue Nov 22, 2016 · 11 comments · Fixed by #2102
Closed

Check assertion message is a string #1125

creeperyang opened this issue Nov 22, 2016 · 11 comments · Fixed by #2102
Assignees
Labels
breaking requires a SemVer major release enhancement new functionality help wanted scope:assertions

Comments

@creeperyang
Copy link

creeperyang commented Nov 22, 2016

(See #1125 (comment), @novemberborn)


[email protected], [email protected]

When I run test, sometimes I get this error:

TypeError: test.error.message.split is not a function
    at /Users/creeper/work/projects/ysprite/node_modules/ava/lib/reporters/mini.js:181:43
    at Array.forEach (native)
    at MiniReporter.finish (/Users/creeper/work/projects/ysprite/node_modules/ava/lib/reporters/mini.js:173:20)
    at Logger.finish (/Users/creeper/work/projects/ysprite/node_modules/ava/lib/logger.js:49:27)
    at /Users/creeper/work/projects/ysprite/node_modules/ava/lib/cli.js:161:12
From previous event:
    at Object.exports.run (/Users/creeper/work/projects/ysprite/node_modules/ava/lib/cli.js:160:5)
    at Object.<anonymous> (/Users/creeper/work/projects/ysprite/node_modules/ava/cli.js:23:24)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

And I found this maybe caused by:

test('message', t => {
  return aPromise.then(() => {
    // error occured 
  })
})
@sindresorhus
Copy link
Member

What error occured in your promise?
The only way the above error can be hit is if the message of the error exists but is not a string.

@creeperyang
Copy link
Author

creeperyang commented Dec 1, 2016

console.log('------>', test.error);
------> { name: 'AssertionError',
  actual: false,
  expected: false,
  operator: 'fail',
  message: 
   { name: 'Error',
     stack: 'Error: ENOENT: no such file or directory, open \'app.png\'\n    at Error (native)',
     message: 'ENOENT: no such file or directory, open \'app.png\'',
     errno: -2,
     code: 'ENOENT',
     syscall: 'open',
     path: 'app.png' },
  generatedMessage: false,
  stack: 'AssertionError: Error: ENOENT: no such file or directory, open \'app.png\'\n    generateSprite.then.then.catch.err (test/sprite.spec.js:103:11)\n    ' }

test.message is an Error object.

@creeperyang
Copy link
Author

creeperyang commented Dec 2, 2016

It eventually looks like caused by calling t.fail(err). I pass an Error object to t.fail and it seems we should use string here.

However I think it's good to support both error message and error object.

x.fail = function (msg) {
        if (msg instanceof Error) {
            msg = msg.message
        }
	msg = msg || 'Test failed via t.fail()';
	test(false, create(false, false, 'fail', msg, x.fail));
};

@novemberborn
Copy link
Member

@creeperyang t.fail() only takes a message, just like the other assertions take a message as their last argument.

@avajs/core perhaps it would be useful to typecheck that argument?

@vadimdemedes
Copy link
Contributor

@novemberborn Agree, a check wouldn't hurt.

@sindresorhus
Copy link
Member

👍

@creeperyang
Copy link
Author

👍 much better.

@novemberborn novemberborn changed the title TypeError: test.error.message.split is not a function Check assertion message is a string Dec 4, 2016
@novemberborn novemberborn added enhancement new functionality help wanted and removed question labels Dec 4, 2016
@novemberborn
Copy link
Member

Cool. We're now looking to add a typecheck for the optional assertion message parameter, ensuring it's a string if it is provided.

@novemberborn novemberborn reopened this Dec 4, 2016
@leewaygroups
Copy link

leewaygroups commented Jan 7, 2017

May I take on this as a gentle step into AVA.

One question: For uniformity, would it not make sense to apply same check in other sibling functions such truthy, falsy etc?

In that case the check can be centralised as a helper and invoked in all sibling functions where it is needed.

@novemberborn
Copy link
Member

@leewaygroups yes please! Thank you!

@novemberborn
Copy link
Member

To clarify, per #1831 (comment), we want to type check the message parameter when the assertion is invoked, and fail the assertion if the parameter is not undefined and not a string.

@novemberborn novemberborn added the breaking requires a SemVer major release label Aug 11, 2018
@novemberborn novemberborn removed this from the 1.0 milestone Aug 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking requires a SemVer major release enhancement new functionality help wanted scope:assertions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants