Naturalclar / expo-typescript-starter

template for react-native project made with expo using typescript
https://expo-storybook-web-example.naturalclar.now.sh
MIT License
116 stars 17 forks source link

Jest tests not working #66

Open EdSancha opened 4 years ago

EdSancha commented 4 years ago

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.
Naturalclar commented 4 years ago

@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"
    ]
  },
EdSancha commented 4 years ago

Thanks a lot!

EdSancha commented 4 years ago

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

Naturalclar commented 4 years ago

@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