简体   繁体   中英

JSLint is throwing an Error - Expected an assignment or function call and instead saw an expression

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;

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM