tauri/cli/tauri.js/test/jest/__tests__/dev.spec.js
Lucas Fernandes Nogueira d37efa5605
test(tauri.js) add template test, close #427 (#501)
* test(tauri.js) add template test

* fix(lint) ignore prefer-optional-chain because babel-jest wont accept it

* feat(webpack) raw-loader test for windows

* fix(workflow) install tauri-bundler

* fix(EOF)

* chore(test) try with done cb

* fix(test) cwd scope

* fix(test) transform regex for windows

* fix(test) windows regex on jest transform

Co-authored-by: nothingismagick <denjell@sfosc.org>
2020-03-09 17:57:27 -03:00

97 lines
2.2 KiB
JavaScript

const path = require('path')
const fixtureSetup = require('../fixtures/app-test-setup')
const distDir = path.resolve(fixtureSetup.fixtureDir, 'app', 'dist')
function startDevServer() {
const http = require('http')
const { statSync, createReadStream } = require('fs')
const app = http.createServer((req, res) => {
if (req.method === 'GET') {
if (req.url === '/') {
const indexPath = path.join(distDir, 'index.html')
const stat = statSync(indexPath)
res.writeHead(200, {
'Content-Type': 'text/html',
'Content-Length': stat.size
})
createReadStream(indexPath).pipe(res)
}
}
})
const port = 7001
const server = app.listen(port)
return {
server,
url: `http://localhost:${port}`
}
}
function runDevTest(tauriConfig) {
fixtureSetup.initJest('app')
const dev = require('api/dev')
return new Promise(async (resolve, reject) => {
try {
const { promise, runner } = dev(tauriConfig)
const isRunning = require('is-running')
let success = false
const checkIntervalId = setInterval(async () => {
if (!isRunning(runner.pid) && !success) {
server.close(() => reject("App didn't reply"))
}
}, 2000)
const server = fixtureSetup.startServer(async () => {
success = true
clearInterval(checkIntervalId)
// wait for the app process to be killed
setTimeout(async () => {
try {
await runner.stop()
} catch {}
resolve()
}, 2000)
})
await promise
} catch (error) {
reject(error)
}
})
}
describe('Tauri Dev', () => {
const build = {
distDir: distDir
}
const devServer = startDevServer()
it.each`
url
${devServer.url}
${distDir}
`('works with dev pointing to $url', ({ url }) => {
const runningDevServer = url.startsWith('http')
const promise = runDevTest({
build: {
...build,
devPath: url
},
ctx: {
debug: true,
dev: true
}
})
promise.then(() => {
if (runningDevServer) {
devServer.server.close()
}
})
return promise
})
})