expo-typescript-starter icon indicating copy to clipboard operation
expo-typescript-starter copied to clipboard

Jest tests not working

Open EdSancha opened this issue 4 years ago • 4 comments

First of all, thanks for putting this template together!

One of the requirements of my project is to support unit testing through Jest, but if I run:

yarn test  # "test": "node ./node_modules/jest/bin/jest.js --watchAll"

I get the following error. Has anyone encountered this problem and if so, what's the solution? Thanks a lot!

 FAIL  src/components/__tests__/StyledText.test.tsx
  ● Test suite failed to run

    babel-plugin-jest-hoist: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables.
    Invalid variable access: process
    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, global, clearInterval, clearTimeout, setInterval, setTimeout, queueMicrotask, clearImmediate, setImmediate.
    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.

      at invariant (node_modules/babel-plugin-jest-hoist/build/index.js:15:11)
      at newFn (node_modules/@babel/traverse/lib/visitors.js:193:21)
      at NodePath._call (node_modules/@babel/traverse/lib/path/context.js:53:20)
      at NodePath.call (node_modules/@babel/traverse/lib/path/context.js:40:17)

 FAIL  __tests__/RoundButton.test.tsx
  ● Test suite failed to run

    babel-plugin-jest-hoist: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables.
    Invalid variable access: process
    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, global, clearInterval, clearTimeout, setInterval, setTimeout, queueMicrotask, clearImmediate, setImmediate.
    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.

      at invariant (node_modules/babel-plugin-jest-hoist/build/index.js:15:11)
      at newFn (node_modules/@babel/traverse/lib/visitors.js:193:21)
      at NodePath._call (node_modules/@babel/traverse/lib/path/context.js:53:20)
      at NodePath.call (node_modules/@babel/traverse/lib/path/context.js:40:17)

Test Suites: 2 failed, 2 total
Tests:       0 total
Snapshots:   0 total
Time:        0.764s
Ran all test suites.

Watch Usage
 › Press f to run only failed tests.
 › Press o to only run tests related to changed files.
 › Press p to filter by a filename regex pattern.
 › Press t to filter by a test name regex pattern.
 › Press q to quit watch mode.
 › Press Enter to trigger a test run.

EdSancha avatar Mar 05 '20 19:03 EdSancha

@EdSancha Thanks for opening an issue! I've investigated, and it seemed like jest-expo was causing the issue you mentioned.

I've modified jest config a bit, and the test has passed.

  "jest": {
    "preset": "react-native",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ]
  },
```

Naturalclar avatar Mar 11 '20 12:03 Naturalclar

Thanks a lot!

EdSancha avatar Mar 12 '20 23:03 EdSancha

It keeps failing with the same error. Did you do anything else?

EdSancha avatar Mar 12 '20 23:03 EdSancha

@EdSancha sorry for the late response! I have a question, do you have jest installed globally on your pc? if so, what version of jest are you using? The error maybe caused by version mismatch of jest

Naturalclar avatar Mar 30 '20 09:03 Naturalclar