Пример #1
0
  it('works with escaped regex', () => {
    // Write the first snapshot
    let result = runJest('snapshot-escape', [
      '-w=1',
      '--ci=false',
      'snapshotEscapeRegex.js',
    ]);
    let stderr = result.stderr;

    expect(stderr).toMatch('2 snapshots written from 1 test suite.');
    expect(result.status).toBe(0);
    expect(wrap(extractSummary(stderr).summary)).toMatchSnapshot();

    result = runJest('snapshot-escape', [
      '-w=1',
      '--ci=false',
      'snapshotEscapeRegex.js',
    ]);
    stderr = result.stderr;

    // Make sure we aren't writing a snapshot this time which would
    // indicate that the snapshot couldn't be loaded properly.
    expect(stderr).not.toMatch('Snapshot Summary');
    expect(wrap(extractSummary(stderr).summary)).toMatchSnapshot();
    expect(result.status).toBe(0);
  });
Пример #2
0
  it('works with template literal substitutions', () => {
    // Write the first snapshot
    let result = runJest('snapshot-escape', [
      '-w=1',
      '--ci=false',
      'snapshotEscapeSubstitution.test.js',
    ]);
    let stderr = result.stderr;

    expect(stderr).toMatch('1 snapshot written');
    expect(result.status).toBe(0);
    expect(wrap(extractSummary(stderr).summary)).toMatchSnapshot();

    result = runJest('snapshot-escape', [
      '-w=1',
      '--ci=false',
      'snapshotEscapeSubstitution.test.js',
    ]);
    stderr = result.stderr;

    // Make sure we aren't writing a snapshot this time which would
    // indicate that the snapshot couldn't be loaded properly.
    expect(stderr).not.toMatch('1 snapshot written');
    expect(wrap(extractSummary(stderr).summary)).toMatchSnapshot();
    expect(result.status).toBe(0);
  });
test('show error message with matching files', () => {
  const {status, stderr} = runJest('resolve-no-extensions');
  const {rest} = extractSummary(stderr);

  expect(status).toBe(1);
  expect(wrap(rest)).toMatchSnapshot();
});
test('show error message when no js moduleFileExtensions', () => {
  writeFiles(DIR, {
    'index.jsx': `
      module.exports ={found: true};
    `,
    'package.json': `
      {
        "jest": {
          "moduleFileExtensions": ["jsx"]
        }
      }
    `,
    'test.jsx': `
      const m = require('../');

      test('some test', () => {
        expect(m.found).toBe(true);
      });
    `,
  });

  const {status, stderr} = runJest('resolve-no-extensions-no-js');

  expect(status).toBe(1);
  expect(wrap(stderr)).toMatchSnapshot();
});
Пример #5
0
it('instruments and collects coverage for typescript files', () => {
  const dir = path.resolve(__dirname, '../typescript-coverage');
  run('yarn', dir);
  const {stdout} = runJest(dir, ['--coverage', '--no-cache'], {
    stripAnsi: true,
  });
  expect(wrap(stdout)).toMatchSnapshot();
});
Пример #6
0
test('json reporter printing with --coverage', () => {
  const {stderr, status} = runJest('json-reporter', ['--coverage'], {
    stripAnsi: true,
  });
  const {summary} = extractSummary(stderr);
  expect(status).toBe(1);
  expect(wrap(summary)).toMatchSnapshot();
});
Пример #7
0
    it('works on subsequent runs without `-u`', () => {
      const firstRun = runWithJson('snapshot', ['-w=1', '--ci=false']);

      // $FlowFixMe dynamic require
      const content = require(snapshotOfCopy);
      expect(content).not.toBe(undefined);
      const secondRun = runWithJson('snapshot', []);

      expect(firstRun.json.numTotalTests).toBe(9);
      expect(secondRun.json.numTotalTests).toBe(9);
      expect(secondRun.json.success).toBe(true);

      expect(firstRun.stderr).toMatch('9 snapshots written from 3 test suites');
      expect(secondRun.stderr).toMatch('9 passed, 9 total');
      expect(wrap(extractSummary(firstRun.stderr).summary)).toMatchSnapshot();
      expect(wrap(extractSummary(secondRun.stderr).summary)).toMatchSnapshot();
    });
Пример #8
0
  it('prints a stack trace for matching errors', () => {
    const {status, stderr} = runJest('stack-trace', ['stackTrace.test.js']);

    expect(wrap(extractSummary(stderr).summary)).toMatchSnapshot();
    expect(status).toBe(1);

    expect(stderr).toMatch(/\s+at\s(?:.+?)\s\(__tests__\/stackTrace.test\.js/);
  });
Пример #9
0
  it('works with escaped characters', () => {
    // Write the first snapshot
    let result = runJest('snapshot-escape', [
      '-w=1',
      '--ci=false',
      'snapshot.test.js',
    ]);
    let stderr = result.stderr;

    expect(stderr).toMatch('1 snapshot written');
    expect(result.status).toBe(0);
    expect(wrap(extractSummary(stderr).summary)).toMatchSnapshot();

    // Write the second snapshot
    const testData =
      `test('escape strings two', () => expect('two: \\\'\"').` +
      `toMatchSnapshot());`;
    const newTestData = initialTestData + testData;
    fs.writeFileSync(snapshotEscapeTestFile, newTestData, 'utf8');

    result = runJest('snapshot-escape', [
      '-w=1',
      '--ci=false',
      '--updateSnapshot',
      'snapshot.test.js',
    ]);
    stderr = result.stderr;

    expect(stderr).toMatch('1 snapshot written');
    expect(wrap(extractSummary(stderr).summary)).toMatchSnapshot();
    expect(result.status).toBe(0);

    // Now let's check again if everything still passes.
    // If this test doesn't pass, some snapshot data was not properly escaped.
    result = runJest('snapshot-escape', [
      '-w=1',
      '--ci=false',
      'snapshot.test.js',
    ]);
    stderr = result.stderr;

    expect(stderr).not.toMatch('Snapshot Summary');
    expect(wrap(extractSummary(stderr).summary)).toMatchSnapshot();
    expect(result.status).toBe(0);
  });
Пример #10
0
test('generates coverage when using the testRegex config param ', () => {
  const {stdout, status} = runJest(DIR, [
    '--no-cache',
    '--testRegex=__tests__',
    '--coverage',
  ]);
  expect(wrap(stdout)).toMatchSnapshot();
  expect(status).toBe(0);
});
Пример #11
0
    it('deletes a snapshot when a test does removes all the snapshots', () => {
      const firstRun = runWithJson('snapshot', ['-w=1', '--ci=false']);

      fs.writeFileSync(copyOfTestPath, emptyTest);
      const secondRun = runWithJson('snapshot', ['-w=1', '--ci=false', '-u']);
      fs.unlinkSync(copyOfTestPath);

      expect(firstRun.json.numTotalTests).toBe(9);
      expect(secondRun.json.numTotalTests).toBe(6);

      expect(fileExists(snapshotOfCopy)).toBe(false);
      expect(firstRun.stderr).toMatch('9 snapshots written from 3 test suites');
      expect(secondRun.stderr).toMatch(
        '1 snapshot file removed from 1 test suite',
      );
      expect(wrap(extractSummary(firstRun.stderr).summary)).toMatchSnapshot();
      expect(wrap(extractSummary(secondRun.stderr).summary)).toMatchSnapshot();
    });
test('CLI accepts exact file names if matchers matched', () => {
  writeFiles(DIR, {
    'foo/bar.spec.js': `
      test('foo', () => {});
    `,
    'package.json': JSON.stringify({jest: {testEnvironment: 'node'}}),
  });

  const result = runJest(DIR, ['-i', '--forceExit', './foo/bar.spec.js']);

  expect(result.status).toBe(0);

  const {rest, summary} = extractSummary(result.stderr);

  expect(wrap(rest)).toMatchSnapshot();
  expect(wrap(summary)).toMatchSnapshot();
  expect(result.stdout).toBe('');
});
Пример #13
0
test('does not output coverage report when html is requested', () => {
  const {stdout, status} = runJest(
    DIR,
    ['--no-cache', '--coverage', '--coverageReporters=html'],
    {stripAnsi: true},
  );
  expect(status).toBe(0);
  expect(stdout).toMatch(/^$/);
  expect(wrap(stdout)).toMatchSnapshot();
});
Пример #14
0
test('testNamePattern', () => {
  const {stderr, status} = runWithJson('test-name-pattern', [
    '--testNamePattern',
    'should match',
  ]);
  const {summary} = extractSummary(stderr);

  expect(status).toBe(0);
  expect(wrap(summary)).toMatchSnapshot();
});
Пример #15
0
test('outputs coverage report when text-summary is requested', () => {
  const {stdout, status} = runJest(
    DIR,
    ['--no-cache', '--coverage', '--coverageReporters=text-summary'],
    {stripAnsi: true},
  );
  expect(status).toBe(0);
  expect(stdout).toMatch(/Coverage summary/);
  expect(wrap(stdout)).toMatchSnapshot();
});
Пример #16
0
it('prints out info about open handlers from inside tests', async () => {
  const {stderr} = await until(
    'detect-open-handles',
    ['inside', '--detectOpenHandles'],
    'Jest has detected',
  );
  const textAfterTest = getTextAfterTest(stderr);

  expect(wrap(textAfterTest)).toMatchSnapshot();
});
Пример #17
0
it('prints message about flag on forceExit', async () => {
  const {stderr} = await until(
    'detect-open-handles',
    ['outside', '--forceExit'],
    'Force exiting Jest',
  );
  const textAfterTest = getTextAfterTest(stderr);

  expect(wrap(textAfterTest)).toMatchSnapshot();
});
Пример #18
0
it('prints message about flag on slow tests', async () => {
  const {stderr} = await until(
    'detect-open-handles',
    ['outside'],
    'Jest did not exit one second after the test run has completed.',
  );
  const textAfterTest = getTextAfterTest(stderr);

  expect(wrap(textAfterTest)).toMatchSnapshot();
});
Пример #19
0
    it('deletes the snapshot if the test suite has been removed', () => {
      const firstRun = runWithJson('snapshot', ['-w=1', '--ci=false']);
      fs.unlinkSync(copyOfTestPath);

      // $FlowFixMe dynamic require
      const content = require(snapshotOfCopy);
      expect(content).not.toBe(undefined);
      const secondRun = runWithJson('snapshot', ['-w=1', '--ci=false', '-u']);

      expect(firstRun.json.numTotalTests).toBe(9);
      expect(secondRun.json.numTotalTests).toBe(5);
      expect(fileExists(snapshotOfCopy)).toBe(false);

      expect(firstRun.stderr).toMatch('9 snapshots written from 3 test suites');
      expect(secondRun.stderr).toMatch(
        '1 snapshot file removed from 1 test suite',
      );
      expect(wrap(extractSummary(firstRun.stderr).summary)).toMatchSnapshot();
      expect(wrap(extractSummary(secondRun.stderr).summary)).toMatchSnapshot();
    });
Пример #20
0
    it('does not save snapshots in CI mode by default', () => {
      const result = runWithJson('snapshot', ['-w=1', '--ci=true']);

      expect(result.json.success).toBe(false);
      expect(result.json.numTotalTests).toBe(9);
      expect(result.json.snapshot.added).toBe(0);
      expect(result.json.snapshot.total).toBe(9);
      const {rest, summary} = extractSummary(result.stderr);

      expect(rest).toMatch('New snapshot was not written');
      expect(wrap(summary)).toMatchSnapshot();
    });
test('prints useful error for environment methods after test is done', () => {
  const {stderr} = runJest('environment-after-teardown');
  const interestingLines = stderr
    .split('\n')
    .slice(9, 18)
    .join('\n');

  expect(wrap(interestingLines)).toMatchSnapshot();
  expect(stderr.split('\n')[9]).toBe(
    'ReferenceError: You are trying to access a property or method of the Jest environment after it has been torn down.',
  );
});
Пример #22
0
test('prints useful error for requires after test is done', () => {
  const {stderr} = runJest('require-after-teardown');

  const interestingLines = stderr
    .split('\n')
    .slice(9, 18)
    .join('\n');

  expect(wrap(interestingLines)).toMatchSnapshot();
  expect(stderr.split('\n')[19]).toMatch(
    new RegExp('(__tests__/lateRequire.test.js:11:20)'),
  );
});
Пример #23
0
  it('prints a stack trace for errors without message in stack trace', () => {
    const {status, stderr} = runJest('stack-trace', [
      'stackTraceWithoutMessage.test.js',
    ]);

    expect(wrap(extractSummary(stderr).summary)).toMatchSnapshot();
    expect(status).toBe(1);

    expect(stderr).toMatch(/important message/);
    expect(stderr).toMatch(
      /\s+at\s(?:.+?)\s\(__tests__\/stackTraceWithoutMessage.test\.js/,
    );
  });
Пример #24
0
  it('does not print a stack trace for matching errors when --noStackTrace is given', () => {
    const {status, stderr} = runJest('stack-trace', [
      'stackTrace.test.js',
      '--noStackTrace',
    ]);

    expect(wrap(extractSummary(stderr).summary)).toMatchSnapshot();
    expect(status).toBe(1);

    expect(stderr).not.toMatch(
      /\s+at\s(?:.+?)\s\(__tests__\/stackTrace.test\.js/,
    );
  });
Пример #25
0
  it('prints a stack trace for runtime errors', () => {
    const {status, stderr} = runJest('stack-trace', ['runtimeError.test.js']);

    expect(wrap(extractSummary(stderr).summary)).toMatchSnapshot();

    expect(status).toBe(1);
    expect(stderr).toMatch(
      /ReferenceError: thisIsARuntimeError is not defined/,
    );
    expect(stderr).toMatch(/> 10 \| thisIsARuntimeError\(\);/);
    expect(stderr).toMatch(
      /\s+at\s(?:.+?)\s\(__tests__\/runtimeError.test\.js/,
    );
  });
Пример #26
0
test('outputs coverage report when text is requested', () => {
  const {stdout, status} = runJest(
    DIR,
    [
      '--no-cache',
      '--coverage',
      '--coverageReporters=text',
      '--coverageReporters=html',
    ],
    {stripAnsi: true},
  );
  expect(status).toBe(0);
  expect(stdout).toMatch(/Stmts | . Branch/);
  expect(wrap(stdout)).toMatchSnapshot();
});
Пример #27
0
test('collects coverage only from specified file', () => {
  const {stdout} = runJest(
    DIR,
    [
      '--no-cache',
      '--coverage',
      '--collectCoverageFrom', // overwrites the one in package.json
      'file.js',
    ],
    {stripAnsi: true},
  );

  // Coverage report should only have `file.js` coverage info
  expect(wrap(stdout)).toMatchSnapshot();
});
Пример #28
0
    it('updates the snapshot when a test removes some snapshots', () => {
      const firstRun = runWithJson('snapshot', ['-w=1', '--ci=false']);
      fs.unlinkSync(copyOfTestPath);
      const beforeRemovingSnapshot = getSnapshotOfCopy();

      fs.writeFileSync(
        copyOfTestPath,
        originalTestContent.replace(
          '.toMatchSnapshot()',
          '.not.toBe(undefined)',
        ),
      );
      const secondRun = runWithJson('snapshot', ['-w=1', '--ci=false', '-u']);
      fs.unlinkSync(copyOfTestPath);

      expect(firstRun.json.numTotalTests).toBe(9);
      expect(secondRun.json.numTotalTests).toBe(9);
      expect(fileExists(snapshotOfCopy)).toBe(true);
      const afterRemovingSnapshot = getSnapshotOfCopy();

      expect(Object.keys(beforeRemovingSnapshot).length - 1).toBe(
        Object.keys(afterRemovingSnapshot).length,
      );
      const keyToCheck =
        'snapshot works with plain objects and the ' +
        'title has `escape` characters 2';
      expect(beforeRemovingSnapshot[keyToCheck]).not.toBe(undefined);
      expect(afterRemovingSnapshot[keyToCheck]).toBe(undefined);

      expect(wrap(extractSummary(firstRun.stderr).summary)).toMatchSnapshot();
      expect(firstRun.stderr).toMatch('9 snapshots written from 3 test suites');

      expect(wrap(extractSummary(secondRun.stderr).summary)).toMatchSnapshot();
      expect(secondRun.stderr).toMatch('1 snapshot updated from 1 test suite');
      expect(secondRun.stderr).toMatch('1 snapshot removed from 1 test suite');
    });
Пример #29
0
  it('does not print a stack trace for runtime errors when --noStackTrace is given', () => {
    const {status, stderr} = runJest('stack-trace', [
      'runtimeError.test.js',
      '--noStackTrace',
    ]);

    expect(wrap(extractSummary(stderr).summary)).toMatchSnapshot();
    expect(status).toBe(1);

    expect(stderr).toMatch(
      /ReferenceError: thisIsARuntimeError is not defined/,
    );
    expect(stderr).not.toMatch(
      /\s+at\s(?:.+?)\s\(__tests__\/runtimeError.test\.js/,
    );
  });
Пример #30
0
test('collects coverage only from multiple specified files', () => {
  const {stdout} = runJest(
    DIR,
    [
      '--no-cache',
      '--coverage',
      '--collectCoverageFrom',
      'file.js',
      '--collectCoverageFrom',
      'otherFile.js',
    ],
    {stripAnsi: true},
  );

  expect(wrap(stdout)).toMatchSnapshot();
});