Skip to content

Commit

Permalink
chore: convert e2e init test and helpers to TS (react-native-communit…
Browse files Browse the repository at this point in the history
…y#705)

* chore: Convert e2e/init.test and jest/helpers to Typescript

* chore: remove readable stream types temporary

* chore: convert e2e tests to TypeScript

* chore: Adding temporary $FlowFixMe

* chore: Update Jest Config

* chore: Update Snaps

* Revert "chore: Update Snaps"

This reverts commit af5ae56.

* chore: Update Snaps

* update snapshots
  • Loading branch information
assuncaocharles authored and thymikee committed Sep 11, 2019
1 parent 8cd6543 commit fde227d
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 20 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions __e2e__/legacyInit.test.js → __e2e__/legacyInit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow
import fs from 'fs';
import path from 'path';
// @ts-ignore
import execa from 'execa';
import {getTempDirectory, cleanup, writeFiles} from '../jest/helpers';

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
...common,
displayName: 'e2e',
setupFiles: ['<rootDir>/jest/setupE2eTests.js'],
testMatch: ['<rootDir>/**/__e2e__/*{.,-}test.js'],
testMatch: ['<rootDir>/**/__e2e__/*{.,-}test.[jt]s'],
},
{
...common,
Expand Down
41 changes: 25 additions & 16 deletions jest/helpers.js → jest/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,32 @@ import fs from 'fs';
import os from 'os';
import path from 'path';
import {createDirectory} from 'jest-util';
// @ts-ignore jsfile
import rimraf from 'rimraf';
// @ts-ignore jsfile
import execa from 'execa';
import chalk from 'chalk';
// @ts-ignore jsfile
import {Writable} from 'readable-stream';

const CLI_PATH = path.resolve(__dirname, '../packages/cli/build/bin.js');

type RunOptions = {
nodeOptions?: string,
nodePath?: string,
timeout?: number, // kill the process after X milliseconds
expectedFailure?: boolean,
nodeOptions?: string;
nodePath?: string;
timeout?: number; // kill the process after X milliseconds
expectedFailure?: boolean;
};

type CLIArgs = {
dir: string;
args: string[] | undefined;
options: RunOptions;
};

export function run(
dir: string,
args?: Array<string>,
args?: string[],
options: RunOptions = {
expectedFailure: false,
},
Expand All @@ -30,7 +39,7 @@ export function run(
// Runs cli until a given output is achieved, then kills it with `SIGTERM`
export async function runUntil(
dir: string,
args: Array<string> | void,
args: string[] | undefined,
text: string,
options: RunOptions = {
expectedFailure: false,
Expand All @@ -40,7 +49,7 @@ export async function runUntil(

spawnPromise.stderr.pipe(
new Writable({
write(chunk, _encoding, callback) {
write(chunk: any, _encoding: string, callback: () => void) {
const output = chunk.toString('utf8');

if (output.includes(text)) {
Expand Down Expand Up @@ -112,7 +121,7 @@ export const copyDir = (src: string, dest: string) => {
export const getTempDirectory = (name: string) =>
path.resolve(os.tmpdir(), name);

function spawnCli(dir: string, args?: Array<string>, options: RunOptions = {}) {
function spawnCli(dir: string, args?: string[], options: RunOptions = {}) {
const {spawnArgs, spawnOptions} = getCliArguments({dir, args, options});

const result = execa.sync(process.execPath, spawnArgs, spawnOptions);
Expand All @@ -122,11 +131,7 @@ function spawnCli(dir: string, args?: Array<string>, options: RunOptions = {}) {
return result;
}

function spawnCliAsync(
dir: string,
args?: Array<string>,
options: RunOptions = {},
) {
function spawnCliAsync(dir: string, args?: string[], options: RunOptions = {}) {
const {spawnArgs, spawnOptions} = getCliArguments({dir, args, options});

try {
Expand All @@ -137,7 +142,7 @@ function spawnCliAsync(
}
}

function getCliArguments({dir, args, options}) {
function getCliArguments({dir, args, options}: CLIArgs) {
const isRelative = !path.isAbsolute(dir);

if (isRelative) {
Expand All @@ -163,10 +168,14 @@ function getCliArguments({dir, args, options}) {
return {spawnArgs, spawnOptions};
}

function handleTestCliFailure(options, result, dir, args) {
function handleTestCliFailure(
options: RunOptions,
result: {[key: string]: any},
dir: string,
args: string[] | undefined,
) {
if (!options.expectedFailure && result.code !== 0) {
console.log(`Running CLI failed for unexpected reason. Here's more info:
${chalk.bold('dir:')} ${dir}
${chalk.bold('args:')} ${(args || []).join(' ')}
${chalk.bold('stderr:')} ${result.stderr}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
*/

import findDependencies from '../findDependencies';

import {
cleanup,
writeFiles,
getTempDirectory,
getTempDirectory, // $FlowFixMe - converted to TS
} from '../../../../../../jest/helpers';

jest.mock('../resolveNodeModuleDir');
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/tools/config/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {logger} from '@react-native-community/cli-tools';
import {
cleanup,
writeFiles,
getTempDirectory,
getTempDirectory, // $FlowFixMe - converted to TS
} from '../../../../../../jest/helpers';

jest.mock('../resolveNodeModuleDir');
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2055,6 +2055,13 @@
"@types/minimatch" "*"
"@types/node" "*"

"@types/graceful-fs@^4.1.3":
version "4.1.3"
resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.3.tgz#039af35fe26bec35003e8d86d2ee9c586354348f"
integrity sha512-AiHRaEB50LQg0pZmm659vNBb9f4SJ0qrAnteuzhSeAUcJKxoYgEnprg/83kppCnc2zvtCKbdZry1a5pVY3lOTQ==
dependencies:
"@types/node" "*"

"@types/istanbul-lib-coverage@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.0.tgz#1eb8c033e98cf4e1a4cedcaf8bcafe8cb7591e85"
Expand Down

0 comments on commit fde227d

Please sign in to comment.