How to use the jest-snapshot.buildSnapshotResolver function in jest-snapshot

To help you get started, we’ve selected a few jest-snapshot examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jest-community / jest-editor-support / src / Snapshot.js View on Github external
});
        }
      },
    };

    traverse(fileNode, {
      enter: path => {
        const visitor = Visitors[path.node.type];
        if (visitor != null) {
          visitor(path, state, this._matchers);
        }
      },
    });

    // NOTE if no projectConfig is given the default resolver will be used
    const snapshotResolver = buildSnapshotResolver(this._projectConfig || {});
    const snapshotPath = snapshotResolver.resolveSnapshotPath(filePath);
    const snapshots = utils.getSnapshotData(snapshotPath, 'none').data;
    let lastParent = null;
    let count = 1;

    return state.found.map(snapshotNode => {
      const parents = snapshotNode.parents.filter(isValidParent);
      const innerAssertion = parents[parents.length - 1];

      if (lastParent !== innerAssertion) {
        lastParent = innerAssertion;
        count = 1;
      }

      const result = {
        content: undefined,
github facebook / jest / packages / jest-circus / src / legacy-code-todo-rewrite / jestAdapterInit.ts View on Github external
dispatch({
      name: 'include_test_location_in_result',
    });
  }

  // Jest tests snapshotSerializers in order preceding built-in serializers.
  // Therefore, add in reverse because the last added is the first tested.
  config.snapshotSerializers
    .concat()
    .reverse()
    .forEach(path => {
      addSerializer(localRequire(path));
    });

  const {expand, updateSnapshot} = globalConfig;
  const snapshotResolver = buildSnapshotResolver(config);
  const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath);
  const snapshotState = new SnapshotState(snapshotPath, {
    expand,
    getBabelTraverse,
    getPrettier,
    updateSnapshot,
  });
  setState({snapshotState, testPath});

  addEventHandler(handleSnapshotStateAfterRetry(snapshotState));

  // Return it back to the outer scope (test runner outside the VM).
  return {globals, snapshotState};
};
github qlik-oss / after-work.js / plugins / chai-plugin-snapshot / src / index.js View on Github external
let currentTestName = null;
    for (let i = lineno - 1; i >= 0; i -= 1) {
      const line = lines[i];
      const trimmed = line.trimLeft();
      if (trimmed.startsWith('it(') || trimmed.startsWith('it.only(')) {
        [, currentTestName] = line.match(/it.*\((.*),/);
        break;
      }
    }
    if (currentTestName === null) {
      throw new Error('Can not find current test name');
    }

    let { snapshotState } = runner.snapshotContexts.get(filename) || {};
    if (!snapshotState) {
      const snapshotResolver = buildSnapshotResolver({
        rootDir: process.cwd(),
      });
      const snapshotPath = snapshotResolver.resolveSnapshotPath(filename);
      snapshotState = new SnapshotState(snapshotPath, {
        updateSnapshot: runner.argv.updateSnapshot ? 'all' : 'new',
      });
      runner.snapshotContexts.set(filename, { currentTestName, snapshotState });
    }
    const matcher = toMatchSnapshot.bind({
      snapshotState,
      currentTestName,
    });
    const result = matcher(this._obj);
    this.assert(
      result.pass,
      result.message,