Skip to content

Commit

Permalink
✨ Add support for .cjs config files and snapshot files (#1011)
Browse files Browse the repository at this point in the history
* ✨ Add cjs extensions to cosmiconfig options

* ✨ Add cjs extensions to supported snapshot files
  • Loading branch information
wwilsman authored Jul 28, 2022
1 parent b82b4c2 commit 6bb4944
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/cli-snapshot/src/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ function merge(...objs) {
async function loadSnapshotFile(file) {
let ext = path.extname(file);

if (ext === '.js') {
if (/\.(c|m)?js$/.test(ext)) {
let { default: module } = await import(path.resolve(file));
return typeof module === 'function' ? await module() : module;
} else if (ext === '.json') {
return JSON.parse(fs.readFileSync(file, 'utf-8'));
} else if (ext.match(/\.ya?ml$/)) {
} else if (/\.ya?ml$/.test(ext)) {
let { default: YAML } = await import('yaml');
return YAML.parse(fs.readFileSync(file, 'utf-8'));
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli-snapshot/test/file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('percy snapshot <file>', () => {
]
}], { depth: null }),

'pages-fn.js': 'module.exports = () => (' + inspect([{
'pages-fn.cjs': 'module.exports = () => (' + inspect([{
name: 'JS Function Snapshot',
url: 'http://localhost:8000'
}], { depth: null }) + ')',
Expand Down Expand Up @@ -126,7 +126,7 @@ describe('percy snapshot <file>', () => {
});

it('snapshots pages from .js files that export a function', async () => {
await snapshot(['./pages-fn.js']);
await snapshot(['./pages-fn.cjs']); // .(c|m)?js

expect(logger.stderr).toEqual([]);
expect(logger.stdout).toEqual([
Expand Down
4 changes: 3 additions & 1 deletion packages/config/src/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ export const explorer = cosmiconfigSync('percy', {
'.percy.yaml',
'.percy.yml',
'.percy.js',
'percy.config.js'
'.percy.cjs',
'percy.config.js',
'percy.config.cjs'
]
});

Expand Down
5 changes: 4 additions & 1 deletion packages/config/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const INTERNAL_FILE_REG = new RegExp(
'(src|dist|test|package\\.json)(\\1|$)'
);

// Used to mock javascript modules
const JS_FILE_REG = /\.(c|m)?js$/;

// Mock and spy on fs methods using an in-memory filesystem
export async function mockfs({
// set `true` to allow mocking files within `node_modules` (may cause dynamic import issues)
Expand All @@ -45,7 +48,7 @@ export async function mockfs({

// when .js files are created, also mock the module for importing
spyOn(vol, 'writeFileSync').and.callFake((...args) => {
if (args[0].endsWith('.js')) mockFileModule(...args);
if (JS_FILE_REG.test(args[0])) mockFileModule(...args);
return vol.writeFileSync.and.originalFn.apply(vol, args);
});

Expand Down

0 comments on commit 6bb4944

Please sign in to comment.