Skip to content

Commit

Permalink
build: replace prepare-e2e script by inline yarn install in test (#940
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ahnpnl committed May 31, 2021
1 parent 498f245 commit 0c37d6f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 20 deletions.
10 changes: 9 additions & 1 deletion e2e/__tests__/custom-typings.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import path from 'path';

import runJest from '../run-jest';
import { onNodeVersions } from '../utils';
import { onNodeVersions, runYarnInstall } from '../utils';

const dir = path.resolve(__dirname, '../custom-typings');

beforeEach(() => {
runYarnInstall(dir);
});

test('support custom typings', () => {
const result = runJest('custom-typings');
Expand Down
16 changes: 13 additions & 3 deletions e2e/__tests__/with-babel.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import path from 'path';

import runJest from '../run-jest';
import { onNodeVersions } from '../utils';
import { onNodeVersions, runYarnInstall } from '../utils';

const dir = path.resolve(__dirname, '../with-babel');

beforeEach(() => {
runYarnInstall(dir);
});

test('support using with Babel', () => {
const result = runJest('with-babel');
// --no-cache because babel can cache stuff and result in false green
const result = runJest('with-babel', ['--no-cache']);

expect(result.exitCode).toBe(0);
});

onNodeVersions('^12.17.0 || >=13.2.0', () => {
test('support using with Babel in ESM mode', () => {
const result = runJest('with-babel', ['-c=jest-esm.config.js'], {
// --no-cache because babel can cache stuff and result in false green
const result = runJest('with-babel', ['-c=jest-esm.config.js', '--no-cache'], {
nodeOptions: '--experimental-vm-modules',
});

Expand Down
49 changes: 47 additions & 2 deletions e2e/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import fs from 'fs';
import path from 'path';

import type { Config } from '@jest/types';
import { ExecaReturnValue, sync as spawnSync } from 'execa';
import semver from 'semver';

interface RunResult extends ExecaReturnValue {
status: number;
error: Error;
}

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

export function onNodeVersions(versionRange: string, testBody: () => void): void {
const run = (cmd: string, cwd?: Config.Path, env?: Record<string, string>): RunResult => {
const args = cmd.split(/\s/).slice(1);
const spawnOptions = { cwd, env, preferLocal: false, reject: false };
const result = spawnSync(cmd.split(/\s/)[0], args, spawnOptions) as RunResult;

// For compat with cross-spawn
result.status = result.exitCode;

if (result.status !== 0) {
const message = `
ORIGINAL CMD: ${cmd}
STDOUT: ${result.stdout}
STDERR: ${result.stderr}
STATUS: ${result.status}
ERROR: ${result.error}
`;
throw new Error(message);
}

return result;
};

export const runYarnInstall = (cwd: Config.Path, env?: Record<string, string>): RunResult => {
const lockfilePath = path.resolve(cwd, 'yarn.lock');
let exists = true;

// If the lockfile doesn't exist, yarn's project detection is confused. Just creating an empty file works
if (!fs.existsSync(lockfilePath)) {
exists = false;
fs.writeFileSync(lockfilePath, '');
}

return run(exists ? 'yarn install --immutable' : 'yarn install', cwd, env);
};

export const onNodeVersions = (versionRange: string, testBody: () => void): void => {
const description = `on node ${versionRange}`;
if (semver.satisfies(process.versions.node, versionRange)) {
// eslint-disable-next-line jest/valid-title
Expand All @@ -22,4 +67,4 @@ export function onNodeVersions(versionRange: string, testBody: () => void): void
testBody();
});
}
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"lint:fix": "eslint --fix --ext .js,.ts .",
"lint:prettier": "prettier '**/*.{yml,yaml}' 'website/**/*.{css,js,md}' 'README.md' --write --ignore-path .gitignore",
"lint:prettier-ci": "prettier '**/*.{yml,yaml}' 'website/**/*.{css,js,md}' 'README.md' --check --ignore-path .gitignore",
"test": "node scripts/prepare-e2e.js && jest",
"test": "jest",
"test:examples": "node scripts/test-examples.js",
"doc": "cd website && yarn start",
"doc:build": "cd website && yarn build",
Expand Down
13 changes: 0 additions & 13 deletions scripts/prepare-e2e.js

This file was deleted.

0 comments on commit 0c37d6f

Please sign in to comment.