From 42d00fa9f694067913f22bc90560b145d64e4175 Mon Sep 17 00:00:00 2001 From: Brian Carlson Date: Mon, 21 Apr 2025 17:24:03 -0500 Subject: [PATCH] Fit vitest into existing legacy framework --- docs/package.json | 4 +- packages/pg/Makefile | 2 +- packages/pg/test/cloudflare/vitest-cf.test.ts | 3 +- packages/pg/test/test-helper.js | 225 +++++++++--------- packages/pg/test/vitest.config.mts | 1 + 5 files changed, 119 insertions(+), 116 deletions(-) diff --git a/docs/package.json b/docs/package.json index dec5cceb..69fb9ab0 100644 --- a/docs/package.json +++ b/docs/package.json @@ -12,8 +12,8 @@ "license": "ISC", "dependencies": { "next": "^12.3.1", - "nextra": "2.0.0-beta.29", - "nextra-theme-docs": "2.0.0-beta.29", + "nextra": "4.2.17", + "nextra-theme-docs": "4.2.17", "react": "^17.0.1", "react-dom": "^17.0.1" } diff --git a/packages/pg/Makefile b/packages/pg/Makefile index dae2b185..7f4aebfa 100644 --- a/packages/pg/Makefile +++ b/packages/pg/Makefile @@ -55,4 +55,4 @@ test-pool: test-worker: @echo "***Testing Cloudflare Worker support***" - yarn vitest -c test/vitest.config.mts test/cloudflare/ --no-watch + yarn vitest run -c test/vitest.config.mts test/cloudflare/ --no-watch -- $(params) diff --git a/packages/pg/test/cloudflare/vitest-cf.test.ts b/packages/pg/test/cloudflare/vitest-cf.test.ts index b9bc1454..3f2f8adb 100644 --- a/packages/pg/test/cloudflare/vitest-cf.test.ts +++ b/packages/pg/test/cloudflare/vitest-cf.test.ts @@ -1,9 +1,10 @@ import { Pool } from 'pg' import { test } from 'vitest' import assert from 'node:assert' +import args from '../cli' test('default', async () => { - const pool = new Pool() + const pool = new Pool(args) const result = await pool.query('SELECT $1::text as name', ['cloudflare']) assert(result.rows[0].name === 'cloudflare') diff --git a/packages/pg/test/test-helper.js b/packages/pg/test/test-helper.js index e4e98771..dfe56dd3 100644 --- a/packages/pg/test/test-helper.js +++ b/packages/pg/test/test-helper.js @@ -16,111 +16,6 @@ process.on('uncaughtException', function (d) { } process.exit(-1) }) - -assert.same = function (actual, expected) { - for (var key in expected) { - assert.equal(actual[key], expected[key]) - } -} - -assert.emits = function (item, eventName, callback, message) { - var called = false - var id = setTimeout(function () { - test("Should have called '" + eventName + "' event", function () { - assert.ok(called, message || "Expected '" + eventName + "' to be called.") - }) - }, 5000) - - item.once(eventName, function () { - if (eventName === 'error') { - // belt and braces test to ensure all error events return an error - assert.ok( - arguments[0] instanceof Error, - 'Expected error events to throw instances of Error but found: ' + sys.inspect(arguments[0]) - ) - } - called = true - clearTimeout(id) - assert.ok(true) - if (callback) { - callback.apply(item, arguments) - } - }) -} - -assert.UTCDate = function (actual, year, month, day, hours, min, sec, milisecond) { - var actualYear = actual.getUTCFullYear() - assert.equal(actualYear, year, 'expected year ' + year + ' but got ' + actualYear) - - var actualMonth = actual.getUTCMonth() - assert.equal(actualMonth, month, 'expected month ' + month + ' but got ' + actualMonth) - - var actualDate = actual.getUTCDate() - assert.equal(actualDate, day, 'expected day ' + day + ' but got ' + actualDate) - - var actualHours = actual.getUTCHours() - assert.equal(actualHours, hours, 'expected hours ' + hours + ' but got ' + actualHours) - - var actualMin = actual.getUTCMinutes() - assert.equal(actualMin, min, 'expected min ' + min + ' but got ' + actualMin) - - var actualSec = actual.getUTCSeconds() - assert.equal(actualSec, sec, 'expected sec ' + sec + ' but got ' + actualSec) - - var actualMili = actual.getUTCMilliseconds() - assert.equal(actualMili, milisecond, 'expected milisecond ' + milisecond + ' but got ' + actualMili) -} - -const spit = function (actual, expected) { - console.log('') - console.log('actual ' + sys.inspect(actual)) - console.log('expect ' + sys.inspect(expected)) - console.log('') -} - -assert.equalBuffers = function (actual, expected) { - if (actual.length != expected.length) { - spit(actual, expected) - assert.equal(actual.length, expected.length) - } - for (var i = 0; i < actual.length; i++) { - if (actual[i] != expected[i]) { - spit(actual, expected) - } - assert.equal(actual[i], expected[i]) - } -} - -assert.empty = function (actual) { - assert.lengthIs(actual, 0) -} - -assert.success = function (callback) { - if (callback.length === 1 || callback.length === 0) { - return assert.calls(function (err, arg) { - if (err) { - console.log(err) - } - assert(!err) - callback(arg) - }) - } else if (callback.length === 2) { - return assert.calls(function (err, arg1, arg2) { - if (err) { - console.log(err) - } - assert(!err) - callback(arg1, arg2) - }) - } else { - throw new Error('need to preserve arrity of wrapped function') - } -} - -assert.lengthIs = function (actual, expectedLength) { - assert.equal(actual.length, expectedLength) -} - var expect = function (callback, timeout) { var executed = false timeout = timeout || parseInt(process.env.TEST_TIMEOUT) || 5000 @@ -156,13 +51,6 @@ var expect = function (callback, timeout) { throw new Error('Unsupported arrity ' + callback.length) } } -assert.calls = expect - -assert.isNull = function (item, message) { - message = message || 'expected ' + item + ' to be null' - assert.ok(item === null, message) -} - // print out the filename process.stdout.write(require('path').basename(process.argv[1])) if (args.binary) process.stdout.write(' (binary)') @@ -198,6 +86,119 @@ const rejection = (promise) => (error) => error ) +if (Object.isExtensible(assert)) { + assert.same = function (actual, expected) { + for (var key in expected) { + assert.equal(actual[key], expected[key]) + } + } + + assert.emits = function (item, eventName, callback, message) { + var called = false + var id = setTimeout(function () { + test("Should have called '" + eventName + "' event", function () { + assert.ok(called, message || "Expected '" + eventName + "' to be called.") + }) + }, 5000) + + item.once(eventName, function () { + if (eventName === 'error') { + // belt and braces test to ensure all error events return an error + assert.ok( + arguments[0] instanceof Error, + 'Expected error events to throw instances of Error but found: ' + sys.inspect(arguments[0]) + ) + } + called = true + clearTimeout(id) + assert.ok(true) + if (callback) { + callback.apply(item, arguments) + } + }) + } + + assert.UTCDate = function (actual, year, month, day, hours, min, sec, milisecond) { + var actualYear = actual.getUTCFullYear() + assert.equal(actualYear, year, 'expected year ' + year + ' but got ' + actualYear) + + var actualMonth = actual.getUTCMonth() + assert.equal(actualMonth, month, 'expected month ' + month + ' but got ' + actualMonth) + + var actualDate = actual.getUTCDate() + assert.equal(actualDate, day, 'expected day ' + day + ' but got ' + actualDate) + + var actualHours = actual.getUTCHours() + assert.equal(actualHours, hours, 'expected hours ' + hours + ' but got ' + actualHours) + + var actualMin = actual.getUTCMinutes() + assert.equal(actualMin, min, 'expected min ' + min + ' but got ' + actualMin) + + var actualSec = actual.getUTCSeconds() + assert.equal(actualSec, sec, 'expected sec ' + sec + ' but got ' + actualSec) + + var actualMili = actual.getUTCMilliseconds() + assert.equal(actualMili, milisecond, 'expected milisecond ' + milisecond + ' but got ' + actualMili) + } + + const spit = function (actual, expected) { + console.log('') + console.log('actual ' + sys.inspect(actual)) + console.log('expect ' + sys.inspect(expected)) + console.log('') + } + + assert.equalBuffers = function (actual, expected) { + if (actual.length != expected.length) { + spit(actual, expected) + assert.equal(actual.length, expected.length) + } + for (var i = 0; i < actual.length; i++) { + if (actual[i] != expected[i]) { + spit(actual, expected) + } + assert.equal(actual[i], expected[i]) + } + } + + assert.empty = function (actual) { + assert.lengthIs(actual, 0) + } + + assert.success = function (callback) { + if (callback.length === 1 || callback.length === 0) { + return assert.calls(function (err, arg) { + if (err) { + console.log(err) + } + assert(!err) + callback(arg) + }) + } else if (callback.length === 2) { + return assert.calls(function (err, arg1, arg2) { + if (err) { + console.log(err) + } + assert(!err) + callback(arg1, arg2) + }) + } else { + throw new Error('need to preserve arrity of wrapped function') + } + } + + assert.lengthIs = function (actual, expectedLength) { + assert.equal(actual.length, expectedLength) + } + + assert.calls = expect + + assert.isNull = function (item, message) { + message = message || 'expected ' + item + ' to be null' + assert.ok(item === null, message) + } +} + module.exports = { Suite: Suite, pg: require('./../lib/'), diff --git a/packages/pg/test/vitest.config.mts b/packages/pg/test/vitest.config.mts index dc0c0d21..735cae83 100644 --- a/packages/pg/test/vitest.config.mts +++ b/packages/pg/test/vitest.config.mts @@ -2,6 +2,7 @@ import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config' export default defineWorkersConfig({ test: { + watch: false, poolOptions: { workers: { wrangler: { configPath: './wrangler.jsonc' },