Skip to content
Open
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
3 changes: 2 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ sade2
.option('--sw', 'Path to a script to be loaded in a service worker.')
.option(
'--assets',
'Folder with assets to be served by the http server. (default process.cwd())'
'One or more folders with assets to be served by the http server. (default process.cwd() will always be included)'
)
.option('--cwd', 'Current directory.', defaultOptions.cwd)
.option(
Expand Down Expand Up @@ -219,6 +219,7 @@ sade2
*
* @type {import('./src/types.js').RunnerOptions}
*/

const options = merge(config ? config.config : {}, {
input: input ? [input, ...opts._] : undefined,
testFiles: [],
Expand Down
1 change: 1 addition & 0 deletions mocks/assets/a/a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "a": 1 }
1 change: 1 addition & 0 deletions mocks/assets/b/b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
27 changes: 27 additions & 0 deletions mocks/assets/test.a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable no-undef */
// eslint-disable-next-line strict
const { is } = require('uvu/assert')

describe('assets', () => {
it('can fetch a and package.json', async () => {
// assets from cwd is available
is(
await fetch(new URL('package.json', import.meta.url)).then(
(res) => res.status
),
200
)

// assets from a
is(
await fetch(new URL('a.json', import.meta.url)).then((res) => res.status),
200
)

// assets from b
is(
await fetch(new URL('b.txt', import.meta.url)).then((res) => res.status),
404
)
})
})
27 changes: 27 additions & 0 deletions mocks/assets/test.ab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable no-undef */
// eslint-disable-next-line strict
const { is } = require('uvu/assert')

describe('assets', () => {
it('can fetch a, b and package.json', async () => {
// assets from cwd is available
is(
await fetch(new URL('package.json', import.meta.url)).then(
(res) => res.status
),
200
)

// assets from a
is(
await fetch(new URL('a.json', import.meta.url)).then((res) => res.status),
200
)

// assets from b
is(
await fetch(new URL('b.txt', import.meta.url)).then((res) => res.status),
200
)
})
})
2 changes: 1 addition & 1 deletion src/node/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const require = createRequire(import.meta.url)
*/
const defaultOptions = {
cwd: process.cwd(),
assets: '',
assets: undefined,
browser: 'chromium',
debug: false,
mode: 'main', // worker
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface RunnerOptions {
mode: 'main' | 'worker' | 'node'
incognito: boolean
extension: boolean
assets: string
assets?: string[]
before?: string
sw?: string
cov: boolean
Expand Down Expand Up @@ -56,7 +56,7 @@ export interface CliOptions {
watch?: boolean
before?: string
sw?: string
assets: string
assets?: string[]
cwd: string
extensions: string
config?: string
Expand Down
46 changes: 28 additions & 18 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const merge = mergeOptions.bind({
*/
export const defaultOptions = {
cwd: process.cwd(),
assets: '',
assets: undefined,
browser: 'chromium',
debug: false,
mode: 'main', // worker
Expand Down Expand Up @@ -617,13 +617,21 @@ function getPort(port = 3000, host = '127.0.0.1') {
*
* @param {string} dir - Runner directory
* @param {string} cwd - Current working directory
* @param {string} assets - Assets directory
* @param {string[]| undefined} assets - Assets directories
* @returns {Promise<{ url: string; server: import('http').Server }>}
*/
export async function createPolka(dir, cwd, assets) {
const host = '127.0.0.1'
const port = await getPort(0, host)
const url = `http://${host}:${port}/`
if (typeof assets === 'string') {
assets = [assets]
} else if (assets === undefined || assets === null) {
assets = []
}
if (!assets.includes(cwd)) {
assets.push(cwd)
}
return new Promise((resolve, reject) => {
const { server } = polka()
.use(
Expand All @@ -643,22 +651,24 @@ export async function createPolka(dir, cwd, assets) {
)
.use(
// @ts-ignore
sirv(path.join(cwd, assets), {
dev: true,
setHeaders: (
/** @type {{ setHeader: (arg0: string, arg1: string) => void; }} */ rsp,
/** @type {string} */ pathname
) => {
// workaround for https://github.com/lukeed/sirv/issues/158 - we
// can't unset the `Content-Encoding` header because sirv sets it
// after this function is invoked and will only set it if it's not
// already set, so we need to set it to a garbage value that will be
// ignored by browsers
if (pathname.endsWith('.gz')) {
rsp.setHeader('Content-Encoding', 'unsupported-encoding')
}
},
})
...assets.map((dir) =>
sirv(dir, {
dev: true,
setHeaders: (
/** @type {{ setHeader: (arg0: string, arg1: string) => void; }} */ rsp,
/** @type {string} */ pathname
) => {
// workaround for https://github.com/lukeed/sirv/issues/158 - we
// can't unset the `Content-Encoding` header because sirv sets it
// after this function is invoked and will only set it if it's not
// already set, so we need to set it to a garbage value that will be
// ignored by browsers
if (pathname.endsWith('.gz')) {
rsp.setHeader('Content-Encoding', 'unsupported-encoding')
}
},
})
)
)
.listen(port, host, (/** @type {Error} */ err) => {
if (err) {
Expand Down
32 changes: 31 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,37 @@ describe('uvu', () => {
})
})

describe.skip('benchmark', () => {
describe('assets', () => {
it('1 asset', async () => {
const proc = await execa('./cli.js', [
'mocks/assets/test.a.js',
'--runner',
'mocha',
'--assets',
'./mocks/assets/a',
])

is(proc.exitCode, 0, 'exit code')
ok(proc.stdout.includes('passing'), 'process stdout')
})

it('2 assets', async () => {
const proc = await execa('./cli.js', [
'mocks/assets/test.ab.js',
'--runner',
'mocha',
'--assets',
'./mocks/assets/a',
'--assets',
'./mocks/assets/b',
])

is(proc.exitCode, 0, 'exit code')
ok(proc.stdout.includes('passing'), 'process stdout')
})
})

describe.skip('benchmark', function () {
it('benchmark', async () => {
const proc = await execa('./cli.js', [
'mocks/benchmark.js',
Expand Down