Skip to content

Commit

Permalink
Refactor tests so that the stx tests also run in NodeJS
Browse files Browse the repository at this point in the history
It would be nice to have tests split up into modules, but this causes
all kinds of headaches with running the tests in both the browser and
NodeJS. So we're keeping them in one file for now.
  • Loading branch information
jcbrand committed Dec 16, 2024
1 parent 172fea3 commit 29a50af
Show file tree
Hide file tree
Showing 8 changed files with 354 additions and 328 deletions.
1 change: 0 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module.exports = function (config) {
'node_modules/sinon/pkg/sinon.js',
'dist/strophe.umd.js',
'tests/tests.js',
'tests/stx.js'
],

// list of files to exclude
Expand Down
2 changes: 1 addition & 1 deletion src/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class Builder {
* @example const stanza = Builder.fromString('<presence from="juliet@example.com/chamber"></presence>');
*/
static fromString(str) {
const el = toElement(str);
const el = toElement(str, true);
const b = new Builder('');
b.#nodeTree = el;
return b;
Expand Down
32 changes: 29 additions & 3 deletions src/shims.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,38 @@ function getWebSocketImplementation() {
if (typeof globalThis.WebSocket === 'undefined') {
try {
return require('ws');
} catch (e) { // eslint-disable-line no-unused-vars
// eslint-disable-next-line no-unused-vars
} catch (e) {
throw new Error('You must install the "ws" package to use Strophe in nodejs.');
}
}
return globalThis.WebSocket;
}
export const WebSocket = getWebSocketImplementation();

/**
* Retrieves the XMLSerializer implementation for the current environment.
*
* In browser environments, it uses the built-in XMLSerializer.
* In Node.js environments, it attempts to load the 'jsdom' package
* to create a compatible XMLSerializer.
*/
function getXMLSerializerImplementation() {
if (typeof globalThis.XMLSerializer === 'undefined') {
let JSDOM;
try {
JSDOM = require('jsdom').JSDOM;
// eslint-disable-next-line no-unused-vars
} catch (e) {
throw new Error('You must install the "ws" package to use Strophe in nodejs.');
}
const dom = new JSDOM('');
return dom.window.XMLSerializer;
}
return globalThis.XMLSerializer;
}
export const XMLSerializer = getXMLSerializerImplementation();

/**
* DOMParser
* https://w3c.github.io/DOM-Parsing/#the-domparser-interface
Expand All @@ -52,7 +76,8 @@ function getDOMParserImplementation() {
let JSDOM;
try {
JSDOM = require('jsdom').JSDOM;
} catch (e) { // eslint-disable-line no-unused-vars
// eslint-disable-next-line no-unused-vars
} catch (e) {
throw new Error('You must install the "jsdom" package to use Strophe in nodejs.');
}
const dom = new JSDOM('');
Expand All @@ -75,7 +100,8 @@ export function getDummyXMLDOMDocument() {
let JSDOM;
try {
JSDOM = require('jsdom').JSDOM;
} catch (e) { // eslint-disable-line no-unused-vars
// eslint-disable-next-line no-unused-vars
} catch (e) {
throw new Error('You must install the "jsdom" package to use Strophe in nodejs.');
}
const dom = new JSDOM('');
Expand Down
2 changes: 1 addition & 1 deletion src/types/builder.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ declare class Builder {
* Creates a new Builder object from an XML string.
* @param {string} str
* @returns {Builder}
* @example const stanza = Builder.fromString('<presence from='juliet@example.com/chamber'></presence>');
* @example const stanza = Builder.fromString('<presence from="juliet@example.com/chamber"></presence>');
*/
static fromString(str: string): Builder;
/**
Expand Down
4 changes: 4 additions & 0 deletions src/types/shims.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export const WebSocket: {
readonly CLOSING: 2;
readonly CLOSED: 3;
} | typeof import("ws");
export const XMLSerializer: {
new (): XMLSerializer;
prototype: XMLSerializer;
};
export const DOMParser: {
new (): DOMParser;
prototype: DOMParser;
Expand Down
2 changes: 1 addition & 1 deletion src/types/stanza.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class Stanza extends Builder {
* const pres = stx`
* <presence from='juliet@example.com/chamber' id='pres1'>
* <show>dnd</show>
* ${unsafeXML(status)
* ${unsafeXML(status)}
* </presence>`;
* connection.send(pres);
*/
Expand Down
276 changes: 0 additions & 276 deletions tests/stx.js

This file was deleted.

Loading

0 comments on commit 29a50af

Please sign in to comment.