Skip to content

Commit 863c56d

Browse files
authored
(refactor): migrate all tests and test helper code to TS (#649)
- 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
1 parent aa09dcb commit 863c56d

10 files changed

+62
-63
lines changed

test/e2e/tsdx-build-default.test.js renamed to test/e2e/tsdx-build-default.test.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
const shell = require('shelljs');
2-
const util = require('../utils/fixture');
3-
const { execWithCache } = require('../utils/shell');
1+
import * as shell from 'shelljs';
2+
3+
import * as util from '../utils/fixture';
4+
import { execWithCache } from '../utils/shell';
45

56
shell.config.silent = false;
67

test/e2e/tsdx-build-invalid.test.js renamed to test/e2e/tsdx-build-invalid.test.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
const shell = require('shelljs');
2-
const util = require('../utils/fixture');
3-
const { execWithCache } = require('../utils/shell');
1+
import * as shell from 'shelljs';
2+
3+
import * as util from '../utils/fixture';
4+
import { execWithCache } from '../utils/shell';
45

56
shell.config.silent = false;
67

test/e2e/tsdx-build-withTsconfig.test.js renamed to test/e2e/tsdx-build-withTsconfig.test.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
const shell = require('shelljs');
2-
const util = require('../utils/fixture');
3-
const { execWithCache } = require('../utils/shell');
1+
import * as shell from 'shelljs';
2+
3+
import * as util from '../utils/fixture';
4+
import { execWithCache } from '../utils/shell';
45

56
shell.config.silent = false;
67

test/e2e/tsdx-lint.test.js renamed to test/e2e/tsdx-lint.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const shell = require('shelljs');
1+
import * as shell from 'shelljs';
22

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

55
shell.config.silent = true;
66

test/integration/tsdx-build-options.test.js renamed to test/integration/tsdx-build-options.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const shell = require('shelljs');
1+
import * as shell from 'shelljs';
22

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

66
shell.config.silent = false;
77

test/integration/tsdx-build-withBabel.test.js renamed to test/integration/tsdx-build-withBabel.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const shell = require('shelljs');
1+
import * as shell from 'shelljs';
22

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

66
shell.config.silent = false;
77

test/integration/tsdx-build-withConfig.test.js renamed to test/integration/tsdx-build-withConfig.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const shell = require('shelljs');
2-
const fs = require('fs-extra');
1+
import * as fs from 'fs-extra';
2+
import * as shell from 'shelljs';
33

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

77
shell.config.silent = false;
88

test/utils/fixture.js

-28
This file was deleted.

test/utils/fixture.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as path from 'path';
2+
import * as shell from 'shelljs';
3+
4+
export const rootDir = process.cwd();
5+
6+
shell.config.silent = true;
7+
8+
export function setupStageWithFixture(
9+
testDir: string,
10+
stageName: string,
11+
fixtureName: string
12+
): void {
13+
const stagePath = path.join(rootDir, stageName);
14+
shell.mkdir(stagePath);
15+
shell.exec(
16+
`cp -a ${rootDir}/test/${testDir}/fixtures/${fixtureName}/. ${stagePath}/`
17+
);
18+
shell.ln(
19+
'-s',
20+
path.join(rootDir, 'node_modules'),
21+
path.join(stagePath, 'node_modules')
22+
);
23+
shell.cd(stagePath);
24+
}
25+
26+
export function teardownStage(stageName: string): void {
27+
shell.cd(rootDir);
28+
shell.rm('-rf', path.join(rootDir, stageName));
29+
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
// this file contains helper utils for working with shell.js functions
2-
const shell = require('shelljs');
2+
import * as shell from 'shelljs';
33

44
shell.config.silent = true;
55

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

@@ -15,7 +18,7 @@ function execWithCache(command, { noCache = false } = {}) {
1518
// reset if command is not to be cached
1619
if (noCache) {
1720
prevCommand = '';
18-
prevCommandOutput = {};
21+
prevCommandOutput = {} as shell.ShellReturnValue;
1922
} else {
2023
prevCommand = command;
2124
prevCommandOutput = output;
@@ -24,19 +27,11 @@ function execWithCache(command, { noCache = false } = {}) {
2427
return output;
2528
}
2629

27-
// shelljs.grep wrapper
28-
// @param {RegExp} pattern
29-
// @param {string} fileName
30-
// @returns {boolean} true if pattern has matches in file
31-
function grep(pattern, fileName) {
30+
// shell.js grep wrapper returns true if pattern has matches in file
31+
export function grep(pattern: RegExp, fileName: string[]): boolean {
3232
const output = shell.grep(pattern, fileName);
3333
// output.code is always 0 regardless of matched/unmatched patterns
3434
// so need to test output.stdout
3535
// https://github.com/jaredpalmer/tsdx/pull/525#discussion_r395571779
3636
return Boolean(output.stdout.match(pattern));
3737
}
38-
39-
module.exports = {
40-
execWithCache,
41-
grep,
42-
};

0 commit comments

Comments
 (0)