Skip to content

Commit

Permalink
support completely ignoring node modules
Browse files Browse the repository at this point in the history
  • Loading branch information
gtsop-d committed Aug 7, 2024
1 parent 07e0f0a commit f5d0f4c
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ code.js -> imports ./lib/index.js -> imports libFunc.js
This is intended to help you defer refactoring some barels or modules that are causing trouble or breaking your tests when you integrate this plugin.
## `ignoreNodeModules` **[boolean]**
Set this flag to true if you want to completely ignore all node\_modules from being traversed. Default is false.
# Plugin directives
## `no-boost`
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
transform: {},
setupFiles: ["<rootDir>/jest.setup.js"],
setupFilesAfterEnv: ["jest-expect-message"],
};
1 change: 1 addition & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.env.BJB_ENV = "test";
26 changes: 19 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"license": "AGPL-3.0-or-later",
"devDependencies": {
"@eslint/js": "^9.2.0",
"empty-npm-package": "^1.0.0",
"eslint": "^9.2.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jest": "^28.5.0",
Expand Down
3 changes: 3 additions & 0 deletions package/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ function cache(func, ...params) {
}

function withCache(func) {
if (process.env.BJB_ENV === "test") {
return func;
}
const map = getCache(func.name);
return function funcWithCache(...params) {
// const cacheKey = params.join('-');
Expand Down
12 changes: 10 additions & 2 deletions package/plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {

let modulePaths = null;
let moduleNameMapper = null;

let ignoreNodeModules = false;
let importWhiteList = [];

const isPathWhitelisted = withCache(function actualIsPathWhitelisted(path) {
Expand All @@ -21,7 +21,14 @@ const bjbResolve = withCache(function resolveWithWhitelist(path, basedir) {
if (isPathWhitelisted(path)) {
return path;
}
return resolve(path, basedir, moduleNameMapper, modulePaths);

const result = resolve(path, basedir, moduleNameMapper, modulePaths);

if (ignoreNodeModules && result.includes("/node_modules/")) {
return path;
}

return result;
} catch (e) {
console.log("failed to resolve", e.message);
return null;
Expand All @@ -44,6 +51,7 @@ module.exports = function babelPlugin(babel) {
moduleNameMapper = state.opts.jestConfig?.moduleNameMapper || {};
modulePaths = state.opts.jestConfig?.modulePaths || [];
importWhiteList = state.opts.importIgnorePatterns || [];
ignoreNodeModules = state.opts.ignoreNodeModules || false;

const comments = path.parent.comments || [];
const skipProgramComment = comments.find((comment) =>
Expand Down
18 changes: 18 additions & 0 deletions spec/import-rewrites.spec.jsm → spec/import-rewrites.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,22 @@ describe("babel-jest-boost plugin import rewrites", () => {
`import { target } from "${__dirname}/test_tree/library/library.js";`,
);
});

it("resolves node_modules", () => {
const oneDirUp = __dirname.replace("/spec", "");
expectTransform(
"import { printMsg } from 'empty-npm-package';",
`import { printMsg } from "${oneDirUp}/node_modules/empty-npm-package/index.js";`,
);
});

it("does not resolve node_modules if configured", () => {
const noNodeModulesExpectTransform = createExpectTransform(__filename, {
ignoreNodeModules: true,
});
noNodeModulesExpectTransform(
"import { printMsg } from 'empty-npm-package';",
"import { printMsg } from 'empty-npm-package';",
);
});
});

0 comments on commit f5d0f4c

Please sign in to comment.