-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added node loader for styles for mocha
- Loading branch information
1 parent
94c5724
commit 1a64f67
Showing
5 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
packages/gasket-plugin-mocha/generator/react-app/test/register-loader.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
packages/gasket-plugin-mocha/lib/node-loader-styles/index.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
packages/gasket-plugin-mocha/lib/node-loader-styles/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/gasket-plugin-mocha/test/node-loader-styles/node-loader-styles.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |