Skip to content

Commit

Permalink
fix: avoid overriding jest resolver when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed May 8, 2023
1 parent 6593473 commit 3018c43
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ The above is roughly equivalent to:
const { defaults } = require("jest-config");

module.exports = {
// uses a webpack style resolver
// uses a webpack style resolver for older versions of Marko
resolver: "...",
// allows for stuff like file watching of `.marko` files
moduleFileExtensions: defaults.moduleFileExtensions.concat("marko"),
Expand Down Expand Up @@ -175,7 +175,7 @@ To have Marko include a `style.css` file you could add [jest-transform-css](http

## Why override the resolver (enhanced-resolve-jest)?

The default jest resolver does actually work fine with Marko when running server side tests, however in the browser they rely on [browser-resolve](https://github.com/shtylman/node-browser-resolve#readme). The browser resolver then relies on a version of [resolve](https://github.com/browserify/resolve) which is over three years old and has had many fixes since.
The default jest resolver does actually work fine with Marko when running server side tests, however in the browser they rely on [browser-resolve](https://github.com/shtylman/node-browser-resolve#readme). The browser resolver then relies on a version of [resolve](https://github.com/browserify/resolve) which is over three years old and has had many fixes since. Newer versions of Marko have been updated to avoid these issues, but `@marko/jest` will override the resolver if a version of Marko older than `5.25.12` is used.

On top of the issues from using this outdated module, there are a number of limitations. Below i've outlined some issues and limitations you might come across because of this dependency used by jest, one of which completely stops Marko's browser modules from being resolved correctly, hence the recommendation here.

Expand Down
21 changes: 20 additions & 1 deletion src/preset/browser/jest-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,26 @@ export = {
...common,
testEnvironment: "jsdom",
// uses a webpack style resolver, the default one has many issues.
resolver: require.resolve("../resolver"),
resolver:
(() => {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const parts = require("@marko/compiler/package.json").version.split(
"."
);
const major = parseInt(parts[0], 10);
const minor = parseInt(parts[1], 10);
const patch = parseInt(parts[2], 10);
// Checks if the version is less than 5.25.12
return major === 5
? minor === 25
? patch < 12
: minor < 25
: major < 5;
} catch {
return false;
}
})() && require.resolve("../resolver"),
// preprocesses Marko files.
transform: {
"\\.marko$": require.resolve("../../transform/browser"),
Expand Down

0 comments on commit 3018c43

Please sign in to comment.