How to use the jest-snapshot.SnapshotState 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 suchipi / chai-jest-snapshot / src / spec / matchSnapshot.spec.js View on Github external
beforeEach(function() {
    // clear out workspace
    rimraf.sync(workspacePath("*"));
    // create the snapshot file that is considered "existing" by these tests
    const existingSnapshotState = new SnapshotState(undefined, {
      snapshotPath: EXISTING_SNAPSHOT_PATH,
      updateSnapshot: "all",
    });
    existingSnapshotState.match(EXISTING_SNAPSHOT_NAME, tree, EXISTING_SNAPSHOT_NAME);
    existingSnapshotState.save();

    object = undefined;
    snapshotFilename = undefined;
    snapshotName = undefined;
    update = false;
    utils = { flag: () => undefined };
  });
github ezcater / sosia / packages / sosia / change-tracker.ts View on Github external
export default ({cachePath, updateSnapshot}: {cachePath: string; updateSnapshot: boolean}) => {
  if (!cacheMap.has(cachePath)) {
    cacheMap.set(cachePath, new SnapshotState(cachePath, {updateSnapshot: updateSnapshot} as any));
  }

  const snapshotState = cacheMap.get(cachePath);

  const markAsDirty = (key: string) => {
    snapshotState._dirty = true;
    delete snapshotState._snapshotData[key];
  };

  const match = (page: NamedPage) => {
    const testName = page.name;
    const currentHash = hash(page);

    const {added, updated} = snapshotState;

    // run snapshot to see if there was a match
github Marwan01 / food-converter / node_modules / jest-jasmine2 / build / setup_jest_globals.js View on Github external
var _default = ({config, globalConfig, localRequire, testPath}) => {
  // 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 => {
      (0, _jestSnapshot.addSerializer)(localRequire(path));
    });
  patchJasmine();
  const expand = globalConfig.expand,
    updateSnapshot = globalConfig.updateSnapshot;
  const snapshotResolver = (0, _jestSnapshot.buildSnapshotResolver)(config);
  const snapshotPath = snapshotResolver.resolveSnapshotPath(testPath);
  const snapshotState = new _jestSnapshot.SnapshotState(snapshotPath, {
    expand,
    getBabelTraverse: () => require('@babel/traverse').default,
    getPrettier: () =>
      config.prettierPath ? require(config.prettierPath) : null,
    updateSnapshot
  });
  (0, _expect.setState)({
    snapshotState,
    testPath
  }); // Return it back to the outer scope (test runner outside the VM).

  return snapshotState;
};
github americanexpress / jest-image-snapshot / __tests__ / integration.spec.js View on Github external
it('does not write a result image for passing tests in update mode by default', () => {
      const updateModeMatcher = toMatchImageSnapshot.bind({
        snapshotState: new SnapshotState(__filename, {
          updateSnapshot: 'all',
        }),
        testPath: __filename,
      });
      updateModeMatcher(updateImageData, {
        customSnapshotIdentifier,
        failureThreshold: 2,
        failureThresholdType: 'pixel',
      });
      expect(fs.readFileSync(updateImageSnapshotPath)).toEqual(imageData);
    });
github americanexpress / jest-image-snapshot / __tests__ / integration.spec.js View on Github external
it('writes a result image for failing test in update mode by default', () => {
      const updateModeMatcher = toMatchImageSnapshot.bind({
        snapshotState: new SnapshotState(__filename, {
          updateSnapshot: 'all',
        }),
        testPath: __filename,
      });
      updateModeMatcher(updateImageData, {
        customSnapshotIdentifier,
        failureThreshold: 0,
        failureThresholdType: 'pixel',
      });
      expect(fs.readFileSync(updateImageSnapshotPath)).toEqual(updateImageData);
    });
github kentcdodds / slice-js / integration / scripts / process-coverage.js View on Github external
.forEach(({filepath, slice}) => {
    const update = false
    const sliceshotsPath = filepath.replace(
      'node_modules',
      '__tests__/__sliceshots__',
    )
    const snapshotPath = `${sliceshotsPath}.slice`
    const expand = false
    const state = new SnapshotState(filepath, update, snapshotPath, expand)
    state.update = true
    const matchResult = state.match(filepath, slice, filepath)
    const saveResult = state.save(true)
    console.log({matchResult, saveResult})
  })
github qlik-oss / after-work.js / plugins / chai-plugin-snapshot / src / index.js View on Github external
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,
      result.message,
      result.expected,
      result.report,
    );
github styleguidist / snapguidist / src / core / snapshot.js View on Github external
const snapshot = (name, tree, update) => {
  const snapshotPath = path.resolve(base, `${name}.sg`)
  const state = new SnapshotState(null, {
    snapshotPath,
    updateSnapshot: update ? 'all' : 'new',
  })

  setTreeTypes(tree)

  const result = state.match({
    testName: name,
    received: tree,
  })

  state.save()

  if (!result.pass) {
    result.diff = stripAnsi(diff(result.expected, result.actual))
    cleanUp(result)
github suchipi / chai-jest-snapshot / src / buildMatchSnapshot.js View on Github external
return function matchSnapshot(...args) {
    const { snapshotFilename, snapshotName, update, ci } = parseArgs(args);

    if (utils.flag(this, 'negate')) {
      throw new Error("`matchSnapshot` cannot be used with `.not`.");
    }

    const obj = this._obj;
    const absolutePathToSnapshot = path.resolve(snapshotFilename);
    const snapshotState = new SnapshotState(undefined, {
      updateSnapshot: ci ? "none" : (update ? "all" : "new"),
      snapshotPath: absolutePathToSnapshot,
    });

    const match = snapshotState.match(snapshotName, obj, snapshotName);
    const actual = match.actual || "";
    const expected = match.expected || "";
    snapshotState.save();

    this.assert(
      match.pass,
      `expected value to match snapshot ${snapshotName}`,
      `expected value to not match snapshot ${snapshotName}`,
      expected.trim(),
      actual.trim(),
      true
github mAAdhaTTah / brookjs / packages / brookjs-cli / features / support / steps.ts View on Github external
toMatchSnapshot(actual: string, filename: string, testname: string) {
      const snapshotState = new SnapshotState(filename, {
        updateSnapshot: process.env.SNAPSHOT_UPDATE ? 'all' : 'new'
      } as any);

      const result = toMatchSnapshot.call(
        {
          snapshotState,
          currentTestName: testname
        } as any,
        actual
      );

      snapshotState.save();

      return result;
    }
  });