Testing React-Capacitor App with Jest getting me wasm file error #406
-
Hi y'all 👋 I use this library within my application. I am hoping to write some unit tests with Jest where each test has a db instance that I can seed and use to test. I've ran into an issue implementing this db instance in my test file where jeep-sqlite cannot load the specified wasm file. Here is my test file I am trying to implement import { resolve } from 'path';
import { CapacitorSQLite, SQLiteConnection, SQLiteDBConnection } from '@capacitor-community/sqlite';
import { defineCustomElements as jeepSqlite, applyPolyfills, JSX as LocalJSX } from 'jeep-sqlite/loader';
// ...
const dbName = config.databaseName;
type StencilToReact<T> = {
[P in keyof T]?: T[P] & Omit<HTMLAttributes<Element>, 'className'> & {
class?: string;
};
} ;
declare global {
export namespace JSX {
interface IntrinsicElements extends StencilToReact<LocalJSX.IntrinsicElements> {
}
}
}
applyPolyfills().then(() => {
jeepSqlite(window, {});
});
it('test', async () => {
const sqlite: SQLiteConnection = new SQLiteConnection(CapacitorSQLite);
const jeepEl = document.createElement('jeep-sqlite');
document.body.appendChild(jeepEl);
const element = await customElements.whenDefined('jeep-sqlite');
const assetPath = resolve(`${__dirname}/../../../public/assets`); // we've also tried '.../assets/sql-wasm.wasm'
jeepEl.setAttribute('wasmPath', assetPath ); // attempt to set the wasmPath
const sqlite: SQLiteConnection = new SQLiteConnection(CapacitorSQLite);
await sqlite.initWebStore();
const ret = await sqlite.checkConnectionsConsistency();
const isConn = (await sqlite.isConnection(dbName, false)).result;
let db: SQLiteDBConnection;
if (ret.result && isConn) {
db = await sqlite.retrieveConnection(dbName, false);
} else {
db = await sqlite.createConnection(dbName, false, 'no-encryption', 1, false);
}
try {
await db.open();
} catch (err) {
// ...
}
// ...
} And we get the following errors:
So I guess a two questions:
Any hints would be highly appreciated! Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
@Brandi-Youreka I never run jest test , but i do not see something wrong to try like you do
must work i think the problem comes from the assetPath given, the error you have is that it cannot load the wasm file |
Beta Was this translation helpful? Give feedback.
Hello, sorry for the late answer.
Yes I did find an answer. The solution that worked was spinning up a server before running the test. So we added the following scripts to our package.json:
Then we ran
npm run test:serve
when running the test. Thetest:serve
script serves a local host that jeepSqlite is trying to reach out to in order to retrieve thesql-wasm.wasm
file.