On This Page

Testing

Import from @neontechspace/fluxerly/testing

Test Fluxer bots built with the default API without a network, a token or a Fluxer account

Import from @neontechspace/fluxerly/testing in application tests

A test client is a real client from createClient whose HTTP and WebSocket traffic goes to an in-memory transport through the transport option. Events therefore pass through the SDK's own decoders, caches, handlers and logging. The fake gateway speaks protocol v1: It sends HELLO, answers Identify with READY and Resume with RESUMED, acknowledges heartbeats and delivers emitted dispatches with increasing sequence numbers. The fake HTTP side serves hosted Fluxer's discovery document, with presigned uploads off so attachments arrive as one multipart request, answers requests from registered responses and records every request without its Authorization header. Message sends and edits without a registered response receive an echoed bot message, and other unmatched requests receive a Fluxer-shaped 404 and a testing.unmatchedRequest Warn record. Waits such as idle and TestRoute.next have no deadline unless timeoutMs is passed, so the test runner's timeout bounds a test that hangs. Log records go to logs() and to any sinks in the logging option instead of the console. Shutdown stops the client and closes the transport, leaving no sockets, timers or listeners behind, and rejects when a handler or command failed without an onError hook and the test did not read that failure from failures()

import assert from "node:assert/strict"
import { createTestClient } from "@neontechspace/fluxerly/testing"

await using test = createTestClient()
const replies = test.rest.respond("POST /channels/:id/messages", { body: test.fixtures.message({ content: "Pong!" }) })
test.client.on("messageCreate", (message) => message.content === "!ping" ? test.client.messages.reply(message, { content: "Pong!" }) : undefined)
await test.ready()
test.emit("MESSAGE_CREATE", test.fixtures.message({ content: "!ping" }))
// Handlers run after emit returns, so wait for the reply before asserting on it
const request = await replies.next()
assert.equal(request.body.content, "Pong!")

// A message the bot ignores sends nothing, which idle confirms without a fixed sleep
test.emit("MESSAGE_CREATE", test.fixtures.message({ content: "hello" }))
await test.idle()
assert.equal(replies.requests().length, 1)
import assert from "node:assert/strict"
import { createTestClient } from "@neontechspace/fluxerly/testing"

await using test = createTestClient()
const replies = test.rest.respond("POST /channels/:id/messages", { body: test.fixtures.message({ content: "Pong!" }) })
test.client.on("messageCreate", (message) =>
    message.content === "!ping" ? test.client.messages.reply(message, { content: "Pong!" }) : undefined,
)
await test.ready()
test.emit("MESSAGE_CREATE", test.fixtures.message({ content: "!ping" }))
// Handlers run after emit returns, so wait for the reply before asserting on it
const request = await replies.next()
assert.equal((request.body as { content?: unknown }).content, "Pong!")

// A message the bot ignores sends nothing, which idle confirms without a fixed sleep
test.emit("MESSAGE_CREATE", test.fixtures.message({ content: "hello" }))
await test.idle()
assert.equal(replies.requests().length, 1)

Categories#

  • Testing

    36 symbols

    createFixtures createTestBot createTestClient TestTimeoutError UnhandledTestFailuresError WireOverrides