Skip to content

Commit 5e5673f

Browse files
committed
test: added test for bundler compatibility
1 parent 58c08a6 commit 5e5673f

4 files changed

Lines changed: 67 additions & 1 deletion

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { describe, test, expect } from 'vitest';
2+
import { execSync } from 'node:child_process';
3+
import path from 'node:path';
4+
import fs from 'node:fs';
5+
6+
describe('Vite bundler compatibility', () => {
7+
test('should rename sqlite3.wasm with a hash and update the import URL', () => {
8+
const testDir = path.resolve(__dirname, 'vite-repro');
9+
const distDir = path.resolve(testDir, 'dist');
10+
11+
// Clean up previous build
12+
if (fs.existsSync(distDir)) {
13+
fs.rmSync(distDir, { recursive: true, force: true });
14+
}
15+
16+
// Run vite build
17+
// We use npx to ensure we use the project's vite
18+
execSync('npx vite build', { cwd: testDir, stdio: 'inherit' });
19+
20+
// 1. Check if hashed WASM file exists in dist/assets
21+
const assetsDir = path.resolve(distDir, 'assets');
22+
const files = fs.readdirSync(assetsDir);
23+
const wasmFile = files.find(
24+
(f) => f.startsWith('sqlite3-') && f.endsWith('.wasm'),
25+
);
26+
27+
expect(wasmFile).toBeDefined();
28+
console.log('Found hashed WASM file:', wasmFile);
29+
30+
// 2. Check if the JS bundle contains the hashed WASM filename
31+
const assetsDirJs = path.resolve(distDir, 'assets');
32+
const jsFiles = fs
33+
.readdirSync(assetsDirJs)
34+
.filter((f) => f.endsWith('.js'));
35+
const mainBundle = jsFiles.find((f) => f.startsWith('index-'));
36+
expect(mainBundle).toBeDefined();
37+
38+
const bundleContent = fs.readFileSync(
39+
path.resolve(assetsDirJs, mainBundle),
40+
'utf8',
41+
);
42+
43+
// It should contain something like: new URL("/assets/sqlite3-hash.wasm", import.meta.url)
44+
// Vite might use different quotes or spacing, but it should definitely have the hashed filename.
45+
expect(bundleContent).toContain(wasmFile);
46+
47+
// Specifically check that it's part of a new URL call or at least correctly referenced
48+
// In our previous check it was: new URL("/assets/sqlite3-Bguoklsa.wasm",import.meta.url)
49+
const urlPattern = new RegExp(
50+
`new URL\\(".*${wasmFile}",\\s*import\\.meta\\.url\\)`,
51+
);
52+
expect(bundleContent).toMatch(urlPattern);
53+
});
54+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!doctype html>
2+
<html>
3+
<body>
4+
<script type="module" src="./main.js"></script>
5+
</body>
6+
</html>

src/__tests__/vite-repro/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import sqlite3InitModule from '../../../src/index.js';
2+
3+
await sqlite3InitModule();

vitest.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ const vitestConfig: ViteUserConfig = defineConfig({
2626
test: {
2727
name: 'node',
2828
environment: 'node',
29-
include: ['src/__tests__/sqlite3-node.test.js'],
29+
include: [
30+
'src/__tests__/sqlite3-node.test.js',
31+
'src/__tests__/bundler-compatibility.test.js',
32+
],
3033
},
3134
},
3235
{

0 commit comments

Comments
 (0)