Skip to content

Commit

Permalink
Port to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Walter committed Dec 15, 2020
1 parent e1e50b4 commit ee00829
Show file tree
Hide file tree
Showing 32 changed files with 136 additions and 147 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ with their **default** values:
Declare a test by calling the test function with a name and a function:

```js
const { test } = require('@ianwalter/bff')
const someFunctionality = require('./someFunctionality')
const { test } from '@ianwalter/bff')
const someFunctionality from './someFunctionality')

test('some functionality', ({ expect }) => {
expect(someFunctionality()).toBeTruthy()
Expand Down
2 changes: 1 addition & 1 deletion docs/.vuepress/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
title: '@ianwalter/bff',
themeConfig: {
repo: 'ianwalter/bff',
Expand Down
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions packages/bff-playwright/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const playwright = require('playwright')
const { createLogger } = require('@generates/logger')
import playwright from 'playwright'
import { createLogger } from '@generates/logger'

const logger = createLogger({ level: 'info', namespace: 'bff.playwright' })
const availableBrowsers = ['chromium', 'firefox', 'webkit']

module.exports = {
export default {
async beforeEach (file, context) {
logger.debug(file.relativePath, '•', context.testContext.name)
context.testContext.playwright = playwright
Expand Down
3 changes: 2 additions & 1 deletion packages/bff-playwright/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
},
"homepage": "https://github.com/ianwalter/bff/blob/main/packages/bff-playwright#readme",
"license": "SEE LICENSE IN LICENSE",
"main": "index.js",
"type": "module",
"module": "index.js",
"scripts": {
"test": "bff"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/bff-playwright/tests/tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { test } = require('@ianwalter/bff')
import { test } from '@ianwalter/bff'

test('My personal site', async t => {
const { page } = await t.chromium({ args: ['--no-sandbox'] })
Expand Down
6 changes: 3 additions & 3 deletions packages/bff-webdriver/cleanup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const execa = require('execa')
const pSettle = require('p-settle')
import execa from 'execa'
import pSettle from 'p-settle'

module.exports = async function cleanup () {
export default async function cleanup () {
const names = [
'selenium',
'webdriver',
Expand Down
10 changes: 5 additions & 5 deletions packages/bff-webdriver/cli.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env node

const cli = require('@ianwalter/cli')
const { createLogger } = require('@generates/logger')
const { webdriverVersion } = require('.')
import cli from '@ianwalter/cli'
import { createLogger } from '@generates/logger'
import selenium from 'selenium-standalone.js'
import cleanup from './cleanup.js'
import { webdriverVersion } from './index.js'

const logger = createLogger({ level: 'info', namespace: 'bff.webdriver.cli' })

Expand All @@ -12,7 +14,6 @@ async function run () {

try {
if (command === 'setup') {
const selenium = require('selenium-standalone')
const { version = webdriverVersion, drivers } = config.webdriver || {}
await new Promise((resolve, reject) => {
selenium.install(
Expand All @@ -27,7 +28,6 @@ async function run () {
)
})
} else if (command === 'cleanup') {
const cleanup = require('./cleanup')
await cleanup()
} else {
logger.error('Unknown command:', command)
Expand Down
5 changes: 3 additions & 2 deletions packages/bff-webdriver/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require('dotenv').config()
import dotenv from 'dotenv'
dotenv.config()

module.exports = {
export default {
before (context) {
const standalone = process.env.SELENIUM_STANDALONE
const hostname = process.env.SELENIUM_HUB_HOST
Expand Down
14 changes: 7 additions & 7 deletions packages/bff-webdriver/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
const { createLogger } = require('@generates/logger')
import { createLogger } from '@generates/logger'
import standalone from 'selenium-standalone'
import { remote } from 'webdriverio'
import zalenium from './integrations/zalenium.js'
import appium from './integrations/appium.js'
import cleanup from './cleanup.js'

const webdriverVersion = '3.141.59'
const logger = createLogger({ level: 'info', namespace: 'bff.webdriver' })

let seleniumStandalone

module.exports = {
export default {
webdriverVersion,
async before (context) {
try {
Expand All @@ -16,7 +21,6 @@ module.exports = {
if (context.webdriver.standalone) {
logger.debug('Starting Selenium Standalone')
return new Promise((resolve, reject) => {
const standalone = require('selenium-standalone')
const spawnOptions = { stdio: 'inherit' }
const { version, drivers } = context.webdriver || {}

Expand Down Expand Up @@ -78,8 +82,6 @@ module.exports = {
async beforeEach (_, context) {
try {
logger.debug('Adding WebDriver integrations')
const zalenium = require('./integrations/zalenium')
const appium = require('./integrations/appium')

// Add enabled integrations to the integrations array so they can be used
// later.
Expand All @@ -99,7 +101,6 @@ module.exports = {
logger.debug('Creating WebdriverIO browser instance')

// Set up the browser instance and add it to the test context.
const { remote } = require('webdriverio')
context.testContext.browser = await remote({
path: '/wd/hub',
...context.webdriver,
Expand Down Expand Up @@ -144,7 +145,6 @@ module.exports = {
if (context.err) {
logger.write('\n')
logger.log('👉', 'bff-webdriver: Running manual cleanup')
const cleanup = require('./cleanup')
await cleanup()
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/bff-webdriver/integrations/appium.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const { createLogger } = require('@generates/logger')
import { createLogger } from '@generates/logger'

const logger = createLogger({
level: 'info',
namespace: 'bff.webdriver.appium'
})

module.exports = function appium (context) {
export default function appium (context) {
logger.debug('Appium integration enabled')

// Define the global capability options.
Expand Down
6 changes: 3 additions & 3 deletions packages/bff-webdriver/integrations/zalenium.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const { createLogger } = require('@generates/logger')
import { createLogger } from '@generates/logger'
import { oneLine } from 'common-tags'

const logger = createLogger({
level: 'info',
namespace: 'bff.webdriver.zalenium'
})

module.exports = function zalenium (context) {
export default function zalenium (context) {
logger.debug('Zalenium integration enabled')

context.webdriver.integrations.push({
Expand All @@ -19,7 +20,6 @@ module.exports = function zalenium (context) {
if (testContext.result.failed && webdriver.zalenium.dashboardUrl) {
// If the test failed, log the Zalenium Dashboard URL for this
// session to make it easier for the user to debug.
const { oneLine } = require('common-tags')
const query = oneLine`
${testContext.capability['zal:name']}
${testContext.capability['zal:build']}
Expand Down
3 changes: 2 additions & 1 deletion packages/bff-webdriver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"bin": {
"bff-webdriver": "cli.js"
},
"main": "index.js",
"type": "module",
"module": "index.js",
"scripts": {
"test": "bff",
"bff-webdriver": "./cli.js",
Expand Down
6 changes: 3 additions & 3 deletions packages/bff-webdriver/tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { test } = require('@ianwalter/bff')
const { createApp } = require('@ianwalter/nrg')
const createUrl = require('@ianwalter/url')
import { test } from '@ianwalter/bff'
import { createApp } from '@ianwalter/nrg'
import createUrl from '@ianwalter/url'

test('test server', async t => {
const app = createApp({ log: false })
Expand Down
15 changes: 7 additions & 8 deletions packages/bff/cli.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/usr/bin/env node

const { promises: fs } = require('fs')
const path = require('path')
const cli = require('@ianwalter/cli')
const { createLogger, chalk } = require('@generates/logger')
const bff = require('.')
import { promises as fs } from 'fs'
import path from 'path'
import cli from '@ianwalter/cli'
import { createLogger, chalk } from '@generates/logger'
import camaro from 'camaro'
import junitBuilder from 'junit-report-builder'
import * as bff from './index.js'

// Set stdout to blocking so that the program doesn't exit with log statements
// still waiting to be logged to the console.
Expand Down Expand Up @@ -102,7 +104,6 @@ async function run () {

// Only run tests marked as failed in a JUnit file.
if (config.failed) {
const camaro = require('camaro')
const file = typeof config.failed === 'string' ? config.failed : 'junit.xml'
const xml = await fs.readFile(path.resolve(file), 'utf8')
const template = { failed: ['//testcase[failure]', '@name'] }
Expand Down Expand Up @@ -163,8 +164,6 @@ async function run () {

// If configured, generate a junit XML report file based on the test results.
if (config.junit) {
const junitBuilder = require('junit-report-builder')

// Determine the junit report file path.
const junit = typeof config.junit === 'string' ? config.junit : 'junit.xml'

Expand Down
37 changes: 19 additions & 18 deletions packages/bff/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
const readline = require('readline')
const path = require('path')
const workerpool = require('workerpool')
const glob = require('tiny-glob')
const { createLogger, chalk } = require('@generates/logger')
const { oneLine } = require('common-tags')
const pSeries = require('p-series')
const { SnapshotState } = require('jest-snapshot')
const merge = require('@ianwalter/merge')
const callsites = require('callsites')
const shuffle = require('array-shuffle')

import readline from 'readline'
import path from 'path'
import { fileURLToPath } from 'url'
import workerpool from 'workerpool'
import glob from 'tiny-glob'
import { createLogger, chalk } from '@generates/logger'
import { oneLine } from 'common-tags'
import pSeries from 'p-series'
import jestSnapshot from 'jest-snapshot'
import merge from '@ianwalter/merge'
import callsites from 'callsites'
import shuffle from 'array-shuffle'
import toHookRun from './lib/toHookRun.js'

const { SnapshotState } = jestSnapshot
const defaultFiles = ['**/*@(.pptr|.play|tests).?(m|c)js']

class FailFastError extends Error {
export class FailFastError extends Error {
constructor () {
super(FailFastError.message)
}
Expand All @@ -23,7 +26,7 @@ FailFastError.message = 'Run failed immediately since failFast option is set'
* Collects test names from test files and assigns them to a worker in a
* worker pool that runs the associated test.
*/
async function run (config) {
export async function run (config) {
// Create the run context using the passed configuration and defaults.
const context = {
tests: defaultFiles,
Expand Down Expand Up @@ -71,6 +74,7 @@ async function run (config) {
}

// Set the path to the file used to create a worker.
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const workerPath = path.join(__dirname, 'worker.js')

// For registering individual tests exported from test files:
Expand Down Expand Up @@ -112,7 +116,6 @@ async function run (config) {
})

// Sequentially run any before hooks specified by plugins.
const toHookRun = require('./lib/toHookRun')
if (context.plugins && context.plugins.length) {
await pSeries(context.plugins.map(toHookRun('before', context)))
}
Expand Down Expand Up @@ -285,7 +288,7 @@ function handleTestArgs (name, tags, test = {}) {
}
}

function test (name, ...tags) {
export function test (name, ...tags) {
return handleTestArgs(name, tags)
}

Expand All @@ -300,5 +303,3 @@ test.only = function only (name, ...tags) {
test.warn = function warn (name, ...tags) {
return handleTestArgs(name, tags, { warn: true })
}

module.exports = { run, test, FailFastError }
14 changes: 8 additions & 6 deletions packages/bff/lib/enhanceTestContext.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
const expect = require('expect')
import expect from 'expect'
import jestSnapshot from 'jest-snapshot'
import clone from '@ianwalter/clone'
import { Subpub } from '@ianwalter/subpub'
import sleep from '@ianwalter/sleep'

const {
SnapshotState,
addSerializer,
toMatchSnapshot,
toMatchInlineSnapshot,
toThrowErrorMatchingSnapshot,
toThrowErrorMatchingInlineSnapshot
} = require('jest-snapshot')
const clone = require('@ianwalter/clone')
const { Subpub } = require('@ianwalter/subpub')
const sleep = require('@ianwalter/sleep')
} = jestSnapshot

module.exports = function enhanceTestContext (testContext) {
export default function enhanceTestContext (testContext) {
// Add a Subpub instance to the testContext so it can listen for the 'done'
// event.
testContext.sp = new Subpub()
Expand Down
8 changes: 5 additions & 3 deletions packages/bff/lib/runTest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const { utils } = require('jest-snapshot')
const cloneable = require('@ianwalter/cloneable')
import jestSnapshot from 'jest-snapshot'
import cloneable from '@ianwalter/cloneable'

module.exports = async function runTest (testContext, testFn) {
const { utils } = jestSnapshot

export default async function runTest (testContext, testFn) {
function done () {
testContext.sp.pub('done')
}
Expand Down
Loading

0 comments on commit ee00829

Please sign in to comment.