Closed brennanbutler01 closed 11 months ago
I was able to fix this with vitest by adding the @originjs/vite-plugin-commonjs plugin to my project:
// vitest.config.ts
import { defineConfig } from 'vitest/config';
import path from 'path';
import react from '@vitejs/plugin-react';
import { viteCommonjs } from '@originjs/vite-plugin-commonjs';
export default defineConfig({
test: {
globals: true,
environment: 'jsdom',
exclude: ['**/node_modules/**', './src/e2e/**', '.next/**'],
coverage: {
provider: 'v8',
reporter: ['text', 'json', 'html'],
lines: 100,
functions: 100,
branches: 100,
statements: 100,
exclude: ['**/src/yourIgnoredFile.js'],
},
setupFiles: ['./vitest.setup.ts'],
server: {
deps: {
inline: ['radar-sdk-js'],
},
},
},
resolve: {
alias: {
'~': path.resolve(__dirname, './src'),
},
mainFields: ['module', 'main'],
},
plugins: [react(), viteCommonjs({ include: ['radar-sdk-js'] })],
});
Alternatively, things were working when I modified radar-sdk-js package.json to have a main entry like:
// node_modules/radar-sdk-js/package.json
{
"name": "radar-sdk-js",
"version": "4.1.6",
"description": "Web Javascript SDK for Radar, location infrastructure for mobile and web apps.",
"homepage": "https://radar.com",
"type": "module",
"module": "dist/radar.js",
"main": "dist/radar.js",
...
For this to all work, I needed to make a few extra changes to my vitest.setup.ts to stop errors from being thrown:
// vitest.setup.ts
//https://stackoverflow.com/questions/52968969/jest-url-createobjecturl-is-not-a-function fix for radar
Object.defineProperty(window.URL, 'createObjectURL', { value() {} });
// https://stackoverflow.com/questions/52868727/how-to-mock-window-navigator-language-using-jest fix for radar
Object.defineProperty(window.navigator, 'permissions', {
writable: true,
value: { query: vi.fn().mockImplementationOnce(() => Promise.resolve({ state: 'granted' })) },
});
I hope this helps someone
Hello,
I created a new beta release 4.2.0-beta.0
which adds support for UMD if you want to try and see if that resolves the issue.
@kochis my hero.... the beta fixes the test errors. thank you!!
Help Needed configuring jest + Radar
I'm trying to use Radar for autocomplete and address validation, but am running into the following error when running jest:
Test suite failed to run. Cannot find module 'radar-sdk-js' from 'fetchAutocomplete/index.tsx'
In that file, I am importing radar-sdk-js as
import Radar from 'radar-sdk-js'
I'm using Next js and my jest config is
I have tried uninstalling and reinstalling Radar. It works fine when I include it as html rather than using yarn. I tried adding a module name mapper of
^radar-sdk-js': '<rootDir>/node_modules/radar-sdk-js'
but that isn't working.I really like the library and want to keep using it, but I'm not sure how to go about making this work. any help would be appreciated.
Thank you
i'm using jest ^29.5.0
I made some changes to jest config and tried:
This got me a new error: