-
-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unit testing with vitest #1601
unit testing with vitest #1601
Comments
here is a related ticket for the cjs/esm loading vitest-dev/vitest#4233 |
hmm on the long run I want to keep ESM only but I think that I should wait another year or two before dropping CJS completely (not everyone uses ESM yet) and that would require to switch from Jest to Vitest for the huge amount of tests that I have which might be long. Anyway, you can maybe try to play with the package "main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
"exports": {
".": {
"types": "./dist/types/index.d.ts",
- "node": "./dist/cjs/index.js",
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
},
"./package.json": "./package.json"
}, if that doesn't work then maybe try to add
why would it do that? it's working fine in Jest and when using the App, so...? I also have |
so I managed to force vitest to load up the esm instead of cjs version with the following config: /// <reference types="vitest" />
import { defineConfig } from "vite";
import path from "path";
import angular from "@analogjs/vite-plugin-angular";
// HELPER TO BUILD PATHS TO ESM VERSION
function getAliasedPath(alias: string) {
return path.resolve(
__dirname,
`../node_modules/@slickgrid-universal/${alias}/dist/esm/index.js`
);
}
export default defineConfig(({ mode }) => ({
plugins: [angular()],
test: {
globals: true,
setupFiles: ["./src/test-setup.ts"],
environment: "jsdom",
include: ["src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"],
reporters: ["default", "junit"],
coverage: {
provider: "v8",
reporter: ["cobertura", "text", "html"],
reportsDirectory: "./reports"
},
outputFile: {
junit: "./reports/junit.xml"
},
css: true,
// REMOUNT CJS TO ESM REQUESTS
alias: {
"@slickgrid-universal/common": getAliasedPath("common"),
"@slickgrid-universal/row-detail-view-plugin": getAliasedPath(
"row-detail-view-plugin"
),
"@slickgrid-universal/empty-warning-component": getAliasedPath(
"empty-warning-component"
),
"@slickgrid-universal/custom-footer-component": getAliasedPath(
"custom-footer-component"
),
"@slickgrid-universal/pagination-component": getAliasedPath(
"pagination-component"
),
"@slickgrid-universal/custom-tooltip-plugin": getAliasedPath(
"custom-tooltip-plugin"
),
"@slickgrid-universal/event-pub-sub": getAliasedPath("event-pub-sub"),
"@slickgrid-universal/excel-export": getAliasedPath("excel-export"),
"@slickgrid-universal/odata": getAliasedPath("odata")
},
server: {
deps: {
inline: ["angular-slickgrid"]
// THIS ONE IS ALSO NECESSARY TO GET THINGS GOING: https://vitest.dev/config/#server-deps-inline
}
}
},
define: {
"import.meta.vitest": mode !== "production"
}
})); The other issue that I mentioned was here https://github.com/ghiscoding/slickgrid-universal/blob/master/packages/common/src/core/slickGrid.ts#L2796 where rule.left and rule.right are undefined due to the special treatment of cssRules which isn't compatible with JSDOM. So adding an if clause there fixes my issue: if (rule.left) {
rule.left.style.left = `${x}px`;
}
if (rule.right) {
rule.right.style.right = (((this._options.frozenColumn !== -1 && i > this._options.frozenColumn!) ? this.canvasWidthR : this.canvasWidthL) - x - w) + 'px';
} |
I think the issue might be due to a special treatment of those by Vitest itself: Anyways, I don't wont to care about those anyways since ESM is the way to go and the alias remaps as above work well enough for me without having to change anything on the slickgrid-universal side. I guess documenting this in our test section should be good enough |
instead of the big change you've done in your Vitest config, you should still try the suggestion I proposed above. I have a good feeling that it will help since that |
I tried messing around with the Anyways I've added a PR for the JSDOM fix as well as Doc updates for the Angular Repository |
another thing that I was never sure if we are supposed to put the |
it would be really strange if the order of properties in a json obj matter as those anyways arent guaranteed. I that can be safely ignored |
- I don't think we need the `exports.node` since it's not a package meant to be used in NodeJS but rather in a browser, so let's remove it - also move `module` to be after the `exports` since order might matter (not entirely sure but that is what `rimraf` does and it's a package made a NodeJS member, so it should be a good indication) - related to issue #1601
even if you're surprised, I'm pretty sure that it's still a fact (at least for the I didn't know it was possible to use Vitest in Angular, it's cool to know, I've never tried AnalogJS |
- I don't think we need the `exports.node` since it's not a package meant to be used in NodeJS but rather in a browser, so let's remove it - also move `module` to be after the `exports` since order might matter (not entirely sure but that is what `rimraf` does and it's a package made a NodeJS member, so it should be a good indication) - related to issue #1601
js world never fails to surprise 🤪 up until recently you needed the full analogjs, but with a recent update now one can pick only the parts necessary for configuring vitest along with an exposed vite config. |
@zewa666 I've pushed a new release and that not only include your changes but also the changes I've done to cleanup the One last and very important note, the latest version of Angular 18.1.x is giving problem caused by an indirect dependency
I only have that problem in my Angular port, Aurelia & React are unaffected and again it's only after updating to 18.1.x. I also searched in the Angular GitHub project but couldn't find anything recent. I'm wondering if you're also having this problem? ... but anyway at the end of the day, my projects are not the cause of it and personally I don't like project that went for ESM only when an hybrid approach is a lot better and what I follow (support both CJS and prefer ESM) temp patcha temp patch to keep updating to latest Angular versions is to use something like Yarn EDIT as per Yarn issue this might a Yarn legacy issue only, so anyway I applied the patch in Angular-Slickgrid PR |
havent yet updated to the latest v18 version. but I wonder whether it will affect vite and vitest projects as there shouldnt be any cjs involved. thanks for the heads up and for the new release |
Describe the bug
hey there @ghiscoding
so I'm trying to get my unit tests up and running but noticed a few gotchas. one of them is that vitest loads slickgrid-common in combo with angular-slickgrid as cjs instead of esm. I'll try to understand where that comes from and fix it.
but while at it I noticed that the Sortablejs import now is wrong as the library tries to check the sortablejs.default instead of sortablejs variable. I guess this is due to non-named imports happening in slickgrid.ts
furthermore there is at least one more new place where jsdom causes issues, but I got a workaround for that which I'll add next week.
I'll also create a sample repo as this might involve a couple of more cases
Reproduction
create unit tests for a grid using vite+vitest
Which Framework are you using?
Angular
Environment Info
Validations
The text was updated successfully, but these errors were encountered: