Skip to content

Commit

Permalink
chore: use ESM in integration tests (#7114)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Oct 7, 2018
1 parent daafad0 commit e3f0b43
Show file tree
Hide file tree
Showing 127 changed files with 404 additions and 420 deletions.
9 changes: 2 additions & 7 deletions TestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const DEFAULT_PROJECT_CONFIG: ProjectConfig = {
watchPathIgnorePatterns: [],
};

const makeGlobalConfig = (overrides: Object = {}): GlobalConfig => {
export const makeGlobalConfig = (overrides: Object = {}): GlobalConfig => {
const overridesKeys = new Set(Object.keys(overrides));
Object.keys(DEFAULT_GLOBAL_CONFIG).forEach(key => overridesKeys.delete(key));

Expand All @@ -133,7 +133,7 @@ const makeGlobalConfig = (overrides: Object = {}): GlobalConfig => {
return Object.assign({}, DEFAULT_GLOBAL_CONFIG, overrides);
};

const makeProjectConfig = (overrides: Object = {}): ProjectConfig => {
export const makeProjectConfig = (overrides: Object = {}): ProjectConfig => {
const overridesKeys = new Set(Object.keys(overrides));
Object.keys(DEFAULT_PROJECT_CONFIG).forEach(key => overridesKeys.delete(key));

Expand All @@ -146,8 +146,3 @@ const makeProjectConfig = (overrides: Object = {}): ProjectConfig => {

return Object.assign({}, DEFAULT_PROJECT_CONFIG, overrides);
};

module.exports = {
makeGlobalConfig,
makeProjectConfig,
};
49 changes: 20 additions & 29 deletions e2e/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

import type {Path} from 'types/Config';

const {sync: spawnSync} = require('execa');
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const rimraf = require('rimraf');
import {sync as spawnSync} from 'execa';
import fs from 'fs';
import path from 'path';
import mkdirp from 'mkdirp';
import rimraf from 'rimraf';

const run = (cmd: string, cwd?: Path) => {
export const run = (cmd: string, cwd?: Path) => {
const args = cmd.split(/\s/).slice(1);
const spawnOptions = {cwd, reject: false};
const result = spawnSync(cmd.split(/\s/)[0], args, spawnOptions);
Expand All @@ -39,7 +39,7 @@ const run = (cmd: string, cwd?: Path) => {
return result;
};

const linkJestPackage = (packageName: string, cwd: Path) => {
export const linkJestPackage = (packageName: string, cwd: Path) => {
const packagesDir = path.resolve(__dirname, '../packages');
const packagePath = path.resolve(packagesDir, packageName);
const destination = path.resolve(cwd, 'node_modules/', packageName);
Expand All @@ -48,17 +48,17 @@ const linkJestPackage = (packageName: string, cwd: Path) => {
fs.symlinkSync(packagePath, destination, 'dir');
};

const makeTemplate = (str: string): ((values?: Array<any>) => string) => (
values: ?Array<any>,
) =>
export const makeTemplate = (
str: string,
): ((values?: Array<any>) => string) => (values: ?Array<any>) =>
str.replace(/\$(\d+)/g, (match, number) => {
if (!Array.isArray(values)) {
throw new Error('Array of values must be passed to the template.');
}
return values[number - 1];
});

const cleanup = (directory: string) => rimraf.sync(directory);
export const cleanup = (directory: string) => rimraf.sync(directory);

/**
* Creates a nested directory with files and their contents
Expand All @@ -70,7 +70,10 @@ const cleanup = (directory: string) => rimraf.sync(directory);
* }
* );
*/
const writeFiles = (directory: string, files: {[filename: string]: string}) => {
export const writeFiles = (
directory: string,
files: {[filename: string]: string},
) => {
mkdirp.sync(directory);
Object.keys(files).forEach(fileOrPath => {
const filePath = fileOrPath.split('/'); // ['tmp', 'a.js']
Expand All @@ -86,7 +89,7 @@ const writeFiles = (directory: string, files: {[filename: string]: string}) => {
});
};

const copyDir = (src: string, dest: string) => {
export const copyDir = (src: string, dest: string) => {
const srcStat = fs.lstatSync(src);
if (srcStat.isDirectory()) {
if (!fs.existsSync(dest)) {
Expand All @@ -100,7 +103,7 @@ const copyDir = (src: string, dest: string) => {
}
};

const createEmptyPackage = (
export const createEmptyPackage = (
directory: Path,
packageJson?: {[keys: string]: any},
) => {
Expand All @@ -119,7 +122,7 @@ const createEmptyPackage = (
);
};

const extractSummary = (stdout: string) => {
export const extractSummary = (stdout: string) => {
const match = stdout.match(
/Test Suites:.*\nTests.*\nSnapshots.*\nTime.*(\nRan all test suites)*.*\n*$/gm,
);
Expand Down Expand Up @@ -148,12 +151,12 @@ const extractSummary = (stdout: string) => {
// different versions of Node print different stack traces. This function
// unifies their output to make it possible to snapshot them.
// TODO: Remove when we drop support for node 4
const cleanupStackTrace = (output: string) =>
export const cleanupStackTrace = (output: string) =>
output
.replace(/.*(?=packages)/g, ' at ')
.replace(/^.*at.*[\s][\(]?(\S*\:\d*\:\d*).*$/gm, ' at $1');

const normalizeIcons = (str: string) => {
export const normalizeIcons = (str: string) => {
if (!str) {
return str;
}
Expand All @@ -163,15 +166,3 @@ const normalizeIcons = (str: string) => {
.replace(new RegExp('\u00D7', 'g'), '\u2715')
.replace(new RegExp('\u221A', 'g'), '\u2713');
};

module.exports = {
cleanup,
copyDir,
createEmptyPackage,
extractSummary,
linkJestPackage,
makeTemplate,
normalizeIcons,
run,
writeFiles,
};
2 changes: 1 addition & 1 deletion e2e/__tests__/auto_clear_mocks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
'use strict';

const runJest = require('../runJest');
import runJest from '../runJest';

test('suite with auto-clear', () => {
const result = runJest('auto-clear-mocks/with-auto-clear');
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/auto_reset_mocks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
'use strict';

const runJest = require('../runJest');
import runJest from '../runJest';

test('suite with auto-reset', () => {
const result = runJest('auto-reset-mocks/with-auto-reset');
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/auto_restore_mocks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
'use strict';

const runJest = require('../runJest');
import runJest from '../runJest';

test('suite with auto-restore', () => {
const result = runJest('auto-restore-mocks/with-auto-restore');
Expand Down
8 changes: 4 additions & 4 deletions e2e/__tests__/babel_plugin_jest_hoist.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
*/
'use strict';

const path = require('path');
const {run} = require('../Utils');
const runJest = require('../runJest');
import path from 'path';
import {json as runWithJson} from '../runJest';
import {run} from '../Utils';

const DIR = path.resolve(__dirname, '..', 'babel-plugin-jest-hoist');

Expand All @@ -19,7 +19,7 @@ beforeEach(() => {
});

it('sucessfully runs the tests inside `babel-plugin-jest-hoist/`', () => {
const {json} = runJest.json(DIR, ['--no-cache', '--coverage']);
const {json} = runWithJson(DIR, ['--no-cache', '--coverage']);
expect(json.success).toBe(true);
expect(json.numTotalTestSuites).toBe(2);
});
2 changes: 1 addition & 1 deletion e2e/__tests__/bad_source_map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
'use strict';

const runJest = require('../runJest');
import runJest from '../runJest';

test('suite with test cases that contain malformed sourcemaps', () => {
const result = runJest('bad-source-map');
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/before-all-filtered.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
'use strict';

const runJest = require('../runJest');
import runJest from '../runJest';

describe('Correct BeforeAll run', () => {
it('ensures the BeforeAll of ignored suite is not run', () => {
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/before-each-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
'use strict';

const runJest = require('../runJest');
import runJest from '../runJest';

describe('Correct beforeEach order', () => {
it('ensures the correct order for beforeEach', () => {
Expand Down
8 changes: 4 additions & 4 deletions e2e/__tests__/clear_cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
*/
'use strict';

const fs = require('fs');
const os = require('os');
const path = require('path');
const runJest = require('../runJest');
import fs from 'fs';
import os from 'os';
import path from 'path';
import runJest from '../runJest';

const CACHE = path.resolve(os.tmpdir(), 'clear-cache-directory');

Expand Down
10 changes: 5 additions & 5 deletions e2e/__tests__/cli-handles-exact-filenames.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

'use strict';

const path = require('path');
const ConditionalTest = require('../../scripts/ConditionalTest');
const {extractSummary, cleanup, writeFiles} = require('../Utils');
const runJest = require('../runJest');
import path from 'path';
import {skipSuiteOnWindows} from '../../scripts/ConditionalTest';
import {cleanup, extractSummary, writeFiles} from '../Utils';
import runJest from '../runJest';

const DIR = path.resolve(__dirname, '../cli_accepts_exact_filenames');

ConditionalTest.skipSuiteOnWindows();
skipSuiteOnWindows();

beforeEach(() => cleanup(DIR));
afterAll(() => cleanup(DIR));
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/compare_dom_nodes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
'use strict';

const runJest = require('../runJest');
import runJest from '../runJest';

test('does not crash when expect involving a DOM node fails', () => {
const result = runJest('compare-dom-nodes');
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
'use strict';

const runJest = require('../runJest');
import runJest from '../runJest';

test('config as JSON', () => {
const result = runJest('verbose-reporter', [
Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/console.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

'use strict';

const {extractSummary} = require('../Utils');
const runJest = require('../runJest');
import {extractSummary} from '../Utils';
import runJest from '../runJest';

test('console printing', () => {
const {stderr, status} = runJest('console');
Expand Down
6 changes: 3 additions & 3 deletions e2e/__tests__/console_log_output_when_run_in_band.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

'use strict';

const path = require('path');
const {extractSummary, cleanup, writeFiles} = require('../Utils');
const runJest = require('../runJest');
import path from 'path';
import {cleanup, extractSummary, writeFiles} from '../Utils';
import runJest from '../runJest';

const DIR = path.resolve(__dirname, '../console_log_output_when_run_in_band');

Expand Down
8 changes: 4 additions & 4 deletions e2e/__tests__/coverage_remapping.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

'use strict';

const {readFileSync} = require('fs');
const path = require('path');
const {cleanup, run} = require('../Utils');
const runJest = require('../runJest');
import {readFileSync} from 'fs';
import path from 'path';
import {cleanup, run} from '../Utils';
import runJest from '../runJest';

const dir = path.resolve(__dirname, '../coverage-remapping');
const coverageDir = path.join(dir, 'coverage');
Expand Down
8 changes: 4 additions & 4 deletions e2e/__tests__/coverage_report.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
*/
'use strict';

const fs = require('fs');
const path = require('path');
const {extractSummary} = require('../Utils');
const runJest = require('../runJest');
import fs from 'fs';
import path from 'path';
import {extractSummary} from '../Utils';
import runJest from '../runJest';

const DIR = path.resolve(__dirname, '../coverage-report');

Expand Down
6 changes: 3 additions & 3 deletions e2e/__tests__/coverage_threshold.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

'use strict';

const path = require('path');
const {cleanup, writeFiles, extractSummary} = require('../Utils');
const runJest = require('../runJest');
import path from 'path';
import {cleanup, extractSummary, writeFiles} from '../Utils';
import runJest from '../runJest';

const DIR = path.resolve(__dirname, '../coverage-threshold');

Expand Down
8 changes: 4 additions & 4 deletions e2e/__tests__/coverage_transform_instrumented.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

'use strict';

const {readFileSync} = require('fs');
const path = require('path');
const {cleanup, run} = require('../Utils');
const runJest = require('../runJest');
import {readFileSync} from 'fs';
import path from 'path';
import {cleanup, run} from '../Utils';
import runJest from '../runJest';

const dir = path.resolve(__dirname, '../coverage-transform-instrumented');
const coverageDir = path.join(dir, 'coverage');
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/custom-resolver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
'use strict';

const runJest = require('../runJest');
import runJest from '../runJest';

test('use the custom resolver', () => {
const result = runJest('custom-resolver');
Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/custom_matcher_stack_trace.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
*/
'use strict';

const runJest = require('../runJest');
const {extractSummary} = require('../Utils');
import runJest from '../runJest';
import {extractSummary} from '../Utils';

test('works with custom matchers', () => {
const {stderr} = runJest('custom-matcher-stack-trace');
Expand Down
8 changes: 4 additions & 4 deletions e2e/__tests__/custom_reporters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
*/
'use strict';

const {cleanup, extractSummary, writeFiles} = require('../Utils');
const runJest = require('../runJest');
const os = require('os');
const path = require('path');
import {cleanup, extractSummary, writeFiles} from '../Utils';
import runJest from '../runJest';
import os from 'os';
import path from 'path';

const DIR = path.resolve(os.tmpdir(), 'custom-reporters-test-dir');

Expand Down
Loading

0 comments on commit e3f0b43

Please sign in to comment.