question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`expect-expect` & `no-standalone-expect` when using `expect` in afterEach

See original GitHub issue

Example Code

Sometimes I group my enzyme snapshot tests like this to avoid writing expect(shallowToJson(wrapper)).toMatchSnapshot() repeatedly:

import React from 'react';
import { shallow } from 'enzyme';
import shallowToJson from 'enzyme-to-json';

import Component1 from './index';


describe('outputs the expected tree when', () => {
  let wrapper;
  
  test('prop1 is foo', () => {
    wrapper = shallow((
      <Component1 prop1="foo" />
    ));
  });
  
  test('prop1 is bar', () => {
    wrapper = shallow((
      <Component1 prop1="bar" />
    ));
  });

  afterEach(() => {
    expect(shallowToJson(wrapper)).toMatchSnapshot();
  });
});

The tests are often times more complex than ^ with which props are being passed.


Problem

There are two eslint-plugin-jest rules that I’d like to use, expect-expect and no-standalone-expect, but they are incompatible with the pattern above.

expect-expect: my tests don’t have an expect (but afterEach does) • no-standalone-expect: my expect is not inside a test (but is in an afterEach of a describe that contains tests)


Proposed solution

Could the two rules be modified to allow the above use case?

no-standalone-expect could have options for which blocks are valid. Default could remain test and it. Optional: "jest/no-standalone-expect": ["error", allowedBlocks: ['test', 'it', 'afterEach']]

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:12
  • Comments:6

github_iconTop GitHub Comments

1reaction
rdsedmundocommented, May 26, 2020

+1 for the suggestion of adding the allowedBlocks, that would allow compatibility with jest-in-case.

0reactions
G-Rathcommented, Jun 4, 2022

Resolved via #1129

Read more comments on GitHub >

github_iconTop Results From Across the Web

Making 'expect()' mandatory for all test cases
Use expect.assertions / expect.hasAssertions inside the tests, so that they will fail if the required number of expectations isn't met.
Read more >
Frequently Asked Questions - Jasmine Documentation
When a spec keeps running after it tells Jasmine that it's done, it interleaves with the execution of other specs. This can cause...
Read more >
unittest — Unit testing framework
This is useful when a resource that needs to be set up is not available. Expected failures use the expectedFailure() decorator. class ExpectedFailureTestCase( ......
Read more >
Basics of testing components
But in many cases, testing the component class alone, without DOM involvement, can validate ... toMatch(/is off/i); comp.clicked(); expect(comp.message) .
Read more >
Testing best practices - GitLab Docs
Use Factory Doctor to find cases where database persistence is not needed in a given ... it 'performs the expected side-effects' do expect(BackgroundJob).to ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found