jest-chain icon indicating copy to clipboard operation
jest-chain copied to clipboard

global setup in Create-React-App

Open hebacks opened this issue 5 years ago • 3 comments

Question

Hey, thanks for an awesome lib! Is it possible to set it up nicely in a React app bootstrapped with create-react-app, without resorting to manual imports in every test file?

I have tried to import ‘jest-chain’ in src/setupTests.js that holds a global setup of the test environment - to no avail - even though other testing frameworks (eg. react-testing-library) imported here work just fine

  • jest-chain: 1.1.2,
  • react: 16.9.0
  • node: 11.15.0
  • npm: 6.7.0
TypeError: Cannot read property 'toBeGreaterThan' of undefined

    5 |   it('adds 1 and 1', () => {
  > 6 |     expect(1 + 1)
      |     ^
    7 |       .toBe(2)
    8 |       .toBeGreaterThan(1)
    9 |       .toBeLessThan(3);

The only workaround that has worked for me is to manually import jest-chain within each test file but I feel it should be possible without making such a mess 🙀 and that maybe I’m just missing something. Any help would be appreciated!

hebacks avatar Oct 21 '19 22:10 hebacks

Did you try following the installation instructions in the README ?

"jest": {
  "setupFilesAfterEnv": ["jest-chain"]
}

dobesv avatar Oct 23 '19 23:10 dobesv

I doesn't work for me either. I followed the instructions in the README. I have the next configuration

// jest.config.js
setupFilesAfterEnv: ['<rootDir>/packages/buildings-web/setupTests.ts'],

// setupTests.ts
import '@testing-library/jest-dom/extend-expect'; // works
import 'jest-chain';   // doesn't work

// package.json
{
  "devDependencies": {
     "@testing-library/jest-dom": "^5.10.0",
     "jest-chain": "^1.1.5"
  }
}

gabycasper007 avatar Jul 01 '20 15:07 gabycasper007

I doesn't work for me either. I followed the instructions in the README. I have the next configuration

// jest.config.js
setupFilesAfterEnv: ['<rootDir>/packages/buildings-web/setupTests.ts'],

// setupTests.ts
import '@testing-library/jest-dom/extend-expect'; // works
import 'jest-chain';   // doesn't work

// package.json
{
  "devDependencies": {
     "@testing-library/jest-dom": "^5.10.0",
     "jest-chain": "^1.1.5"
  }
}

I got it working by including the global.d.ts file:

// tsconfig.json
"include": ["./global.d.ts"]

// global.d.ts
import '@testing-library/jest-dom/extend-expect';
import 'expect-more-jest';
import 'jest-extended';
import 'jest-chain';

// jest.config.js
setupFilesAfterEnv: ['<rootDir>/packages/buildings-web/setupTests.ts'],

// setupTests.ts
import '@testing-library/jest-dom/extend-expect';
import 'expect-more-jest';
import 'jest-extended';
import 'jest-chain';

gabycasper007 avatar Jul 02 '20 00:07 gabycasper007

Closing as I think this should just work as of the latest version.

Add jest-chain to your jest config and for issues with setting up Typescript, see here.

Please feel free to re-open if there is something I've missed

mattphillips avatar Sep 28 '22 17:09 mattphillips