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

Wallaby.js for ReactNative #2230

Closed
manish-jain-1 opened this issue Oct 5, 2019 · 14 comments
Closed

Wallaby.js for ReactNative #2230

manish-jain-1 opened this issue Oct 5, 2019 · 14 comments

Comments

@manish-jain-1
Copy link

manish-jain-1 commented Oct 5, 2019

I cannot get wallaby working with ReactNative. Automatic configuration doesn't work neither manual from https://wallabyjs.com/docs/integration/react-jsx.html. I tried various other combinations that fail into errors from VS Code. Is there an official React Native working copy for Wallaby.js

Please use this repo to reproduce the issue:
https://github.com/manish-jain-1/wallabytest.git

Here's part of package.json info:

"react": "16.6.3",
"react-native": "0.57.8",
"devDependencies": {
        "babel-eslint": "10.0.1",
        "babel-jest": "23.6.0",
        "babel-plugin-jsx-property-alias": "2.0.0",
        "dotenv": "6.2.0",
        "enzyme": "3.9.0",
        "enzyme-adapter-react-16": "1.13.2",
        "eslint": "5.12.0",
        "eslint-config-prettier": "3.4.0",
        "eslint-plugin-filenames": "1.3.2",
        "eslint-plugin-import": "2.14.0",
        "eslint-plugin-prettier": "3.0.1",
        "eslint-plugin-react": "7.12.3",
        "eslint-plugin-react-native": "3.6.0",
        "eslint-plugin-react-native-a11y": "1.2.0",
        "husky": "1.3.1",
        "jest": "23.6.0",
        "jsdom": "15.1.1",
        "jsdom-global": "3.0.2",
        "lint-staged": "8.1.0",
        "metro-react-native-babel-preset": "0.51.1",

babel.config.js

'react-native': {
            presets: ['module:metro-react-native-babel-preset']
        },

Errors with wallaby.js from the page: https://wallabyjs.com/docs/integration/react-jsx.html

[Error] Failed to run compilers on src/file.js, Error: Cannot find module 'babel-preset-react-app' from 'path' 
[Error]     at Function.module.exports [as sync] (/node_modules/resolve/lib/sync.js:58:15) 
[Error]     at resolveStandardizedName (/node_modules/@babel/core/lib/config/files/plugins.js:101:31) 
[Error]     at resolvePreset (/node_modules/@babel/core/lib/config/files/plugins.js:58:10) 
[Error]     at loadPreset (/node_modules/@babel/core/lib/config/files/plugins.js:77:20) 
[Error]     at createDescriptor (/node_modules/@babel/core/lib/config/config-descriptors.js:154:9) 
[Error]     at items.map (/node_modules/@babel/core/lib/config/config-descriptors.js:109:50) 
[Error]     at Array.map (<anonymous>) 
[Error]     at createDescriptors (/node_modules/@babel/core/lib/config/config-descriptors.js:109:29) 
[Error]     at createPresetDescriptors (/node_modules/@babel/core/lib/config/config-descriptors.js:101:10) 
[Error]     at passPerPreset (/node_modules/@babel/core/lib/config/config-descriptors.js:58:96) 
[Error]     at cachedFunction (/node_modules/@babel/core/lib/config/caching.js:33:19) 
[Error]     at presets.presets (/node_modules/@babel/core/lib/config/config-descriptors.js:29:84) 
[Error]     at mergeChainOpts (/node_modules/@babel/core/lib/config/config-chain.js:320:26) 
[Error]     at /node_modules/@babel/core/lib/config/config-chain.js:283:7 
[Error]     at buildRootChain (/node_modules/@babel/core/lib/config/config-chain.js:68:29) 
[Error]     at loadPrivatePartialConfig (/node_modules/@babel/core/lib/config/partial.js:85:55) 
[Error]     at loadFullConfig (/node_modules/@babel/core/lib/config/full.js:43:39) 
[Error]     at transformSync (/node_modules/@babel/core/lib/transform.js:41:38) 
[Error]     at Object.transform (/node_modules/@babel/core/lib/transform.js:22:38) 
[Error]     at process._tickCallback (internal/process/next_tick.js:61:11) 

I also tried solution from #1798 that didn't work.

@smcenlly
Copy link
Member

smcenlly commented Oct 5, 2019

Can you please share how you created your react native app, or else provide us with a sample repo?

We created a new react app using expo-cli following the official getting started instructions and everything worked for us using Automatic Configuration.

@manish-jain-1
Copy link
Author

manish-jain-1 commented Oct 5, 2019

Thanks for your response. This project wasn't created using expo rather it was created with react-native init AwesomeProject. I tried wallaby with my other playground project (RN 0.60) and it seems to be working. But it didn't work with the existing prod project RN (0.57)
Initial error was:
[Error] Incompatible Jest version: Automatic configuration detection is only supported for Jest v24.x or greater, but found v23.6.0 at node_modules/jest/node_modules/jest-cli/build/jest.js 
After upgrade now getting:
TypeError: Cannot read property '_jasmineAdapter' of undefined

Jest from package.json:

"babel-jest": "24.9.0",
"jest": "24.9.0",
"jest": {
       "setupFilesAfterEnv": ["jest"],
       "preset": "react-native",
       "collectCoverage": true,
       "transform": {
           "\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
       },
       "moduleDirectories": [
           "node_modules",
           "bower_components",
           "shared"
       ],
       "testPathIgnorePatterns": [
           "/node_modules/",
           "/__tests__/utils.js"
       ],
       "setupFiles": [
           "<rootDir>/jest/setup.js"
       ],
       "transformIgnorePatterns": [
           "node_modules/(?!(jest-)?react-native)"
       ],
       "coveragePathIgnorePatterns": [
           "/node_modules/",
           "/jest"
       ],
       "coverageDirectory": "jest/coverage",
       "reporters": [
           "default",
           "jest-junit"
       ],
       "coverageReporters": [
           "cobertura",
           "html",
           "lcov"
       ],
       "coverageThreshold": {
           "global": {
               "statements": 73,
               "branches": 41,
               "functions": 51,
               "lines": 70
           }
       }
   },

Wallaby console:

[Info]  Started Wallaby.js Core v1.0.711
[Info]  Wallaby App (realtime reports) is available at: http://localhost:51245
[Error] Runtime error: TypeError: Cannot read property '_jasmineAdapter' of undefined 
[Error]     at Runtime._execModule (/node_modules/jest-runtime/build/index.js:867:68) 
[Error]     at Runtime._loadModule (/node_modules/jest-runtime/build/index.js:577:12) 
[Error]     at Runtime.requireModule (/node_modules/jest-runtime/build/index.js:433:10) 
[Error]     at config.setupFilesAfterEnv.forEach.path (/node_modules/jest/node_modules/jest-jasmine2/build/index.js:187:55) 
[Error]     at Array.forEach (<anonymous>) 
[Error]     at /node_modules/jest/node_modules/jest-jasmine2/build/index.js:187:31 
[Error]     at Generator.next (<anonymous>) 
[Error]     at asyncGeneratorStep (/node_modules/jest/node_modules/jest-jasmine2/build/index.js:27:24) 
[Error]     at _next (/node_modules/jest/node_modules/jest-jasmine2/build/index.js:47:9) 
[Error] Runtime error: TypeError: Cannot read property '_jasmineAdapter' of undefined 

I'll investigate further but unfortunately cannot provide this repo. Let me know if you need any versions frompackage.json

@manish-jain-1
Copy link
Author

Please use this repo to reproduce the issue:

https://github.com/manish-jain-1/wallabytest

@manish-jain-1
Copy link
Author

Looks like I may not be able to upgrade jest because of this issue:
https://stackoverflow.com/questions/54419342/jest-enzyme-shallowwrapper-is-empty-when-creating-snapshot
Is it possible to make it work with "jest": "23.6.0"?

@smcenlly
Copy link
Member

smcenlly commented Oct 7, 2019

Thanks for providing the sample repo. We saw that your sample repo already works with Automatic Configuration if the ShallowWrapper issue you referenced above is not a problem.

It looks like you used our Wallaby configuration file for create-react-app as the base for your configuration file. Create-react-app is a little different because create-react-app tests are run using the react-scripts command while the react native sample you provided uses jest directly from the command line (which is much simpler).

We have provided a working Wallaby.js configuration file (see below) that will work if you don't want to use Automatic Configuration. The changes to the configuration you already had in your sample repo were:

  1. Correct the Wallaby files configuration. The previous configuration did not point to any of your source files.
  2. Update compilers to use the babel configuration provided in your file instead of the create-react-app override.
  3. Read jest configuration from your package.json file instead of using create-react-app's react-scripts.
module.exports = function(wallaby) {
    var path = require('path');
    process.env.NODE_PATH += path.delimiter + path.join(__dirname, 'node_modules');
    require('module').Module._initPaths();

    return {
        files: ['jest/setup.js', 'App.js', '!__tests__/**/*'],

        tests: ['src/**/*.test.js?(x)', '__tests__/**/*.js', '__tests__/*.js'],

        env: {
            type: 'node'
        },

        compilers: {
            '**/*.js?(x)': wallaby.compilers.babel()
        },

        setup: wallaby => {
            const jestConfig = require('./package.json').jest;
            Object.keys(jestConfig.transform || {}).forEach(
                k => ~k.indexOf('^.+\\.(js|jsx') && void delete jestConfig.transform[k]
            );
            delete jestConfig.testEnvironment;
            wallaby.testFramework.configure(jestConfig);
        },

        testFramework: 'jest'
    };
};

@smcenlly smcenlly closed this as completed Oct 7, 2019
@manish-jain-1
Copy link
Author

Note for all: Make sure you add your src folder to wallaby.js files:
files: ['jest/setup.js', 'App.js', '!__tests__/**/*', './src/**/*'],

@manish-jain-1
Copy link
Author

manish-jain-1 commented Oct 8, 2019

Running into an issue from wallaby. Current workaround is to change folder name in package.json.

[Error] Runtime error: ● Validation Error: 
[Error]   Module ./jest/preprocessor.js in the transform option was not found. 
[Error]          <rootDir> is: /Users/user/projectname 

@ArtemGovorov
Copy link
Member

You need to add the missing file to the wallaby config files list:

files: [
  'jest/setup.js', 
+ 'jest/preprocessor.js', 
  'App.js', 
  '!__tests__/**/*', 
  './src/**/*'
],

@manish-jain-1
Copy link
Author

manish-jain-1 commented Oct 10, 2019

So this is working great except react components (anything that extends React.Component) always show white dots (not covered) from wallaby even when there is a snapshot test for it. Second issue, for tests files: looks like tests are showing white with renderer.create (react-test-renderer) but working with enzyme shallow for snapshot testing.

@smcenlly
Copy link
Member

Could you please create or update your previous repo with a sample so we can duplicate the problem?

@smcenlly smcenlly reopened this Oct 11, 2019
@manish-jain-1
Copy link
Author

So, quickly I am seeing below in the wallaby console:

Invariant Violation: withNavigation can only be used on a view hierarchy of a navigator. The wrapped component is unable to get access to navigation from props or context. 

 The above error occurred in the <Context.Consumer> component: 
          in withNavigation(NavigationEvents) (created by ConDetails) 
          in View (created by View) 
          in View (at createAnimatedComponent.js:153) 
          in AnimatedComponent (at react-native-safe-area-view/index.js:153) 
          in SafeView (at withOrientation.js:54) 
          in withOrientation (created by ConsumptionDetails) 
          in ConsumptionDetails (created by Context.Consumer) 
          in Connect(ConsumptionDetails) 
          in Provider 

This is likely coming from import { SafeAreaView } from 'react-navigation';

If you have any ideas, let me know, I will try to investigate further.

Thank you.

@smcenlly
Copy link
Member

It sounds like your issue has something to do with react-native. Unfortunately we're not especially familiar with react-native. Could you please create or update your previous repo with a sample so we can duplicate the problem and see what's going on?

@smcenlly
Copy link
Member

Closing this issue due to lack of activity. If you can provide us with a sample repo, we'll re-open the issue and investigate for you.

@manish-jain-1
Copy link
Author

Few more notes for the community:

  1. There were a lot of Invariant Violation errors due to ReactNative/Jest mocking issues. I believe it's not related to Wallaby. I narrowed down by putting one single test file at a time in wallaby.js and troubleshoot it.
  2. react-test-renderer shows green/red squares whereas enzyme shallow always shows white squares.
  3. In some cases, I was not able to figure out the issue (yet!) so I skipped that part by putting below (looks bad I understand - temporary solution):
{process.env.NODE_ENV !== 'test' && (
                                <NavigationEvents onDidFocus={this.handleDidFocus} />
                            )}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants