Skip to content

Commit

Permalink
feat: automatically whitelist Marko dependencies in transforms
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Preset now contains more options.
BREAKING CHANGE: Preset contains babel-jest by default (same as vanilla jest).
  • Loading branch information
DylanPiercey committed Aug 9, 2019
1 parent 1443671 commit 5cd263e
Show file tree
Hide file tree
Showing 4 changed files with 2,458 additions and 978 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,14 @@ In the above example config, any tests with `*.browser.js` will run in a JSDOM c

By default Jest will not transform any `.marko` files within your `node_modules` folder. Marko recommends publishing the original source Marko files when publishing to npm. To get around this you can use the [`transformIgnorePatterns`](https://jestjs.io/docs/en/tutorial-react-native#transformignorepatterns-customization) option in Jest and whitelist any packages which contain Marko tags.

The `@marko/jest` preset by will scan your `node_modules` and build the appropriate ignore pattern for you. If you are just using the `@marko/jest` transformer standalone then you will have to do this yourself, like so:

**jest.config.js**

```javascript
module.exports = {
browser: true,
preset: "@marko/jest",
transformIgnorePatterns: [
// `marko` is excluded automatically by the preset.
// Here we are also adding `@marko-tags` to allow using https://github.com/marko-js/tags
"node_modules/(?!(marko|@marko-tags)/)"
]
...,
transformIgnorePatterns: ["node_modules/(?!(marko|@marko-tags|another-package-with-marko-tags)/)"]
};
```

Expand Down
27 changes: 24 additions & 3 deletions jest-preset.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
const fs = require("fs");
const path = require("path");
const { defaults } = require("jest-config");
const escapeRegexp = require("escape-string-regexp");
const { package, path: packagePath } = require("read-pkg-up").sync({
normalize: false
});
const nodeModulesPath = path.join(packagePath, "../node_modules");

module.exports = {
// uses a webpack style resolver, the default one has many issues.
Expand All @@ -7,8 +14,22 @@ module.exports = {
moduleFileExtensions: defaults.moduleFileExtensions.concat("marko"),
// preprocesses Marko files.
transform: {
"\\.marko$": require.resolve(".")
"\\.marko$": require.resolve("."),
"\\.[tj]s$": "babel-jest"
},
// transforms top level `.marko` files in the Marko package.
transformIgnorePatterns: ["node_modules/(?!(marko)/)"]
// Jest ignores node_module transforms by default.
// Here we whitelist all node_modules that contain a `marko.json`.
// Only checks for node_modules listed in package.json, same as Marko taglib scanning.
transformIgnorePatterns: [
`node_modules/(?!(${Object.keys(package.dependencies || {})
.concat(Object.keys(package.devDependencies || {}))
.concat(Object.keys(package.peerDependencies || {}))
.filter((name, i, all) => all.indexOf(name) === i)
.filter(name =>
fs.existsSync(path.join(nodeModulesPath, name, "marko.json"))
)
.map(escapeRegexp)
.concat("marko")
.join("|")})/)`
]
};
Loading

0 comments on commit 5cd263e

Please sign in to comment.