Files
speckle-server/packages/server/test/plugins/graphql.ts
T
Kristaps Fabians Geikins bde148f286 chore(server): migrating fully to ESM (#5042)
* wip

* some extra fixes

* stuff kinda works?

* need to figure out mocks

* need to figure out mocks

* fix db listener

* gqlgen fix

* minor gqlgen watch adjustment

* lint fixes

* delete old codegen file

* converting migrations to ESM

* getModuleDIrectory

* vitest sort of works

* added back ts-vitest

* resolve gql double load

* fixing test timeout configs

* TSC lint fix

* fix automate tests

* moar debugging

* debugging

* more debugging

* codegen update

* server works

* yargs migrated

* chore(server): getting rid of global mocks for Server ESM (#5046)

* got rid of email mock

* got rid of comment mocks

* got rid of multi region mocks

* got rid of stripe mock

* admin override mock updated

* removed final mock

* fixing import.meta.resolve calls

* another import.meta.resolve fix

* added requested test

* nyc ESM fix

* removed unneeded deps + linting

* yarn lock forgot to commit

* tryna fix flakyness

* email capture util fix

* sendEmail fix

* fix TSX check

* sender transporter fix + CR comments

* merge main fix

* test fixx

* circleci fix

* gqlgen bigint fix

* error formatter fix

* more error formatting improvements

* esmloader added to Dockerfile

* more dockerfile fixes

* bg jobs fix
2025-07-14 10:26:19 +03:00

108 lines
3.2 KiB
TypeScript

import { Optional } from '@/modules/shared/helpers/typeHelper'
import { ExecuteOperationResponse } from '@/test/graphqlHelper'
import { AssertionError } from 'chai'
import { isString } from 'lodash-es'
type ChaiPluginThis<O = Record<string, unknown>> = {
__flags: {
message: Optional<string>
negate: Optional<boolean>
object: O
}
}
/**
* Adds various useful assertions for GraphQL API integration tests
*/
const graphqlChaiPlugin: Chai.ChaiPlugin = (_chai, utils) => {
const { Assertion } = _chai
utils.addMethod(
Assertion.prototype,
'haveGraphQLErrors',
function (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this: ChaiPluginThis<ExecuteOperationResponse<any>>,
messageOrOptions?: string | ({ message: string } | { code: string })
) {
const message = isString(messageOrOptions)
? messageOrOptions
: messageOrOptions && 'message' in messageOrOptions
? messageOrOptions.message
: undefined
const code =
messageOrOptions && !isString(messageOrOptions) && 'code' in messageOrOptions
? messageOrOptions.code
: undefined
const { negate, object } = this.__flags
const { errors } = object
const shouldNotHaveErrors = negate
const shouldHaveErrors = !negate
const errorsArr = errors || []
try {
if (shouldNotHaveErrors) {
new Assertion(errorsArr).to.have.lengthOf(0)
} else {
new Assertion(errorsArr).to.have.lengthOf.greaterThanOrEqual(1)
}
if (message) {
if (shouldNotHaveErrors) {
new Assertion(
errorsArr.map((e) => e.message.toLowerCase()).join('\n')
).to.not.contain(message.toLowerCase())
} else {
new Assertion(
errorsArr.map((e) => e.message.toLowerCase()).join('\n')
).to.contain(message.toLowerCase())
}
}
if (code) {
const errorCodes = errorsArr.map((e) => `${e.extensions?.code}`).join('\n')
if (shouldNotHaveErrors) {
new Assertion(errorCodes).to.not.contain(code)
} else {
new Assertion(errorCodes).to.contain(code)
}
}
} catch (e) {
if (!(e instanceof AssertionError)) {
throw e
}
const getPrettyErrors = () => `\nErrors: ${JSON.stringify(errorsArr, null, 2)}`
const msgOrCode = message || code
let msg = ''
if (shouldHaveErrors) {
if (msgOrCode) {
msg = `Expected GraphQL response to have errors containing "${msgOrCode}", but`
msg += errorsArr.length
? ' only found others' + getPrettyErrors()
: ' found none'
} else {
msg = 'Expected GraphQL response to have errors, but found none'
}
} else {
if (msgOrCode) {
msg = `Expected GraphQL response to have no errors containing "${msgOrCode}", but found some`
} else {
msg = 'Expected GraphQL response to have no errors, but found some'
}
msg += getPrettyErrors()
}
throw new AssertionError(msg)
}
}
)
}
export default graphqlChaiPlugin