diff --git a/packages/gasket-plugin-mocha/generator/react-app/test/register-loader.js b/packages/gasket-plugin-mocha/generator/react-app/test/register-loader.js index 532faea22..b39ddb2fb 100644 --- a/packages/gasket-plugin-mocha/generator/react-app/test/register-loader.js +++ b/packages/gasket-plugin-mocha/generator/react-app/test/register-loader.js @@ -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')); diff --git a/packages/gasket-plugin-mocha/lib/node-loader-styles/index.d.ts b/packages/gasket-plugin-mocha/lib/node-loader-styles/index.d.ts new file mode 100644 index 000000000..762fdabdc --- /dev/null +++ b/packages/gasket-plugin-mocha/lib/node-loader-styles/index.d.ts @@ -0,0 +1,10 @@ +export function resolve(specifier: string, context: any, defaultResolve: any): { + url: string; + shortCircuit: boolean; +} | ReturnType; + +export function load(url: string, context: any, defaultLoad: any): { + format: string; + source: string; + shortCircuit: boolean; +} | ReturnType; diff --git a/packages/gasket-plugin-mocha/lib/node-loader-styles/index.js b/packages/gasket-plugin-mocha/lib/node-loader-styles/index.js new file mode 100644 index 000000000..69533ea7c --- /dev/null +++ b/packages/gasket-plugin-mocha/lib/node-loader-styles/index.js @@ -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 +}; diff --git a/packages/gasket-plugin-mocha/package.json b/packages/gasket-plugin-mocha/package.json index a073816d5..e372d7a0d 100644 --- a/packages/gasket-plugin-mocha/package.json +++ b/packages/gasket-plugin-mocha/package.json @@ -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": { diff --git a/packages/gasket-plugin-mocha/test/node-loader-styles/node-loader-styles.test.js b/packages/gasket-plugin-mocha/test/node-loader-styles/node-loader-styles.test.js new file mode 100644 index 000000000..996e3d4ac --- /dev/null +++ b/packages/gasket-plugin-mocha/test/node-loader-styles/node-loader-styles.test.js @@ -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(); + }); +});