Skip to content

Fix tests failing in async mode

Illya Klymov requested to merge xanf-vtu-30-sync into master

What does this MR do?

Closes #193491 (closed)

In &2291 (closed) we've created multiple MR to address various issues to address failing tests due to async nature of new @vue/test-utils

This MR is intended to cover remaining issues after all other MRs will be merged.

MR consists of 3 commits:

  1. Adding sync: false to all existing tests This is done via following codemod:
Codemod
function processMountNode(node, j, report) {
  if (!node.arguments[1]) {
    node.arguments.push(
      j.objectExpression([j.property('init', j.identifier('sync'), j.literal(false))]),
    );
  } else if (node.arguments[1].type === 'Identifier') {
    return node;
  } else {
    const hasSync = node.arguments[1].properties.find(p => p.key && p.key.name === 'sync');

    if (!hasSync) {
      node.arguments[1].properties.unshift(
        j.property('init', j.identifier('sync'), j.literal(false)),
      );
      report();
    }
  }
  return node;
}

export default (fileInfo, api) => {
  const j = api.jscodeshift;
  const root = j(fileInfo.source);

  const importDeclaration = root.find(j.ImportDeclaration, {
    source: {
      type: 'Literal',
      value: '@vue/test-utils',
    },
  });

  // get the local name for the imported module
  const shallowMount = importDeclaration.find(j.Identifier, { name: 'shallowMount' });
  const mount = importDeclaration.find(j.Identifier, { name: 'mount' });

  let chain = root;
  [shallowMount, mount]
    .filter(decl => decl.length)
    .map(decl => decl.get(0).node.name)
    .forEach(decl => {
      chain = chain
        .find(j.CallExpression, {
          callee: {
            type: 'Identifier',
            name: decl,
          },
        })
        .replaceWith(nodePath =>
          processMountNode(nodePath.node, j, () => console.log('--- ', fileInfo.path)),
        );
    });

  return chain.toSource({ quote: 'single', trailingComma: true });
};

jscodeshift -t vtu.js ee/spec/frontend spec/frontend

  1. Fixing failing tests. If you want to test it locally, you will need all open MRs for &2291 (closed) to be merged. I've prepared branch xanf-vtu-30-merge-all, so you can do:

git checkout xanf-vtu-30-merge-all; git cherry-pick 0ee1f6ff650c924a508b72a516edd680c009a4f8 to run tests locally

  1. Reverting commit, made on step 1 so we're sure that we do not create additional mess in MR

Conformity

Edited by Illya Klymov

Merge request reports