Skip to content

Commit

Permalink
Merge pull request #1439 from zewa666/patch-1
Browse files Browse the repository at this point in the history
docs: testing hints for vitest
  • Loading branch information
ghiscoding committed Jul 12, 2024
2 parents 076d8c2 + 5d92fef commit f788518
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions docs/testing/testing-patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,64 @@ describe('Example 3 - Grid with Editors', () => {
});
});
```

#### Vitest CJS instead of ESM loading
You may experience issues when using Vite + Vitest (e.g via AnalogJS) where Vitest would load the cjs version instead of esm of slickgrid-universal. The reason for this is that nested sub-depdendencies aren't properly analyzed and left up to node's loading mechanism. (see https://github.com/vitest-dev/vitest/discussions/4233 for more details).

To workaround that limitation you can remap (alias) the cjs calls to esm with the following configuration in your vite.config.mts

```ts
/// <reference types="vitest" />
import { defineConfig } from "vite";
import path from "path";

import angular from "@analogjs/vite-plugin-angular";

// helper to get the aliased path
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}"],
css: true,
alias: { // <--- here come the remounts
"@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: { // <--- this ones is needed as well to let vitest process the bundling
deps: {
inline: ["angular-slickgrid"]
}
}
},
define: {
"import.meta.vitest": mode !== "production"
}
}));
```

0 comments on commit f788518

Please sign in to comment.