Skip to content

Commit

Permalink
fix incompatibilities with vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
KorbinianK committed May 1, 2024
1 parent 0251283 commit b5db617
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
29 changes: 29 additions & 0 deletions packages/bridge-ui/scripts/setupTestEnvironmentCompatibility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Source: https://github.com/vitest-dev/vitest/issues/4043#issuecomment-1905172846

// This file is needed in order to support Uint8Array with vitest and jsdom.
if (process.env.VITEST === 'true') {
class ESBuildAndJSDOMCompatibleTextEncoder extends TextEncoder {
constructor() {
super();
}

encode(input: string) {
if (typeof input !== 'string') {
throw new TypeError('`input` must be a string');
}

const decodedURI = decodeURIComponent(encodeURIComponent(input));
const arr = new Uint8Array(decodedURI.length);
const chars = decodedURI.split('');
for (let i = 0; i < chars.length; i++) {
arr[i] = decodedURI[i].charCodeAt(0);
}
return arr;
}
}

Object.defineProperty(global, 'TextEncoder', {
value: ESBuildAndJSDOMCompatibleTextEncoder,
writable: true,
});
}
2 changes: 1 addition & 1 deletion packages/bridge-ui/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default defineProject({
test: {
environment: 'jsdom',
// setupFiles: ['./../../setup.ts'],
setupFiles: ['./src/tests/setup.ts'],
setupFiles: ['./scripts/setupTestEnvironmentCompatibility.ts', './src/tests/setup.ts'],
globals: true,
include: ['./**/*.{test,spec}.{js,ts}'],
},
Expand Down

0 comments on commit b5db617

Please sign in to comment.