relay icon indicating copy to clipboard operation
relay copied to clipboard

[relay-test-utils] createMockEnvironment expects jest to be defined

Open rachel-church opened this issue 2 years ago • 5 comments

The package.json file of relay-test-utils does not require jest, but attempting to use createMockEnvironment in any test runner except for jest will result in the following error:

ReferenceError: jest is not defined
      at mockDisposableMethod (node_modules/relay-test-utils/lib/RelayModernMockEnvironment.js:40:17)
      at Object.createMockEnvironment (node_modules/relay-test-utils/lib/RelayModernMockEnvironment.js:354:5)

It looks like RelayModernMockEnvironment utilizes jest.fn under the hood to create mock methods on the Environment object.

This issue prevents users from using createMockEnvironment with test runners such as vitest, mocha, jasmine, ava, etc.

Reproduction using mocha: https://glitch.com/edit/#!/frost-tulip-polka

rachel-church avatar Feb 24 '23 22:02 rachel-church

It looks like this could be an easy fix by adding a dependency on jest-mock and conditionally using it if the global jest instance isn't defined.

packages/relay-test-utils/RelayModernMockEnvironment.js

const jest = typeof jest === 'undefined' ? new require('jest-mock').ModuleMocker(global) : jest;

Would jest-mock need to be a peerDependency or a dependency?

rachel-church avatar Feb 28 '23 16:02 rachel-church

  • peerDependency

sibelius avatar Feb 28 '23 16:02 sibelius

Forgive my ignorance here, just started using Relay myself with a Vite/Vitest app.

Is there a suggested workaround I can employ until this is fixed?

Dmo16 avatar Mar 11 '23 01:03 Dmo16

@Dmo16 for a workaround, in a setupFiles, i put:

import {vi} from 'vitest'
global.jest = vi

jhsu avatar Mar 22 '23 20:03 jhsu

anybody got an example of using vitest with relay?

anybody willing to pursue a pull request to fix this ?

sibelius avatar Apr 21 '24 18:04 sibelius