Skip to content

Commit

Permalink
feat(node): automagically mock with source content
Browse files Browse the repository at this point in the history
Example [
  [**/*.{scss,less,css,html]
]
with an undefined `value` will automatically be mocked
with the files content as string if it exists
  • Loading branch information
Christoffer Åström committed Jun 25, 2018
1 parent 6862a08 commit 25e3380
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 9 deletions.
3 changes: 2 additions & 1 deletion aw.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ module.exports = {
watchGlob: [...src, ...glob],
mocks: [
['**/cdp/src/browser-shim.js', '{}'],
['*.{scss,less,css}', '{}'],
['**/*.{scss,less,css,html}'],
['./foobar-virtual.html', '"<div>hello world</div>"'],
],
nyc: {
include: src,
Expand Down
14 changes: 7 additions & 7 deletions commands-common/babel/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ let removeCompileHook = () => { };
let removeLoadHook = () => { };

function compileHook(argv, code, filename, virtualMock = false) {
// Ignore transpiling after-work.js except react
// if (/after-work.js\/*!(react)/.test(filename)) {
// return code;
// }
const sourceRoot = path.dirname(filename);
const { babel, options } = argv.babel;
const opts = new babel.OptionManager().init({
Expand Down Expand Up @@ -61,9 +57,13 @@ function hookedLoader(options, request, parent, isMain) {
filename = request;
}

for (const item of options.mocks || []) { // eslint-disable-line
const [key, value] = item;
for (const item of options.mocks ||  []) { // eslint-disable-line
let [key, value] = item; //eslint-disable-line
if (minimatch(filename, key)) {
if (value === undefined && fs.existsSync(filename)) {
const src = fs.readFileSync(filename, 'utf8');
value = `${JSON.stringify(src)}`;
}
return compile(value, filename, options);
}
}
Expand Down Expand Up @@ -104,7 +104,7 @@ module.exports = function register(options = {}) {
installSourceMapSupport();
removeCompileHook();
removeLoadHook();
const exts = [...new Set(options.extensions || []), '.js', '.ts', '.jsx', '.scss', '.less', '.css'];
const exts = [...new Set(options.extensions || []), '.js', '.ts', '.jsx', '.scss', '.less', '.css', '.html'];
removeCompileHook = addHook(compileHook.bind(null, options), { exts });
removeLoadHook = addLoadHook(options);
};
12 changes: 12 additions & 0 deletions examples/node/test/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf

# space indentation
[*.*]
indent_style = space
indent_size = 2

# ignore final new line for html
[*.html]
insert_final_newline = false
5 changes: 5 additions & 0 deletions examples/node/test/__snapshots__/html.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`'should import template' 1`] = `"<div>foobar</div>"`;

exports[`'should import virtual template' 1`] = `"<div>hello world</div>"`;
1 change: 1 addition & 0 deletions examples/node/test/foobar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>foobar</div>
12 changes: 12 additions & 0 deletions examples/node/test/html.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import virtual from './foobar-virtual.html'; // eslint-disable-line
import template from './foobar.html';

describe('html', () => {
it('should import virtual template', () => {
expect(virtual).toMatchSnapshot();
});

it('should import template', () => {
expect(template).toMatchSnapshot();
});
});
1 change: 0 additions & 1 deletion examples/react/test/button.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import renderer from 'react-test-renderer';
import 'foo.scss'; // eslint-disable-line import/no-unresolved
import 'bar.less'; // eslint-disable-line import/no-unresolved
import 'baz.css'; // eslint-disable-line import/no-unresolved

import Button from '../src/button';

describe('button', () => {
Expand Down

0 comments on commit 25e3380

Please sign in to comment.