3

I'm using detox with cucumber and the setup works great until the last moment of calling AfterAll hooks. In the hook I do the following:

AfterAll(async () => {
  console.log('CLEANING DETOX');
  await detox.cleanup();
});

To run cucumber I use the following script from package.json:

"bdd": "cucumber-js --require-module @babel/register",

The problem happens when I interrupt cucumber run for any reason. AfterAll hook won't run in this case and detox won't clear its stale files. Which wouldn't be a problem but detox is using ~/Library/Detox/device.registry.state.lock file to track which simulators are being used by runner. Essentially, this leads to detox constantly launching new simulator devices as this file never gets cleared. I thought, I could just create a simple wrapper script:

const { execSync } = require('child_process');
const detox = require('detox');

const stdout = execSync('cucumber-js --require-module @babel/register');
process.on('SIGINT', async function () {
  console.log('Cleanig up detox');
  await detox.cleanup();
});
console.log(stdout);

However, that didn't work either as detox.cleanup() only removes the file when it has detox.device setup. Which happens in BeforeAll hook:

BeforeAll({ timeout: 120 * 1000 }, async () => {
  const detoxConfig = { selectedConfiguration: 'ios.debug2' };
  // console.log('CONFIG::', detoxConfig);
  await detox.init(detoxConfig);
  await detox.device.launchApp();
});

My only idea left is to manually clear the file - I should be able to grab lock file path from detox internal somehow - my worry about this approach is tight dependency on detox implementation. Which is why I would rather call detox.cleanup.

EDIT: Ended up doing this workaround for now:

BeforeAll({ timeout: 120 * 1000 }, async () => {
  await startDetox();
});
async function startDetox() {
  const detoxConfig = { selectedConfiguration: 'ios.debug' };

  const lockFile = getDeviceLockFilePathIOS();
  unlink(lockFile, (err) => {
    if (err) {
      console.debug('Lock file was not deleted:::', err);
    }
  });
  await detox.init(detoxConfig);
  await detox.device.launchApp();
}

Wonder if anyone has a better idea ?

mfilimonov
  • 587
  • 4
  • 18
  • Posted issue on detox GH page - https://github.com/wix/Detox/issues/3318. Maybe will be resolved there ? – mfilimonov Apr 11 '22 at 09:28
  • Hi, through the post on GH, I noticed you use jest-circus as Detox's underlying test runner. What sense is there in calling detox's init and cleanup manually, then? (should be done automatically in a jest-circus env). Also, 2¢ - Detox clears out the lock file in each fresh run – d4vidi Apr 11 '22 at 11:16
  • @d4vidi Thanks for the comment! I assumed that jest-circus is the default env. Is it not? How do I check then which env detox is running? I did notice that detox doesn't clear lock file when second init is called do I need to pass something extra config for that? – mfilimonov Apr 12 '22 at 07:05

2 Answers2

0

Have you tried adding

detox clean-framework-cache && detox build-framework-cache into your command like so:

detox clean-framework-cache && detox build-framework-cache && cucumber-js --require-module @babel/register

This is a detox command that clears the cache and resets it so detox starts from fresh every time, it barely adds any time to the execution time so can be run on every test and means the first thing the build does before starting the simulator is clear the cache and start from fresh. I had the exact same issue with our cucumber detox builds and this fixed it for us.

Richard C
  • 513
  • 1
  • 7
  • 26
  • @AlexanderL.Hayes `This does not really answer the question`, what does it really do then? It looks like an answer to me, seen the fact it's the only answer so far after 8 months, I would give it a go though .. – dbf Dec 23 '22 at 19:19
  • (My bad, looks like I misclicked when reviewing) Supporting information or explanation might improve this answer. I'm usually skeptical of answers that amount to: "*just type these in your terminal*," these answers tend to be better as a comment. – Alexander L. Hayes Dec 23 '22 at 19:59
  • Added detail, answered the question while I was hunting slack for answers to my own issue and stumbled on this. – Richard C Dec 25 '22 at 11:46
0

After fiddling with workaround for way too long I ended up migrating to jest-cucumber. It manages to work way better with detox and doesn't require all this workarounds.

mfilimonov
  • 587
  • 4
  • 18