Closed m-nathani closed 6 months ago
I was exporting all the utils from a index.ts
file like :
utils/index.ts
...
export * from './sentry';
export * from './time-and-date';
export * from './error';
separating out the imports in file js/service/resevation-service.ts
for formattedAvailabilityTime
as follows passes the tests now:
import { getTrackingId, getFormattedDate, ... } from 'js/utils';
import { formattedAvailabilityTime } from 'js/utils/time-and-date';
Not sure if this is vite build issue or vitest thing... but certainly could not find the logical way out of it.
Too complicated to suggest anything useful, but can you at least share your vite config? Some plugins might be interfering with something.
Also do you use vi.mock
somewhere?
Hello @m-nathani. Please provide a minimal reproduction using a GitHub repository or StackBlitz (you can also use examples). Issues marked with needs reproduction
will be closed if they have no activity within 3 days.
@hi-ogawa using vi.mock
in setupTests, which is used as test.setupFiles
in the vite.config
added below:
setupTests.ts
...
// Use mock implementation from `./__mocks__/react-i18next.js`
vi.mock('react-i18next');
vi.mock('moment-timezone', async () => {
const moment: Moment = await vi.importActual('moment-timezone');
// Use same timezone and moment locale for all tests
(moment.tz as any).setDefault('Asia/Singapore');
moment.locale('en');
// Use the actual module in our tests
return moment;
});
...
vite.config.js
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react-swc';
import checker from 'vite-plugin-checker';
import { visualizer } from 'rollup-plugin-visualizer';
import { resolve } from 'path';
export default defineConfig(({ command, mode }) => ({
resolve: {
alias: {
js: resolve(__dirname, 'src/js/'),
'@components': resolve(__dirname, 'src/js/components'),
'@utils': resolve(__dirname, 'src/js/utils'),
'@store': resolve(__dirname, 'src/js/store'),
'@constants': resolve(__dirname, 'src/js/constants'),
'@api': resolve(__dirname, 'src/js/api'),
'@images': resolve(__dirname, 'images'),
'@contexts': resolve(__dirname, 'src/js/contexts'),
'@services': resolve(__dirname, 'src/js/services'),
'@styles': resolve(__dirname, 'src/js/styles'),
'@locales': resolve(__dirname, 'locales'),
},
},
plugins: [
react(),
// Only lint while using dev server,
// for deployments run eslint separately in Github action
command === 'serve' &&
!process.env.VITEST &&
checker({
overlay: {
initialIsOpen: false,
// Show panel on top of widget modal
badgeStyle: 'z-index: 1000000001;',
panelStyle: 'z-index: 1000000000;',
},
eslint: {
lintCommand: 'eslint "./src/js/**/*.{js,jsx,ts,tsx}"',
},
typescript: true,
}),
// Bundle analizer
mode === 'analyze' && visualizer(),
].filter(Boolean),
build: {
rollupOptions: {
input: {
embed: resolve(__dirname, 'src/js/embed.ts'),
widget: resolve(__dirname, 'src/js/widget.tsx'),
},
output: {
entryFileNames: '[name].min.js',
assetFileNames: (assetInfo) => {
const ext = assetInfo.name.split('.').pop();
if (ext === 'css') {
return '[name].min.css';
}
return assetInfo.name;
},
},
},
sourcemap: true,
target: 'es2015',
},
server: {
port: 8080,
},
define: {
// Use `__APP_VERSION__` to get the version instead of directly importing `package.json`
__APP_VERSION__: JSON.stringify(process.env.npm_package_version),
},
// Use Vitest instead of Jest as test runner
test: {
// describe, it, test, will be available without having to import (Similar to Jest)
globals: true,
environment: 'jsdom',
setupFiles: './src/setupTests.ts',
coverage: {
provider: 'v8',
},
},
}));
As one more tip to debug potential code transform bugs, you can try running it with VITE_NODE_DEBUG_DUMP=true
, which will output transformed code in .vite-node/dump
.
You might be able to find how __vite_ssr_import_2__.formattedAvailabilityTime
is used from there.
@hi-ogawa alright thanks... Will check on this tip and revert back to you. 🙏
Describe the bug
My test for a component is failing due to the error as mentioned in title.
A/c to the error it appears that the vitest is not able to find the function
formattedAvailabilityTime
, where as this function certainly exist on the importHere is the function that exists in
js/utils/time-and-date
Here are the functions which is using the above in
js/services/reservation-service
Scratching my head of to find why it can't find inside
sortWorkingHours
function used, where as the this function is passing tests on other locations in code where test independently.Moreover, it passes test when i remove
sortWorkingHours({ workingHours, type: 'opens_at', sort: 'asc' })
from the component or addformattedAvailabilityTime
inside service file :SComplete stack trace of the error:
Reproduction
System Info
Used Package Manager
pnpm
Validations