Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest "not allowed to reference out of scope variables" for typescript types #5662

Closed
sampsonjoliver opened this issue Nov 1, 2018 · 2 comments

Comments

@sampsonjoliver
Copy link

sampsonjoliver commented Nov 1, 2018

Is this a bug report?

Yes

Did you try recovering your dependencies?

Yes

Environment

Yarn 1.10.1 on Windows 10
CRA 2.1
Node 8.10.0

Steps to Reproduce

  1. Init a CRA using typescript yarn create-react-app <name> --typescript
  2. In App.test.tsx, mock a package, specifying a type argument in the mock value
type MyType = {};

jest.mock('./index', () => {
  return {
    index: (foo: MyType ) => foo
  };
});

it('renders without crashing', () => {
  ...
});
  1. Run tests yarn test

Expected Behavior

Typescript and all type information to be removed/ignored during runtime by the test runner.

Actual Behavior

It seems like the babel-plugin-jest-hoist doesn't play with babel's typescript compiler, and is being fed sources that still have type information attached while it's running. Which would be a-okay, except that Jest gets angry any time something is referenced inside a mock function that doesn't start with mock<TheThing>.

This might be a babel issue rather than a CRA issue - but raising it here as this is behaviour that works in react-scripts-ts - probably thanks to the way ts-jest loads the tests - but doesn't work with CRA 2.1.

Here's the output from jest

babel-plugin-jest-hoist: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables.
    Invalid variable access: MyType

    Whitelisted objects: Array, ArrayBuffer, Boolean, DataView, Date, Error, EvalError, Float32Array, Float64Array, Function, Generator, GeneratorFunction, Infinity, Int16Array, Int32Array, Int8Array, InternalError, Intl, JSON, Map, Math, NaN, Number, Object, Promise, Proxy, RangeError, ReferenceError, Reflect, RegExp, Set, String, Symbol, SyntaxError, TypeError, URIError, Uint16Array, Uint32Array, Uint8Array, Uint8ClampedArray, WeakMap, WeakSet, arguments, console, expect, isNaN,
jest, parseFloat, parseInt, require, undefined, DTRACE_NET_SERVER_CONNECTION, DTRACE_NET_STREAM_END, DTRACE_HTTP_SERVER_REQUEST, DTRACE_HTTP_SERVER_RESPONSE, DTRACE_HTTP_CLIENT_REQUEST, DTRACE_HTTP_CLIENT_RESPONSE, COUNTER_NET_SERVER_CONNECTION, COUNTER_NET_SERVER_CONNECTION_CLOSE, COUNTER_HTTP_SERVER_REQUEST, COUNTER_HTTP_SERVER_RESPONSE, COUNTER_HTTP_CLIENT_REQUEST, COUNTER_HTTP_CLIENT_RESPONSE, global, process, Buffer, clearImmediate, clearInterval, clearTimeout, setImmediate, setInterval, setTimeout.
    Note: This is a precaution to guard against uninitialized mock variables. If it is ensured that the mock is required lazily, variable names prefixed with `mock` (case insensitive) are permitted.

Solution currently is to either not include type information inside of the jest.mock(...) call, or to rename the type

type mockMyType = {};

jest.mock('./index', () => {
  return {
    index: (foo: mockMyType ) => foo
  };
});

it('renders without crashing', () => {
  ...
});
@ianschmitz
Copy link
Contributor

Some reference issues:

facebook/react-native#19120
jestjs/jest#2567

@Timer Timer modified the milestones: 2.1.2, 2.1.3 Dec 23, 2018
@ianschmitz ianschmitz modified the milestones: 2.1.3, 2.1.4 Jan 4, 2019
@ianschmitz ianschmitz modified the milestones: 2.1.4, 2.1.5 Feb 10, 2019
@iansu iansu modified the milestones: 2.1.6, 2.1.x, 3.x Mar 6, 2019
@iansu
Copy link
Contributor

iansu commented Jun 30, 2021

Can you try the latest release and see if this has improved? If not please open a new issue.

@iansu iansu closed this as completed Jun 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants