Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions multidim-interop/js/testground/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env node

import { fork } from 'child_process'

import yargs from 'yargs/yargs'
import { hideBin } from 'yargs/helpers'

import { bundle } from './cli/webpack.js'
import { run } from './runtime/browser/index.js'

; (async () => {
const argv = yargs(hideBin(process.argv))
.command('<path>', 'path to the test file to run')
.demandCommand(1)
.parseSync()

const RUNTIME = argv.runtime || 'node' // other possibilities: chromium, webkit, firefox

const testFile = argv._[0]

if (RUNTIME === 'node') {
// just run the test file in a forked node process, no preparation required
fork(testFile)
} else if (['chromium', 'webkit', 'firefox'].indexOf(RUNTIME) > -1) {
// 1. webpack the test file
const bundledTestFile = await bundle(testFile)
console.log(bundledTestFile)

// 2. run the test using the browser runtime
await run(RUNTIME, bundledTestFile)
} else {
throw new Error(`Unsupported runtime: ${RUNTIME}`)
}
})()
56 changes: 56 additions & 0 deletions multidim-interop/js/testground/cli/webpack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import os from 'os'
import path from 'path'
import { existsSync } from 'fs'
import fs from 'fs/promises'
import crypto from 'crypto'

import webpack from 'webpack'

// bundles the test file using webpack to a temporary directory,
// ready to be served by your automated browser for testing purposes
export async function bundle (testFile) {
const contextDir = process.cwd()

console.log(`bundle test file: ${testFile} (context: ${contextDir})`)

const tmpdir = path.join(os.tmpdir(), 'testground')
if (!(existsSync(tmpdir))) {
await fs.mkdir(tmpdir)
}
const bundleFileName = `${hashFileName(testFile)}.${randomId()}.test.bundle.js`

return await new Promise((resolve, reject) => {
webpack({
context: contextDir,
entry: testFile,
target: 'web',
output: {
path: tmpdir,
filename: bundleFileName
},
mode: 'development',
devtool: false
}, (err, stats) => {
if (err || stats.hasErrors()) {
if (err) {
console.error(err.stack || err)
if (err.details) {
console.error(err.details)
}
reject(err)
return
}
}

resolve(path.join(tmpdir, bundleFileName))
})
})
}

function hashFileName (name) {
return crypto.createHash('sha256').update(name).digest('hex')
}

function randomId () {
return crypto.randomBytes(8).toString('hex')
}
Loading