-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
163 lines (140 loc) · 4.77 KB
/
test.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { readdir } from "fs/promises";
import PostgreSqlDriver from "./dist/drivers/postgres/PostgreSqlDriver.js";
import SqlServerDriver from "./dist/drivers/sql-server/SqlServerDriver.js";
import * as ports from "tcp-port-used";
import path from "path";
const host = process.env.POSTGRES_HOST ?? "localhost";
const postGresPort = Number(process.env.POSTGRES_PORT ?? 5432);
/**
* @type string
*/
let testFile;
const testFileIndex = process.argv.indexOf("--test-file");
if (testFileIndex !== -1) {
testFile = process.argv[testFileIndex+1];
}
testFile = testFile ? testFile.replace("/src/", "/dist/").replace("\\src\\","\\dist\\").replace(".ts", ".js") : void 0;
if (testFile) {
if (testFile.startsWith(".")) {
testFile = path.resolve(testFile);
}
console.log(`Executing test - ${testFile}`);
}
// if (process.argv.includes("test-db")) {
// // wait for ports to open...
// console.log("Waiting for port to be open");
// await ports.waitUntilUsedOnHost(port, host, void 0, 15000);
// }
/**
* @type Array<{ name: string, error: string }>
*/
const results = [];
let start = Date.now();
const onlyPostGres = process.argv.some((x) => x === "--test-only-postgres");
const onlySqlServer = process.argv.some((x) => x === "--test-only-sql-server");
export default class TestRunner {
static get drivers() {
const database = "D" + (start++);
const pg = new PostgreSqlDriver({
database,
host,
user: "postgres",
password: "abcd123",
port: postGresPort,
ssl: null
// deleteDatabase: async (driver) => [driver.config.database = "postgres", await driver.executeQuery(`DROP DATABASE IF EXISTS "${database}" WITH (FORCE)`)]
});
const sqlServer = new SqlServerDriver({
database,
host,
user: "sa",
password: "$EntityAccess2023",
port: 1433,
options: {
encrypt: true, // for azure
trustServerCertificate: true // change to true for local dev / self-signed certs
},
// deleteDatabase: async (driver) => {
// try {
// driver.config.database = "master";
// await driver.executeQuery(`USE master;
// ALTER DATABASE ${database} SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
// DROP DATABASE ${database}`);
// } catch {
// }
// }
})
if (onlyPostGres) {
return [pg];
}
if (onlySqlServer) {
return [sqlServer];
}
return [ pg, sqlServer ];
}
/**
*
* @param {string} name
*/
static async runTest(name, thisParam) {
const moduleExports = await import(name);
const { default: d } = moduleExports;
if (!d) {
return;
}
try {
const r = d.call(thisParam);
if (r?.then) {
await r;
}
results.push({ name });
await thisParam.driver.config.deleteDatabase?.(thisParam.driver);
} catch (error) {
results.unshift({ name, error });
}
}
static async runAll(dir, db) {
const items = await readdir(dir, { withFileTypes: true });
const tasks = [];
for (const iterator of items) {
const next = dir + "/" + iterator.name;
if (iterator.isDirectory()) {
tasks.push(this.runAll(next, db));
continue;
}
if (iterator.name.endsWith(".js")) {
if (testFile) {
if (next !== testFile) {
if(testFile !== path.resolve(next)) {
continue;
}
}
}
for (const driver of this.drivers) {
tasks.push(this.runTest(next, { driver, db }));
}
}
}
await Promise.all(tasks);
}
}
const testDb = !process.argv.includes("no-db");
await TestRunner.runAll("./dist/tests", testDb);
let exitCode = 0;
let failed = 0;
for (const { error, name } of results) {
if (error) {
exitCode = 1;
failed++;
console.error(`${name} failed`);
console.error(error?.stack ?? error);
continue;
}
console.log(`${name} executed.`);
}
if (exitCode === 0) {
console.log(`${results.length} tests ran successfully.`);
} else {
console.log(`${failed} Tests out of ${results.length} failed.`);
}
process.exit(exitCode);