-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathrun.test.ts
More file actions
78 lines (69 loc) · 2.32 KB
/
Copy pathrun.test.ts
File metadata and controls
78 lines (69 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { join, resolve } from "path";
import { existsSync, readdirSync } from "fs";
import config from "./config.json" with { type: "json" };
import filter from "lodash/filter.js";
import forEach from "lodash/forEach.js";
import isEmpty from "lodash/isEmpty.js";
import isArray from "lodash/isArray.js";
import includes from "lodash/includes.js";
import dotenv from 'dotenv';
// NOTE init env variables
dotenv.config();
const { IS_TS, UNIT_EXECUTION_ORDER, INTEGRATION_EXECUTION_ORDER } = config;
const testFileExtension = IS_TS ? ".ts" : ".js";
process.env.TS_NODE_PROJECT = resolve("test/tsconfig.json");
/**
* @method getFileName
* @param {string} file
* @returns {string}
*/
const getFileName = (file: string): string => {
if (includes(file, ".test") && includes(file, testFileExtension)) return file;
else if (includes(file, ".test")) return `${file}${testFileExtension}`;
else if (!includes(file, ".test")) return `${file}.test${testFileExtension}`;
else return `${file}.test${testFileExtension}`;
};
/**
* @method includeTestFiles
* @param {Array<string>} files
* @param {string} basePath
*/
const includeTestFiles = (files: Array<string>, basePath = "integration") => {
forEach(files, (file) => {
const filename = getFileName(file);
const filePath = join(__dirname, basePath, filename);
try {
if (existsSync(filePath)) {
require(filePath);
} else {
console.error(`File not found - ${filename}`);
}
} catch (err) {}
});
};
/**
* @method run
* @param {Array<string> | undefined | null} executionOrder
* @param {boolean} isIntegrationTest
*/
const run = (
executionOrder: Array<string> | undefined | null,
isIntegrationTest = true
) => {
const testFolder = isIntegrationTest ? "integration" : "unit";
if (executionOrder && isArray(executionOrder) && !isEmpty(executionOrder)) {
includeTestFiles(executionOrder, testFolder);
} else {
const basePath = join(__dirname, testFolder);
const allIntegrationTestFiles = filter(readdirSync(basePath), (file) =>
includes(file, `.test${testFileExtension}`)
);
includeTestFiles(allIntegrationTestFiles);
}
};
const args = process.argv.slice(2);
if (includes(args, "--integration-test")) {
run(INTEGRATION_EXECUTION_ORDER);
} else if (includes(args, "--unit-test")) {
// run(UNIT_EXECUTION_ORDER, false);
}