How to write an unit test in Jest to check for variable reassignment?










0















I am using Jest to test a helper function in my React application. Following is my test module:



import React from 'react';
import returnFirstTwoDrivers from '../../../src/Utils/returnFirstTwoDrivers';

describe('returnFirstTwoDrivers()', function () {
it('should be assigned to a constant', function ()
expect(function ()
returnFirstTwoDrivers = 'testing reassignment';
throw new TypeError();
).toThrow(TypeError);
);


Here, I am reassigning the returnFirstTwoDrivers variable and I am expecting this operation to throw an error because returnFirstTwoDrivers is a const variable.



When I run the test, the test suite fails to run and it is showing an error on line: returnFirstTwoDrivers = 'testing reassignment'; If anyone know how to fix this issue, please help me. Thank you.










share|improve this question



















  • 1





    What are you actually trying to test here? This is a language-level error, not a runtime error.

    – Dave Newton
    Nov 15 '18 at 15:47












  • If i get you right then you basically check wether a const could be reassigned. But that is a language feature and you dont have to test it.

    – Thomas Kleßen
    Nov 15 '18 at 15:48











  • @DaveNewton I am trying to check whether I can reassign returnFirstTwoDrivers

    – socrates
    Nov 15 '18 at 15:48











  • @socrates Obviously you cannot--but this is a compile-time error, not a runtime error. This makes no sense to test.

    – Dave Newton
    Nov 15 '18 at 15:49











  • yeah, I know. How do I check it? I do not want returnFirstTwoDrivers to be assigned to a let or var

    – socrates
    Nov 15 '18 at 15:49
















0















I am using Jest to test a helper function in my React application. Following is my test module:



import React from 'react';
import returnFirstTwoDrivers from '../../../src/Utils/returnFirstTwoDrivers';

describe('returnFirstTwoDrivers()', function () {
it('should be assigned to a constant', function ()
expect(function ()
returnFirstTwoDrivers = 'testing reassignment';
throw new TypeError();
).toThrow(TypeError);
);


Here, I am reassigning the returnFirstTwoDrivers variable and I am expecting this operation to throw an error because returnFirstTwoDrivers is a const variable.



When I run the test, the test suite fails to run and it is showing an error on line: returnFirstTwoDrivers = 'testing reassignment'; If anyone know how to fix this issue, please help me. Thank you.










share|improve this question



















  • 1





    What are you actually trying to test here? This is a language-level error, not a runtime error.

    – Dave Newton
    Nov 15 '18 at 15:47












  • If i get you right then you basically check wether a const could be reassigned. But that is a language feature and you dont have to test it.

    – Thomas Kleßen
    Nov 15 '18 at 15:48











  • @DaveNewton I am trying to check whether I can reassign returnFirstTwoDrivers

    – socrates
    Nov 15 '18 at 15:48











  • @socrates Obviously you cannot--but this is a compile-time error, not a runtime error. This makes no sense to test.

    – Dave Newton
    Nov 15 '18 at 15:49











  • yeah, I know. How do I check it? I do not want returnFirstTwoDrivers to be assigned to a let or var

    – socrates
    Nov 15 '18 at 15:49














0












0








0


1






I am using Jest to test a helper function in my React application. Following is my test module:



import React from 'react';
import returnFirstTwoDrivers from '../../../src/Utils/returnFirstTwoDrivers';

describe('returnFirstTwoDrivers()', function () {
it('should be assigned to a constant', function ()
expect(function ()
returnFirstTwoDrivers = 'testing reassignment';
throw new TypeError();
).toThrow(TypeError);
);


Here, I am reassigning the returnFirstTwoDrivers variable and I am expecting this operation to throw an error because returnFirstTwoDrivers is a const variable.



When I run the test, the test suite fails to run and it is showing an error on line: returnFirstTwoDrivers = 'testing reassignment'; If anyone know how to fix this issue, please help me. Thank you.










share|improve this question
















I am using Jest to test a helper function in my React application. Following is my test module:



import React from 'react';
import returnFirstTwoDrivers from '../../../src/Utils/returnFirstTwoDrivers';

describe('returnFirstTwoDrivers()', function () {
it('should be assigned to a constant', function ()
expect(function ()
returnFirstTwoDrivers = 'testing reassignment';
throw new TypeError();
).toThrow(TypeError);
);


Here, I am reassigning the returnFirstTwoDrivers variable and I am expecting this operation to throw an error because returnFirstTwoDrivers is a const variable.



When I run the test, the test suite fails to run and it is showing an error on line: returnFirstTwoDrivers = 'testing reassignment'; If anyone know how to fix this issue, please help me. Thank you.







javascript reactjs unit-testing jasmine jestjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 16:40









LazerBass

1,44531123




1,44531123










asked Nov 15 '18 at 15:36









socratessocrates

136110




136110







  • 1





    What are you actually trying to test here? This is a language-level error, not a runtime error.

    – Dave Newton
    Nov 15 '18 at 15:47












  • If i get you right then you basically check wether a const could be reassigned. But that is a language feature and you dont have to test it.

    – Thomas Kleßen
    Nov 15 '18 at 15:48











  • @DaveNewton I am trying to check whether I can reassign returnFirstTwoDrivers

    – socrates
    Nov 15 '18 at 15:48











  • @socrates Obviously you cannot--but this is a compile-time error, not a runtime error. This makes no sense to test.

    – Dave Newton
    Nov 15 '18 at 15:49











  • yeah, I know. How do I check it? I do not want returnFirstTwoDrivers to be assigned to a let or var

    – socrates
    Nov 15 '18 at 15:49













  • 1





    What are you actually trying to test here? This is a language-level error, not a runtime error.

    – Dave Newton
    Nov 15 '18 at 15:47












  • If i get you right then you basically check wether a const could be reassigned. But that is a language feature and you dont have to test it.

    – Thomas Kleßen
    Nov 15 '18 at 15:48











  • @DaveNewton I am trying to check whether I can reassign returnFirstTwoDrivers

    – socrates
    Nov 15 '18 at 15:48











  • @socrates Obviously you cannot--but this is a compile-time error, not a runtime error. This makes no sense to test.

    – Dave Newton
    Nov 15 '18 at 15:49











  • yeah, I know. How do I check it? I do not want returnFirstTwoDrivers to be assigned to a let or var

    – socrates
    Nov 15 '18 at 15:49








1




1





What are you actually trying to test here? This is a language-level error, not a runtime error.

– Dave Newton
Nov 15 '18 at 15:47






What are you actually trying to test here? This is a language-level error, not a runtime error.

– Dave Newton
Nov 15 '18 at 15:47














If i get you right then you basically check wether a const could be reassigned. But that is a language feature and you dont have to test it.

– Thomas Kleßen
Nov 15 '18 at 15:48





If i get you right then you basically check wether a const could be reassigned. But that is a language feature and you dont have to test it.

– Thomas Kleßen
Nov 15 '18 at 15:48













@DaveNewton I am trying to check whether I can reassign returnFirstTwoDrivers

– socrates
Nov 15 '18 at 15:48





@DaveNewton I am trying to check whether I can reassign returnFirstTwoDrivers

– socrates
Nov 15 '18 at 15:48













@socrates Obviously you cannot--but this is a compile-time error, not a runtime error. This makes no sense to test.

– Dave Newton
Nov 15 '18 at 15:49





@socrates Obviously you cannot--but this is a compile-time error, not a runtime error. This makes no sense to test.

– Dave Newton
Nov 15 '18 at 15:49













yeah, I know. How do I check it? I do not want returnFirstTwoDrivers to be assigned to a let or var

– socrates
Nov 15 '18 at 15:49






yeah, I know. How do I check it? I do not want returnFirstTwoDrivers to be assigned to a let or var

– socrates
Nov 15 '18 at 15:49













0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322868%2fhow-to-write-an-unit-test-in-jest-to-check-for-variable-reassignment%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322868%2fhow-to-write-an-unit-test-in-jest-to-check-for-variable-reassignment%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党