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

fix: type check for error instance when used toThrowError method #14576

Merged
merged 4 commits into from
Sep 27, 2023

Conversation

verycosy
Copy link
Contributor

@verycosy verycosy commented Sep 27, 2023

Summary

toThrow/toThrowError method does not type checking when expected value is error instance.

current behavior

class ErrorA extends Error { }
class ErrorB extends Error { }

const throwErrorA = () => {
  throw new ErrorA();
};

expect(throwErrorA).toThrowError(ErrorA); // pass
expect(throwErrorA).toThrowError(new ErrorA()); // pass
expect(throwErrorA).not.toThrowError(ErrorB); // pass
expect(throwErrorA).not.toThrowError(new ErrorB()); // fail, but this test should be passed

expect behavior

expect(throwErrorA).not.toThrowError(new ErrorB()); // pass

I added some codes that compare type for this problem.
When comparing, need to check error instance is custom error or not.
Because in some tests like this:

// jest-runtime/src/__test__/runtime_internal_module.test.js >> internalModule
it('loads modules and applies transforms', async () => {
  const runtime = await createRuntime(__filename, {
    transform: {'\\.js$': './test_preprocessor'},
  });
  const modulePath = path.resolve(
    path.dirname(runtime.__mockRootPath),
    'internal-root.js',
  );
  expect(() => {
    runtime.requireModule(modulePath);
  }).toThrow(new Error('preprocessor must not run.'));
});

In this case, both thrown.value and expected are Error instances.
But their constructor is not same.

console.log(thrown.value.constructor); // [Function: Error] { stackTraceLimit: 100 }
console.log(expected.constructor); 
/*
[Function: Error] {
    stackTraceLimit: 100,
     prepareStackTrace: [Function: prepareStackTrace]
}
*/

It's no wonder that thrown.value instanceof expected.constructor is false, thus test will fail.
So I checked expected error is custom error or not before comparing type of instance.

@netlify
Copy link

netlify bot commented Sep 27, 2023

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 1ab1836
🔍 Latest deploy log https://app.netlify.com/sites/jestjs/deploys/6513c633aa5f460008232ba5
😎 Deploy Preview https://deploy-preview-14576--jestjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff, thanks!

@SimenB SimenB merged commit 05b3a83 into jestjs:main Sep 27, 2023
8 of 9 checks passed
@verycosy verycosy deleted the fix-to-throw-error branch September 27, 2023 06:12
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 28, 2023
@SimenB
Copy link
Member

SimenB commented Oct 30, 2023

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants