tutanota/test/test.js

87 lines
2.6 KiB
JavaScript
Raw Permalink Normal View History

2022-12-27 15:37:40 +01:00
import { runTestBuild } from "./TestBuilder.js"
import { Option, program } from "commander"
2017-08-15 13:54:22 +02:00
2022-05-10 16:16:38 +02:00
await program
.addOption(new Option("-i, --integration", "Include integration tests (requires local tutadb server)"))
2022-05-10 16:16:38 +02:00
.addOption(new Option("-c, --clean"))
2023-06-29 18:26:45 +02:00
.addOption(new Option("-f, --filter <query>", "Filter for tests and specs to run only matching tests"))
2023-07-27 14:03:47 +02:00
.addOption(new Option("--no-run", "Do not run the tests in node"))
.addOption(new Option("-b, --browser", "Start the web server and run the tests in browser").default(false))
.addOption(new Option("--browser-cmd <path>", "Command used to run the browser").default("xdg-open"))
.action(async ({ clean, integration, filter, run, browser, browserCmd }) => {
await runTestBuild({ clean })
2019-09-13 13:49:11 +02:00
console.log("build finished!")
2023-07-27 14:03:47 +02:00
let nodeOk
2023-06-29 18:26:45 +02:00
if (run) {
2023-07-27 14:03:47 +02:00
nodeOk = await runTestsInNode({ integration, filter })
} else {
nodeOk = true
}
let browserOk
if (browser) {
browserOk = await runTestsInBrowser({ filter, browserCmd })
2023-07-27 14:03:47 +02:00
} else {
browserOk = true
}
if (browserOk && nodeOk) {
process.exit(0)
} else {
process.exit(1)
2023-06-29 18:26:45 +02:00
}
2022-05-10 16:16:38 +02:00
})
.parseAsync(process.argv)
2023-07-27 14:03:47 +02:00
function resultIsOk(result) {
return result.failingTests.length === 0
}
/** Function which runs tests and exits with the exit code afterwards. */
async function runTestsInBrowser({ filter, browserCmd }) {
2023-07-27 14:03:47 +02:00
const { default: express } = await import("express")
const app = express()
app.use(express.static("build"))
// deafault limit (100Kb) is way too small
2023-07-27 14:03:47 +02:00
app.use(express.json({ limit: "10Mb" }))
const { spawn } = await import("node:child_process")
const server = await new Promise((resolve) => {
const s = app.listen(0, () => resolve(s))
})
const result = await new Promise((resolve) => {
2023-07-27 15:59:54 +02:00
app.post("/status", (req, res) => {
console.log("browser: ", req.body)
res.status(200).send()
})
2023-07-27 14:03:47 +02:00
app.post("/result", (req, res) => {
resolve(req.body)
res.status(200).send()
})
const url = new URL(`http://localhost:${server.address().port}/test.html`)
if (filter) {
url.searchParams.set("filter", filter)
}
const command = `${browserCmd} '${url.toString()}'`
console.log(`> ${command}`)
2023-07-27 15:59:54 +02:00
spawn(command, { stdio: "inherit", shell: true })
2023-07-27 14:03:47 +02:00
})
2023-07-27 14:03:47 +02:00
const { default: o } = await import("@tutao/otest")
console.log("\n--------------- BROWSER ---------------")
o.printReport(result)
return resultIsOk(result)
}
async function runTestsInNode({ integration, filter }) {
2023-06-29 18:26:45 +02:00
const { run } = await import("./build/testInNode.js")
2023-07-27 14:03:47 +02:00
console.log("\n--------------- NODE ---------------")
2023-06-29 18:26:45 +02:00
const result = await run({ integration, filter })
2023-07-27 14:03:47 +02:00
return resultIsOk(result)
}