Fit vitest into existing legacy framework

This commit is contained in:
Brian Carlson 2025-04-21 17:24:03 -05:00
parent 5a0f59cd32
commit 42d00fa9f6
5 changed files with 119 additions and 116 deletions

View File

@ -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"
}

View File

@ -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)

View File

@ -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')

View File

@ -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/'),

View File

@ -2,6 +2,7 @@ import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config'
export default defineWorkersConfig({
test: {
watch: false,
poolOptions: {
workers: {
wrangler: { configPath: './wrangler.jsonc' },