Failing tests in non-TS app due to undefined vars #419
-
I’ve run into something like a brick wall while introducing I am building a new component library in TS, with Vanilla-extract. It is meant to be consumed in a non-TS application, and based on advice from Matt I’ve opted to just export the result of building the library with On the library side, I have the following setup: A // src/themes/vars
import { createThemeContract } from '@vanilla-extract/css';
const themeTokens = {
colors: {
links: {
default: null,
focus: null,
},
// some other stuff here
},
} as const;
export const vars = createThemeContract(themeTokens); One thing to note is that we also export/expose the above: // srs/index.ts
export { vars } from './themes/vars.css'; It’s then used to create a couple ot themes (there’s about 8 of them at the moment…): // src/themes/theme1/theme.css.ts
import { createGlobalTheme } from '@vanilla-extract/css';
import { vars } from '../vars.css';
import '../globalStyles.css'; // some reset stuff using globalStyle()
const themeTokens = {
colors: {
links: {
default: '#2b75bb',
focus: '#2668a6',
},
},
// etc.
};
export default createGlobalTheme(':root', vars, themeTokens); The themes are consumed by a // src/components/themeprovider/ThemeProvider.tsx
import { ReactNode, useEffect } from 'react';
export type ThemeProviderTypes = {
brand: string;
children: ReactNode;
};
export const ThemeProvider = ({ brand, children }: ThemeProviderTypes) => {
useEffect(() => {
const loadTheme = async (brand: string) => {
try {
await import(/* webpackChunkName: "[request]" */ `../../themes/${brand}/theme.css`);
} catch () {
console.warn(`The \`ThemeProvider\` failed to properly fetch the global CSS file for \`${brand}\`. Falling back to the default theme.`);
await import(/* webpackChunkName: "default-theme-css" */ `../../themes/default/theme.css`);
}
};
loadTheme(brand);
}, [brand]);
return children;
}; The // some/file/in/consumer/app.jsx
import { vars } from '@company/componentLibrary';
const Component = styled('div')`
color: ${vars.colors.links.default};
`; Now to the issue: The above works just fine in the browser – then correct theme file is fetched, and using
I am not sure how to address this: One thought that went through my head was that ”of course Some pointers would be deeply appreciated, feel free to suggest a different take on the whole setup if that is what is wrong as well – this is the final piece needed to introduce |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Hey bud. It's very hard to give much advice here without a reproduction of the issue. If you can reproduce it then we can take a look. However, I'd say your setup looks good to me. The only advice I can give is, from a jest perspective vanilla is really just normal code. There's no magic going on. The babel plugin does a very small transform but if that was the issue you wouldn't get an error like you're getting. I'd be debugging your tests and seeing what is going on inside your library when you run a failing test. Running jest through the node debugger in vscode is quite easy now, that's my go-to for this kinda stuff. |
Beta Was this translation helpful? Give feedback.
Hey bud. It's very hard to give much advice here without a reproduction of the issue. If you can reproduce it then we can take a look. However, I'd say your setup looks good to me.
The only advice I can give is, from a jest perspective vanilla is really just normal code. There's no magic going on. The babel plugin does a very small transform but if that was the issue you wouldn't get an error like you're getting. I'd be debugging your tests and seeing what is going on inside your library when you run a failing test. Running jest through the node debugger in vscode is quite easy now, that's my go-to for this kinda stuff.