Skip to content

Commit

Permalink
feat: support ESM config files (#414)
Browse files Browse the repository at this point in the history
Uses `import` for dynamic module imports allowing you to use ESM as a config file.

Also lets you return a function that returns a promise to get the config in case you need to do some async work to retrieve it from somewhere.

Fixes #413
  • Loading branch information
achingbrain committed Apr 6, 2022
1 parent bb33080 commit 3f731c8
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ node_modules
coverage.lcov
yarn-error.log
dist
.tmp
39 changes: 32 additions & 7 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import path from 'path'
import sade from 'sade'
import kleur from 'kleur'
import { fileURLToPath } from 'url'
import { lilconfigSync } from 'lilconfig'
import { lilconfig } from 'lilconfig'
import mergeOptions from 'merge-options'
import { runnerOptions } from './src/utils/index.js'
import UvuRunner from './src/runner-uvu.js'
Expand Down Expand Up @@ -105,6 +105,22 @@ const sade2 = new Proxy(sade('playwright-test [files]', true), {
},
})

const loadEsm = async (/** @type {string} */ filepath) => {
/** @type {any} */
const res = await import(filepath)

if (res.default) {
return res.default
}

return res
}

const configLoaders = {
'.js': loadEsm,
'.mjs': loadEsm,
}

sade2
.version(version)
.describe(
Expand Down Expand Up @@ -143,20 +159,29 @@ sade2
'File extensions allowed in the bundle. (default js,cjs,mjs,ts,tsx)'
)
.option('--config', 'Path to the config file')
.action((input, opts) => {
.action(async (input, opts) => {
let config
try {
if (opts.config) {
config = lilconfigSync('playwright-test').load(
path.resolve(opts.config)
)
config = await lilconfig('playwright-test', {
loaders: configLoaders,
}).load(path.resolve(opts.config))
} else {
config = lilconfigSync('playwright-test').search()
config = await lilconfig('playwright-test', {
loaders: configLoaders,
}).search()
if (!config) {
config = lilconfigSync('pw-test').search()
config = await lilconfig('pw-test', {
loaders: configLoaders,
}).search()
}
}

// if the supplied config module was a function, invoke it
if (config && typeof config.config === 'function') {
config.config = await config.config()
}

let Runner

switch (opts.runner) {
Expand Down
10 changes: 10 additions & 0 deletions mocks/sw/sw.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require('path')

export default {
buildSWConfig: {
inject: [path.join(__dirname, 'sw-globals.js')],
},
afterTests: () => {
console.log('AFTER')
}
}
12 changes: 12 additions & 0 deletions mocks/sw/sw.function.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const path = require('path')

export default function () {
return {
buildSWConfig: {
inject: [path.join(__dirname, 'sw-globals.js')],
},
afterTests: () => {
console.log('AFTER')
}
}
}
10 changes: 10 additions & 0 deletions mocks/sw/sw.promise.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require('path')

export default Promise.resolve({
buildSWConfig: {
inject: [path.join(__dirname, 'sw-globals.js')],
},
afterTests: () => {
console.log('AFTER')
}
})
36 changes: 36 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,42 @@ describe('mocha', function () {

is(proc.exitCode, 0, 'exit code')
})

it('supports esm config files', async () => {
const proc = await execa('./cli.js', [
'mocks/sw/sw-test.js',
'--sw',
'mocks/sw/sw.js',
'--config',
'mocks/sw/sw.config.mjs',
])

is(proc.exitCode, 0, 'exit code')
})

it('supports esm config files that return promises', async () => {
const proc = await execa('./cli.js', [
'mocks/sw/sw-test.js',
'--sw',
'mocks/sw/sw.js',
'--config',
'mocks/sw/sw.promise.config.mjs',
])

is(proc.exitCode, 0, 'exit code')
})

it('supports esm config files that return functions', async () => {
const proc = await execa('./cli.js', [
'mocks/sw/sw-test.js',
'--sw',
'mocks/sw/sw.js',
'--config',
'mocks/sw/sw.function.config.mjs',
])

is(proc.exitCode, 0, 'exit code')
})
})

describe('tape', function () {
Expand Down

0 comments on commit 3f731c8

Please sign in to comment.