enso/app/ide-desktop/lib/dashboard/test-server.ts
somebody1234 3c31155fe9
Dashboard tests (#7656)
- Implements https://github.com/enso-org/cloud-v2/issues/631
- Tests for dashboard (`app/ide-desktop/lib/dashboard/`):
- End-to-end tests
- Unit tests
- Component tests

The purpose of this PR is to introduce the testing framework - more tests can be added later in separate PRs.

# Important Notes
To test, run `npm run test` in `app/ide-desktop`, or `app/ide-desktop/lib/dashboard/`. All tests should pass.

Individual test types can be run using `npm run test-unit`, `npm run test-component` and `npm run test-e2e` in `app/ide-desktop/lib/dashboard/`.
Individual end-to-end tests can be run using `npx playwright test -c playwright-e2e.config.ts test-e2e/<file name>.spec.ts` in `app/ide-desktop/lib/dashboard/`.

End-to-end tests require internet access to pass (for things like fonts).

This PR *does* check in screenshots to guard against visual regessions (and/or to make visual changes obvious)
2023-10-11 10:24:33 +00:00

74 lines
2.6 KiB
TypeScript

/** @file File watch and compile service. */
import * as fs from 'node:fs/promises'
import * as path from 'node:path'
import * as url from 'node:url'
import * as esbuild from 'esbuild'
import * as bundler from './esbuild-config'
// =================
// === Constants ===
// =================
/** The path of this file. */
const THIS_PATH = path.resolve(path.dirname(url.fileURLToPath(import.meta.url)))
/** This must be port `8080` because it is defined as such in AWS. */
const PORT = 8080
// `outputPath` does not have to be a real directory because `write` is `false`,
// meaning that files will not be written to the filesystem.
// However, the path should still be non-empty in order for `esbuild.serve` to work properly.
const OPTS = bundler.bundlerOptions({ outputPath: '/', devMode: true })
OPTS.define['REDIRECT_OVERRIDE'] = JSON.stringify(`http://localhost:${PORT}`)
OPTS.entryPoints.push(
path.resolve(THIS_PATH, 'src', 'index.html'),
path.resolve(THIS_PATH, 'src', 'index.tsx'),
path.resolve(THIS_PATH, 'src', 'serviceWorker.ts')
)
OPTS.write = false
OPTS.loader['.html'] = 'copy'
OPTS.plugins.push({
name: 'inject-mock-modules',
setup: build => {
build.onResolve({ filter: /^\..+$/ }, async args => {
const importerIsMockFile = /[\\/]lib[\\/]dashboard[\\/]mock[\\/]/.test(args.importer)
const sourcePath = path.resolve(path.dirname(args.importer), args.path)
if (!importerIsMockFile && /[\\/]lib[\\/]dashboard[\\/]src[\\/]/.test(sourcePath)) {
const mockPath = sourcePath
.replace('/lib/dashboard/src/', '/lib/dashboard/mock/')
.replace('\\lib\\dashboard\\src\\', '\\lib\\dashboard\\mock\\')
try {
await fs.access(mockPath + '.ts', fs.constants.R_OK)
return { path: mockPath + '.ts' }
} catch {
try {
await fs.access(mockPath + '.tsx', fs.constants.R_OK)
return { path: mockPath + '.tsx' }
} catch {
return
}
}
} else {
// The `if` case above always returns.
// eslint-disable-next-line no-restricted-syntax
return
}
})
},
})
// ===============
// === Watcher ===
// ===============
/** Start the esbuild watcher. */
async function serve() {
const builder = await esbuild.context(OPTS)
await builder.serve({
port: PORT,
servedir: OPTS.outdir,
})
}
void serve()