Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for tests that import css #929

Merged
merged 3 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
Loading