expo / eas-tests-example

Demo of running E2E tests on EAS Build
33 stars 2 forks source link

`testFailed` does not work with detox@20 #7

Open anikkumard opened 1 year ago

anikkumard commented 1 year ago

Thanks for the example application. Currently I am facing the error ReferenceError: testFailed is not defined while running my tests If I comment testFailed flag in the below code everything is working fine but missing the feature of only taking screenshot for failed test cases.

here is my code, please suggest how to solve this issue.

ReferenceError: testFailed is not defined

  1 |
  2 | afterEach(async () => {
> 3 |     if (testFailed) {
    |     ^
  4 |       await device.takeScreenshot('screenshot');
  5 |     }
  6 | });
dsokal commented 1 year ago

Hey, Did you create environment.js - https://github.com/expo/eas-tests-example/blob/eca97443e671c488bb5eee805fdd5294eef0ae6f/e2e/environment.js#L26 and update testEnvironment in config.json (https://github.com/expo/eas-tests-example/blob/eca97443e671c488bb5eee805fdd5294eef0ae6f/e2e/config.json#L3)?

anikkumard commented 1 year ago

Hi dsokal, Thanks for checking my issue. Yes I have both the files with correct code in place but still facing the issue. Here is my environment.js const { DetoxCircusEnvironment, } = require('detox/runners/jest-circus'); class CustomDetoxEnvironment extends DetoxCircusEnvironment { constructor(config, context) { super(config, context);

  // Can be safely removed, if you are content with the default value (=300000ms)
  this.initTimeout = 300000;
}

async handleTestEvent(event, state) { const { name } = event;

if (['test_start', 'test_fn_start'].includes(name)) { this.global.testFailed = false; console.log("test passed..........................."); }

if (name === 'test_fn_failure') { this.global.testFailed = true; testFailed = true; console.log("test failed..........................."); }

await super.handleTestEvent(event, state); } } module.exports = CustomDetoxEnvironment; // module.exports = {CustomDetoxEnvironment,testFailed:CustomDetoxEnvironment.testFailed};

Here is my config.json { "maxWorkers": 1, "testEnvironment": "./environment", "testRunner": "jest-circus/runner", "testTimeout": 120000, "testRegex": "\.e2e\.js$", "verbose": true, "setupFilesAfterEnv": ["./setup.js"] }

please do suggest how to solve this issue.

dsokal commented 1 year ago

Can you share a minimal reproducible example of the issue? Preferably in a form of a repository.

anikkumard commented 1 year ago

Hi dsokal, here is the link to minimal reproducible example app. https://github.com/anikkumard/e2e-testing.git

anikkumard commented 1 year ago

Hi dsokal, is there any update on the above issue?

dsokal commented 1 year ago

The update is I don't have an idea. I took a look at your repo and it seems that you've modified some of the parts of the setup from this repo. Sorry but I don't have time to do debug your private configuration as it has nothing to do with this repo. I also think this has nothing to do with EAS Build. I'm closing the issue for now.

anikkumard commented 1 year ago

Hi disokal, The issue is with the expo e2e testing code only, my repo is derived from your repo only. there is no customization except adding few console logs to make debugging simple, expo offical e2e documentation have some code and your example have different code. I tried cloning and running tests on your repository this repo code is broken and not even running. So I demand you to re-open my case and re-investigate it. that will help the community. If this repo code is breaking then why you actively maintaining it, just deprecate the repository and remove it from public githup.

douglowder commented 1 year ago

@anikkumard the problem you have is due to upgrading Detox to 0.20.x. Also, Detox should be in devDependencies, not dependencies. Please make the following patch to your package.json, and the issue should be resolved.

diff --git a/package.json b/package.json
index 449170a..9e5846c 100644
--- a/package.json
+++ b/package.json
@@ -14,13 +14,12 @@
     "expo-splash-screen": "~0.17.5",
     "expo-status-bar": "~1.4.2",
     "react": "18.1.0",
-    "react-native": "0.70.5",
-    "detox": "^20.1.1"
+    "react-native": "0.70.5"
   },
   "devDependencies": {
     "@babel/core": "^7.12.9",
     "@config-plugins/detox": "^4.0.0",
-    "detox": "^20.1.1",
+    "detox": "^19.12.1",
     "jest": "^29.3.1"
   },
   "private": true
anikkumard commented 1 year ago

Hi @douglowder downgrading detox version to "^19.12.1" was fixed the issue, Thanks a lot. Is there any reason why same code is not working with latest detox version 20.1.1, when any new developer set up detox the version will be latest and they do not know to downgrade version. how we can fix the issue in latest version and help community to not face this in future?

ludwig-pro commented 1 year ago

There are some breaking changes with the latest version. It's not possible to register to SpecReporter, WorkerAssignReporter, Detox 20.x don't expose them anymore.

CLI

If you need to take screenshots and logs of failing test, use the CLI :

how to configure detoxrc

.detoxrc.js

/** @type {Detox.DetoxConfig} */
module.exports = {
  testRunner: {
    forwardEnv: true, // <= HERE to have ENV variables
    args: {
      $0: "jest",
      config: "e2e/jest.config.js",
    },
    jest: {
      setupTimeout: 120000,
    },
  },
  //...

how to configure jest.config.js

/** @type {import('@jest/types').Config.InitialOptions} */
module.exports = {
  globalSetup: "detox/runners/jest/globalSetup",
  globalTeardown: "detox/runners/jest/globalTeardown",
  maxWorkers: 1,
  reporters: ["detox/runners/jest/reporter"],
  testEnvironment: "detox/runners/jest/testEnvironment", // <= use the classic test environment from jest
  testRegex: "\\.e2e\\.js$",
  testTimeout: 120000,
  verbose: true,
};
ludwig-pro commented 1 year ago

The last thing I don't understand with the example repo is why I still see the "first time opening" bottom sheet, disableOnboarding doesn't work anymore? Is there another query string?

http://localhost:19000/index.bundle?platform=${platform}&dev=true&minify=false&**disableOnboarding**=1

EDIT: I search and I found that,

          await device.launchApp({
          newInstance: true,
          launchArgs: {
            EXDevMenuIsOnboardingFinished: true,
          },
        });

douglowder indicates that android is not supported at the moment

https://github.com/expo/expo/pull/19024#issue-1365146332

KiwiKilian commented 1 year ago

Just for clarification, the URL param disableOnboarding=1 works fine form me on iOS and Android. launchArgs.EXDevMenuIsOnboardingFinished is currently iOS only. All of this is not a detox@20 problem.

What's actually a problem on detox@20 is the usage of process.env.DETOX_CONFIGURATION, see the new approach to accessing the config.

You can fix openApp like this:

  const { configurationName } = await resolveConfig();
  const [platform, target] = configurationName.split('.');