Skip to content

Commit

Permalink
feat: added node loader for styles for mocha
Browse files Browse the repository at this point in the history
  • Loading branch information
jpina1-godaddy committed Sep 25, 2024
1 parent 94c5724 commit 1a64f67
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { register } from 'module';
import { pathToFileURL } from 'url';

// Register the Babel loader
register('@gasket/plugin-mocha/node-loader-babel', pathToFileURL('./test'));

// Register the styles loader
register('@gasket/plugin-mocha/node-loader-styles', pathToFileURL('./test'));
10 changes: 10 additions & 0 deletions packages/gasket-plugin-mocha/lib/node-loader-styles/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function resolve(specifier: string, context: any, defaultResolve: any): {
url: string;
shortCircuit: boolean;
} | ReturnType<typeof defaultResolve>;

export function load(url: string, context: any, defaultLoad: any): {
format: string;
source: string;
shortCircuit: boolean;
} | ReturnType<typeof defaultLoad>;
34 changes: 34 additions & 0 deletions packages/gasket-plugin-mocha/lib/node-loader-styles/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const path = require('path');
const { pathToFileURL } = require('url');

// Extended regex for matching common CSS-related file extensions
const styleFileRegex = /\.(css|scss|sass|less|styl)$/;

function resolve(specifier, context, defaultResolve) {
// Use the regex to check if the file is a CSS or related file
if (styleFileRegex.test(specifier)) {
const filePath = path.resolve(__dirname, 'empty-module.js');
return {
url: pathToFileURL(filePath).href,
shortCircuit: true
};
}
return defaultResolve(specifier, context, defaultResolve);
}

function load(url, context, defaultLoad) {
// Handle loading for the 'empty-module.js' placeholder
if (url.endsWith('empty-module.js')) {
return {
format: 'module',
source: 'export default {};',
shortCircuit: true
};
}
return defaultLoad(url, context, defaultLoad);
}

module.exports = {
resolve,
load
};
4 changes: 4 additions & 0 deletions packages/gasket-plugin-mocha/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"types": "./lib/node-loader-babel/index.d.ts",
"default": "./lib/node-loader-babel/index.js"
},
"./node-loader-styles": {
"types": "./lib/node-loader-styles/index.d.ts",
"default": "./lib/node-loader-styles/index.js"
},
"./package.json": "./package.json"
},
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { resolve, load } = require('../../lib/node-loader-styles/index.js');
const path = require('path');

describe('resolve', () => {
it('should return a URL for CSS-related files', () => {
const result = resolve('./test.css', {}, () => {});
expect(result).toHaveProperty('url');
expect(result).toHaveProperty('shortCircuit', true);
});

it('should call defaultResolve for non-CSS-related files', () => {
const defaultResolve = jest.fn();
resolve('./test.js', {}, defaultResolve);
expect(defaultResolve).toHaveBeenCalled();
});
});

describe('load', () => {
it('should return a module format for the empty-module.js file', () => {
const result = load(path.resolve(__dirname, 'empty-module.js'), {}, () => {});
expect(result).toHaveProperty('format', 'module');
expect(result).toHaveProperty('source', 'export default {};');
expect(result).toHaveProperty('shortCircuit', true);
});

it('should call defaultLoad for non-empty-module.js files', () => {
const defaultLoad = jest.fn();
load('./test.js', {}, defaultLoad);
expect(defaultLoad).toHaveBeenCalled();
});
});

0 comments on commit 1a64f67

Please sign in to comment.