Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoinitialize test context #559

Merged
merged 2 commits into from
Sep 6, 2023
Merged
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
7 changes: 3 additions & 4 deletions api/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4271,6 +4271,9 @@ declare module "socket:hooks" {
declare module "socket:test/fast-deep-equal" {
export default function equal(a: any, b: any): boolean;
}
declare module "socket:test/context" {
export default function _default(GLOBAL_TEST_RUNNER: any): void;
}
declare module "socket:test/index" {
export function getDefaultTestRunnerTimeout(): number;
/**
Expand Down Expand Up @@ -5588,10 +5591,6 @@ declare module "socket:internal/worker" {
const undefined: any;
export default undefined;
}
declare module "socket:test/context" {
const _default: any;
export default _default;
}
declare module "socket:test/harness" {
/**
* @typedef {import('./index').Test} Test
Expand Down
53 changes: 29 additions & 24 deletions api/test/context.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,11 @@
import { GLOBAL_TEST_RUNNER } from './index.js'
import console from '../console.js'
import process from '../process.js'
import ipc from '../ipc.js'

import '../application.js'

export default null

if (process.env.SOCKET_DEBUG_IPC) {
ipc.debug.enabled = true
ipc.debug.log = (...args) => console.log(...args)
}

if (typeof globalThis?.addEventListener === 'function') {
globalThis.addEventListener('error', onerror)
globalThis.addEventListener('messageerror', onerror)
globalThis.addEventListener('unhandledrejection', onerror)
}

let finishing = false
GLOBAL_TEST_RUNNER.onFinish(({ fail }) => {
if (!finishing) {
finishing = true
if (!process.env.DEBUG) {
setTimeout(() => {
process.exit(fail > 0 ? 1 : 0)
}, 1024) // give app time to print TAP output
}
}
})
let isInitialized = false

function onerror (e) {
const err = e.error || e.stack || e.reason || e.message || e
Expand All @@ -40,3 +17,31 @@ function onerror (e) {
})
}
}

export default function (GLOBAL_TEST_RUNNER) {
if (isInitialized) return

if (process.env.SOCKET_DEBUG_IPC) {
ipc.debug.enabled = true
ipc.debug.log = (...args) => console.log(...args)
}

if (typeof globalThis?.addEventListener === 'function') {
globalThis.addEventListener('error', onerror)
globalThis.addEventListener('messageerror', onerror)
globalThis.addEventListener('unhandledrejection', onerror)
}

GLOBAL_TEST_RUNNER.onFinish(({ fail }) => {
if (!finishing) {
finishing = true
if (!process.env.DEBUG) {
setTimeout(() => {
process.exit(fail > 0 ? 1 : 0)
}, 1024) // give app time to print TAP output
}
}
})

isInitialized = true
}
3 changes: 3 additions & 0 deletions api/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { format } from '../util.js'
import deepEqual from './fast-deep-equal.js'
import process from '../process.js'
import os from '../os.js'
import initContext from './context.js'

const {
SOCKET_TEST_RUNNER_TIMEOUT = getDefaultTestRunnerTimeout()
Expand Down Expand Up @@ -493,6 +494,8 @@ function printLine (line) {

export const GLOBAL_TEST_RUNNER = new TestRunner()

initContext(GLOBAL_TEST_RUNNER)

/**
* @param {string} name
* @param {TestFn} [fn]
Expand Down
3 changes: 0 additions & 3 deletions test/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* eslint-disable import/first */
import 'socket:test/context' // this should be first

import './webview.js'
import './hooks.js'
import './diagnostics.js'
Expand Down
Loading