Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
SimenB merged 4 commits intojestjs:mainfromverycosy:fix-to-throw-error
Sep 27, 2023
Merged

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

SimenB merged 4 commits intojestjs:mainfromverycosy:fix-to-throw-error
Sep 27, 2023

Conversation

@verycosy
Copy link
Contributor

@verycosyverycosy commentedSep 27, 2023
edited
Loading

Summary

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

current behavior

classErrorAextendsError{}classErrorBextendsError{}constthrowErrorA=()=>{thrownewErrorA();};expect(throwErrorA).toThrowError(ErrorA);// passexpect(throwErrorA).toThrowError(newErrorA());// passexpect(throwErrorA).not.toThrowError(ErrorB);// passexpect(throwErrorA).not.toThrowError(newErrorB());// fail, but this test should be passed

expect behavior

expect(throwErrorA).not.toThrowError(newErrorB());// 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 >> internalModuleit('loads modules and applies transforms',async()=>{construntime=awaitcreateRuntime(__filename,{transform:{'\\.js$':'./test_preprocessor'},});constmodulePath=path.resolve(path.dirname(runtime.__mockRootPath),'internal-root.js',);expect(()=>{runtime.requireModule(modulePath);}).toThrow(newError('preprocessor must not run.'));});

In this case, boththrown.value andexpected areError 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 thatthrown.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

netlifybot commentedSep 27, 2023
edited
Loading

Deploy Preview forjestjs ready!

Builtwithout sensitive environment variables

NameLink
🔨 Latest commit1ab1836
🔍 Latest deploy loghttps://app.netlify.com/sites/jestjs/deploys/6513c633aa5f460008232ba5
😎 Deploy Previewhttps://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 yourNetlify site configuration.

Copy link
Member

@SimenBSimenB 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!

verycosy reacted with laugh emoji
@SimenBSimenB merged commit05b3a83 intojestjs:mainSep 27, 2023
@verycosyverycosy deleted the fix-to-throw-error branchSeptember 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 usingStackOverflow or ourdiscord channel for questions.

@github-actionsgithub-actionsbot locked asresolvedand limited conversation to collaboratorsOct 28, 2023
@SimenB
Copy link
Member

Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

@SimenBSimenBSimenB approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@verycosy@SimenB

[8]ページ先頭

©2009-2025 Movatter.jp