Skip to content

Commit

Permalink
(refactor): migrate all tests and test helper code to TS (#649)
Browse files Browse the repository at this point in the history
- woot, all TS now!
  - get that big blue line for language TS on GitHub haha

- for the most part, not much more to do other than .js -> .ts
  - for the utils had to add some types around

- also use ESM instead of CJS in tests and test helpers
  - require -> import
  - module.exports -> export
    - the indentation change here with utils/fixture made git treat
      it as a delete + new instead of a rename
  • Loading branch information
agilgur5 authored Mar 28, 2020
1 parent aa09dcb commit 863c56d
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const shell = require('shelljs');
const util = require('../utils/fixture');
const { execWithCache } = require('../utils/shell');
import * as shell from 'shelljs';

import * as util from '../utils/fixture';
import { execWithCache } from '../utils/shell';

shell.config.silent = false;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const shell = require('shelljs');
const util = require('../utils/fixture');
const { execWithCache } = require('../utils/shell');
import * as shell from 'shelljs';

import * as util from '../utils/fixture';
import { execWithCache } from '../utils/shell';

shell.config.silent = false;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const shell = require('shelljs');
const util = require('../utils/fixture');
const { execWithCache } = require('../utils/shell');
import * as shell from 'shelljs';

import * as util from '../utils/fixture';
import { execWithCache } from '../utils/shell';

shell.config.silent = false;

Expand Down
4 changes: 2 additions & 2 deletions test/e2e/tsdx-lint.test.js → test/e2e/tsdx-lint.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const shell = require('shelljs');
import * as shell from 'shelljs';

const util = require('../utils/fixture');
import * as util from '../utils/fixture';

shell.config.silent = true;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const shell = require('shelljs');
import * as shell from 'shelljs';

const util = require('../utils/fixture');
const { execWithCache } = require('../utils/shell');
import * as util from '../utils/fixture';
import { execWithCache } from '../utils/shell';

shell.config.silent = false;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const shell = require('shelljs');
import * as shell from 'shelljs';

const util = require('../utils/fixture');
const { execWithCache, grep } = require('../utils/shell');
import * as util from '../utils/fixture';
import { execWithCache, grep } from '../utils/shell';

shell.config.silent = false;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const shell = require('shelljs');
const fs = require('fs-extra');
import * as fs from 'fs-extra';
import * as shell from 'shelljs';

const util = require('../utils/fixture');
const { execWithCache } = require('../utils/shell');
import * as util from '../utils/fixture';
import { execWithCache } from '../utils/shell';

shell.config.silent = false;

Expand Down
28 changes: 0 additions & 28 deletions test/utils/fixture.js

This file was deleted.

29 changes: 29 additions & 0 deletions test/utils/fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as path from 'path';
import * as shell from 'shelljs';

export const rootDir = process.cwd();

shell.config.silent = true;

export function setupStageWithFixture(
testDir: string,
stageName: string,
fixtureName: string
): void {
const stagePath = path.join(rootDir, stageName);
shell.mkdir(stagePath);
shell.exec(
`cp -a ${rootDir}/test/${testDir}/fixtures/${fixtureName}/. ${stagePath}/`
);
shell.ln(
'-s',
path.join(rootDir, 'node_modules'),
path.join(stagePath, 'node_modules')
);
shell.cd(stagePath);
}

export function teardownStage(stageName: string): void {
shell.cd(rootDir);
shell.rm('-rf', path.join(rootDir, stageName));
}
23 changes: 9 additions & 14 deletions test/utils/shell.js → test/utils/shell.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// this file contains helper utils for working with shell.js functions
const shell = require('shelljs');
import * as shell from 'shelljs';

shell.config.silent = true;

// simple shell.exec "cache" that doesn't re-run the same command twice in a row
let prevCommand = '';
let prevCommandOutput = {};
function execWithCache(command, { noCache = false } = {}) {
let prevCommandOutput = {} as shell.ShellReturnValue;
export function execWithCache(
command: string,
{ noCache = false } = {}
): shell.ShellReturnValue {
// return the old output
if (!noCache && prevCommand === command) return prevCommandOutput;

Expand All @@ -15,7 +18,7 @@ function execWithCache(command, { noCache = false } = {}) {
// reset if command is not to be cached
if (noCache) {
prevCommand = '';
prevCommandOutput = {};
prevCommandOutput = {} as shell.ShellReturnValue;
} else {
prevCommand = command;
prevCommandOutput = output;
Expand All @@ -24,19 +27,11 @@ function execWithCache(command, { noCache = false } = {}) {
return output;
}

// shelljs.grep wrapper
// @param {RegExp} pattern
// @param {string} fileName
// @returns {boolean} true if pattern has matches in file
function grep(pattern, fileName) {
// shell.js grep wrapper returns true if pattern has matches in file
export function grep(pattern: RegExp, fileName: string[]): boolean {
const output = shell.grep(pattern, fileName);
// output.code is always 0 regardless of matched/unmatched patterns
// so need to test output.stdout
// https://github.com/jaredpalmer/tsdx/pull/525#discussion_r395571779
return Boolean(output.stdout.match(pattern));
}

module.exports = {
execWithCache,
grep,
};

0 comments on commit 863c56d

Please sign in to comment.