<commit_before>import env from 'gulp-env';
import istanbul from 'gulp-babel-istanbul';
import tape from 'gulp-tape';
import gulpif from 'gulp-if';
import gutil from 'gulp-util';
import tapSpec from 'tap-spec';

import gulpOptionsBuilder from './gulp-options-builder';

function setupJsdom () {
  // Configure JSDOM and set global variables
  // to simulate a browser environment for tests.
  const exposedProperties = ['window', 'navigator', 'document'];

  global.document = require('jsdom').jsdom('');
  global.window = document.defaultView;
  Object.keys(document.defaultView).forEach((property) => {
    if (typeof global[property] === 'undefined') {
      exposedProperties.push(property);
      global[property] = document.defaultView[property];
    }
  });

  global.navigator = {
    userAgent: 'node.js'
  };
}
export function testTasks (gulp, opts) {

  const envs = env.set({
    NODE_ENV: 'test'
  });

  const runSequence = require('run-sequence').use(gulp);

  const options = gulpOptionsBuilder(opts);

  let watch;

  gulp.task('test', () => {
    setupJsdom();
    if (watch) {
      process.env.NODE_ENV = 'test';
    }
    if (options.testPaths) {
      setupJsdom();
      return gulp.src(options.testPaths)
        .pipe(gulpif(!watch, envs))
        .pipe(tape({
          reporter: tapSpec(),
          bail: true
        }))
        .on('error', (error) => {
          gutil.log(error.message);
          process.exit(1);
        })
        .pipe(gulpif(!watch, envs.reset));
    }
  });

  gulp.task('test:watcher', () =>
    gulp.watch([...options.testPaths, ...options.jsAssets], ['test'])
  );

  gulp.task('test:watch', () => {
    watch = true;
    runSequence('test', 'test:watcher');
  });

  gulp.task('test:coverage', (done) => {
    setupJsdom();
    if (options.testPaths) {
      setupJsdom();
      gulp.src(options.jsAssets)
        .pipe(istanbul({
          instrumenter: require('isparta').Instrumenter,
          includeUntested: true<commit_msg>Fix: setupJsdom() being called twice<commit_after>import env from 'gulp-env';
import istanbul from 'gulp-babel-istanbul';
import tape from 'gulp-tape';
import gulpif from 'gulp-if';
import gutil from 'gulp-util';
import tapSpec from 'tap-spec';

import gulpOptionsBuilder from './gulp-options-builder';

function setupJsdom () {
  // Configure JSDOM and set global variables
  // to simulate a browser environment for tests.
  const exposedProperties = ['window', 'navigator', 'document'];

  global.document = require('jsdom').jsdom('');
  global.window = document.defaultView;
  Object.keys(document.defaultView).forEach((property) => {
    if (typeof global[property] === 'undefined') {
      exposedProperties.push(property);
      global[property] = document.defaultView[property];
    }
  });

  global.navigator = {
    userAgent: 'node.js'
  };
}
export function testTasks (gulp, opts) {

  const envs = env.set({
    NODE_ENV: 'test'
  });

  const runSequence = require('run-sequence').use(gulp);

  const options = gulpOptionsBuilder(opts);

  let watch;

  gulp.task('test', () => {
    if (watch) {
      process.env.NODE_ENV = 'test';
    }
    if (options.testPaths) {
      setupJsdom();
      return gulp.src(options.testPaths)
        .pipe(gulpif(!watch, envs))
        .pipe(tape({
          reporter: tapSpec(),
          bail: true
        }))
        .on('error', (error) => {
          gutil.log(error.message);
          process.exit(1);
        })
        .pipe(gulpif(!watch, envs.reset));
    }
  });

  gulp.task('test:watcher', () =>
    gulp.watch([...options.testPaths, ...options.jsAssets], ['test'])
  );

  gulp.task('test:watch', () => {
    watch = true;
    runSequence('test', 'test:watcher');
  });

  gulp.task('test:coverage', (done) => {
    if (options.testPaths) {
      setupJsdom();
      gulp.src(options.jsAssets)
        .pipe(istanbul({
          instrumenter: require('isparta').Instrumenter,
          includeUntested: true
{'fraction_non_alphanumeric': 0.16368221941992434, 'fraction_numerical': 0.0005044136191677175, 'mean_word_length': 2.9779338014042125, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6054297', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1276, 'n_words': 253}