Skip to content

Instantly share code, notes, and snippets.

@thanhtunguet
Created August 6, 2021 10:26
Show Gist options
  • Save thanhtunguet/91512e80d806cf15d001bf0db4d89938 to your computer and use it in GitHub Desktop.
Save thanhtunguet/91512e80d806cf15d001bf0db4d89938 to your computer and use it in GitHub Desktop.
Generate SCSS mocks for Jest
const fs = require('fs');
const {join} = require('path');
function readDirAll(path, files = []) {
if (fs.lstatSync(path).isDirectory()) {
fs.readdirSync(path).forEach((entry) => {
if (entry !== '.' && entry !== '..') {
readDirAll(join(path, entry), files);
}
});
return files;
}
if (path.match(/\.scss/)) {
files.push(path);
}
return files;
}
const files = [];
readDirAll('src', files);
let content = '';
files.forEach((element) => {
content += `
jest.mock(
'${element}',
() => ({}),
);`;
});
fs.writeFileSync('__mocks__/styles.mock.ts', content);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment