Cleaning up detox in cucumber run

Viewed 120

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 ?

0 Answers
Related