Skip to content

Commit

Permalink
feat: improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Nov 22, 2019
1 parent a4db4c2 commit 74c41c5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion templates/test/fixtures/foo.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log('foo');
export default 'foo';
2 changes: 2 additions & 0 deletions templates/test/fixtures/simple.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import foo from './foo';

__export__ = foo;

export default foo;
22 changes: 22 additions & 0 deletions templates/test/helpers/execute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Module from 'module';
import path from 'path';

const parentModule = module;

export default (code) => {
const resource = 'test.js';
const module = new Module(resource, parentModule);
// eslint-disable-next-line no-underscore-dangle
module.paths = Module._nodeModulePaths(
path.resolve(__dirname, '../fixtures')
);
module.filename = resource;

// eslint-disable-next-line no-underscore-dangle
module._compile(
`let __export__;${code};module.exports = __export__;`,
resource
);

return module.exports;
};
6 changes: 2 additions & 4 deletions templates/test/helpers/getCompiler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';

import webpack from 'webpack';
import memfs from 'memfs';
import { createFsFromVolume, Volume } from 'memfs';

export default (fixture, loaderOptions = {}, config = {}) => {
const fullConfig = {
Expand Down Expand Up @@ -34,9 +34,7 @@ export default (fixture, loaderOptions = {}, config = {}) => {
const compiler = webpack(fullConfig);

if (!config.outputFileSystem) {
const outputFileSystem = memfs;

outputFileSystem.vol.reset();
const outputFileSystem = createFsFromVolume(new Volume());
// Todo remove when we drop webpack@4 support
outputFileSystem.join = path.join.bind(path);

Expand Down
14 changes: 10 additions & 4 deletions templates/test/loader.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { getCompiler, compile, normalizeErrors, readAsset } from './helpers';
import {
compile,
execute,
getCompiler,
normalizeErrors,
readAsset,
} from './helpers';

describe('loader', () => {
it('should work', async () => {
const compiler = getCompiler('simple.js');
const stats = await compile(compiler);

expect(readAsset('main.bundle.js', compiler, stats)).toMatchSnapshot(
'result'
);
expect(
execute(readAsset('main.bundle.js', compiler, stats))
).toMatchSnapshot('result');
expect(normalizeErrors(stats.compilation.warnings)).toMatchSnapshot(
'warnings'
);
Expand Down

0 comments on commit 74c41c5

Please sign in to comment.