1

Am trying to write a test case for a component. Test cases are getting passed. But JS lint is bugging me. Its throwing an error - :Expected an assignment or function call and instead saw an expression

The error is coming here : expect(dummyOutput.find('h1.screen-reader-text')).to.exist;

My complete testcase code is as below. Can anyone help me here to solve the error please?

import React from 'react';
import { shallow } from 'enzyme';
import Page from './page';

const props = {
  pageLayout: 'article',
  theme: 'test-theme',
  header: {},
  footer: {},
  singleAds: {},
  siteMeta: { copyright_text: '©COPYRIGHT CONTENT HERE' },
};

const dummyProps = {
  pageLayout: 'dummy_text',
  theme: 'test-theme',
  header: {},
  footer: {},
  singleAds: {},
  siteMeta: { copyright_text: '©COPYRIGHT CONTENT HERE' },
};

const specs = () => describe('Page Layout', () => {
  describe('Renders', () => {
    const wrapper = shallow(<Page {...props} />);
    const dummyOutput = shallow(<Page {...dummyProps} />);

    it.only('should not return H1 tag', () => {
      expect(wrapper.find('h1.screen-reader-text')).not.exist;
    });

    it.only('should return H1 tag', () => {
      expect(dummyOutput.find('h1.screen-reader-text')).to.exist;
    });
  });
});

if (process.env.NODE_ENV === 'test') {
  specs();
}

export default specs;
user2349508
  • 55
  • 1
  • 8
  • I had a similar problem once upon a time - I got help with it in this question of mine: https://stackoverflow.com/questions/37558795/nice-way-to-get-rid-of-no-unused-expressions-linter-error-with-chai, maybe it can help you – Ben Hare Aug 19 '17 at 00:14
  • Does this answer your question? [Nice way to get rid of no-unused-expressions linter error with chai](https://stackoverflow.com/questions/37558795/nice-way-to-get-rid-of-no-unused-expressions-linter-error-with-chai) – Mario Varchmin Aug 09 '21 at 16:04

1 Answers1

2

It will warn you about this when it sees an expression on a line where it isn't being assigned to anything. For example,

1 + 1

will make it complain because it just evaluates and does nothing, which the linter thinks is unintended. It likes to see an assignment like

const x = 1 + 1 

In your case I think it is interpreting that line as an expression that isn't being used for anything because it sees that in the end you are accessing a property of a property of the result of the function (.to.exist) but you aren't doing anything (as far as it can tell) with that value.

If you want to make it stop complaining, maybe you could try adding a return:

return expect(dummyOutput.find('h1.screen-reader-text')).to.exist;
hellojeffhall
  • 291
  • 3
  • 8
  • what if the expression in question exists within a for loop? since we cannot return (this would end the iteration), how can we fix something like this? – ChumiestBucket Feb 26 '21 at 22:24
  • 1
    @ChumiestBucket It's hard to say what is best without seeing your code, this link from Ben Hare (above) might be helpful for disabling this rule. https://stackoverflow.com/a/37777342/8185858 – hellojeffhall Feb 27 '21 at 23:33
  • 1
    Alternatively you could try adding a variable assignment or using `map().every()` to create and check an array of result values, but disabling the rule is probably cleaner and easier. – hellojeffhall Feb 27 '21 at 23:34