-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
generate-tests.js
65 lines (56 loc) · 1.55 KB
/
generate-tests.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
import fs from "fs";
import * as path from "path";
const readFiles = (dirname, onFile, onError = () => {}) => {
fs.readdir(dirname, function(err, filenames) {
if (err) {
onError(err);
return;
}
filenames.forEach(function(filename) {
fs.readFile(dirname + filename, 'utf-8', function(err, content) {
if (err) {
onError(err);
return;
}
onFile(filename, content);
});
});
});
}
const sourceDir = './__html__/'
const targetDir = './__tests__/'
const test = `
import {beforeAll, afterAll, describe, it, expect, delay, getFileUrl, B} from "@olton/easytest";
beforeAll(async () => {
await B.create()
})
afterAll(async () => {
await B.bye()
})
describe("$file tests", () => {
it("$file", async () => {
await B.visit(\`\$\{getFileUrl(\`./__html__/$file\`\)\}\`)
expect(B.error).toBeNull(B.error)
})
})
`
fs.readdir(targetDir, (err, files) => {
if (err) throw err;
for (const file of files) {
fs.unlink(path.join(targetDir, file), (err) => {
if (err) throw err;
});
}
});
readFiles(sourceDir, (filename, content) => {
const [name, _] = filename.split(".")
fs.writeFile(`${targetDir}/${name}.test.js`, `${test.replaceAll('$file', filename)}`, function (error) {
if (error) {
throw error;
} else {
console.log(`File ${filename} created`);
}
});
}, (e) => {
console.log(e)
})