Skip to content

Commit

Permalink
feat: more testing helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Dec 9, 2019
1 parent 9b0db8f commit fda4d2e
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 35 deletions.
5 changes: 5 additions & 0 deletions templates/test/helpers/getErrors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import normalizeErrors from './normalizeErrors';

export default (stats) => {
return normalizeErrors(stats.compilation.errors);
};
5 changes: 5 additions & 0 deletions templates/test/helpers/getWarnings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import normalizeErrors from './normalizeErrors';

export default (stats) => {
return normalizeErrors(stats.compilation.warnings);
};
15 changes: 14 additions & 1 deletion templates/test/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import compile from './compile';
import execute from './execute';
import getCompiler from './getCompiler';
import getErrors from './getErrors';
import getWarnings from './getWarnings';
import normalizeErrors from './normalizeErrors';
import readAsset from './readAsset';
import readsAssets from './readAssets';

export { compile, getCompiler, normalizeErrors, readAsset };
export {
compile,
execute,
getCompiler,
getErrors,
getWarnings,
normalizeErrors,
readAsset,
readsAssets,
};
10 changes: 9 additions & 1 deletion templates/test/helpers/readAsset.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ import path from 'path';
export default (asset, compiler, stats) => {
const usedFs = compiler.outputFileSystem;
const outputPath = stats.compilation.outputOptions.path;

let data = '';
let targetFile = asset;

const queryStringIdx = targetFile.indexOf('?');

if (queryStringIdx >= 0) {
targetFile = targetFile.substr(0, queryStringIdx);
}

try {
data = usedFs.readFileSync(path.join(outputPath, asset)).toString();
data = usedFs.readFileSync(path.join(outputPath, targetFile)).toString();
} catch (error) {
data = error.toString();
}
Expand Down
11 changes: 11 additions & 0 deletions templates/test/helpers/readAssets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import readAsset from './readAsset';

export default function readAssets(compiler, stats) {
const assets = {};

Object.keys(stats.compilation.assets).forEach((asset) => {
assets[asset] = readAsset(asset, compiler, stats);
});

return assets;
}
9 changes: 4 additions & 5 deletions templates/test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
compile,
execute,
getCompiler,
normalizeErrors,
getErrors,
getWarnings,
readAsset,
} from './helpers';

Expand All @@ -14,9 +15,7 @@ describe('loader', () => {
expect(
execute(readAsset('main.bundle.js', compiler, stats))
).toMatchSnapshot('result');
expect(normalizeErrors(stats.compilation.warnings)).toMatchSnapshot(
'warnings'
);
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot('errors');
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});
});
21 changes: 13 additions & 8 deletions templates/test/name-option.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { compile, getCompiler, normalizeErrors, readAsset } from './helpers';
import {
compile,
execute,
getCompiler,
getErrors,
getWarnings,
readAsset,
} from './helpers';

describe('"name" option', () => {
it('should work with "Boolean" value equal "true"', async () => {
Expand All @@ -7,12 +14,10 @@ describe('"name" option', () => {
});
const stats = await compile(compiler);

expect(readAsset('main.bundle.js', compiler, stats)).toMatchSnapshot(
'result'
);
expect(normalizeErrors(stats.compilation.warnings)).toMatchSnapshot(
'warnings'
);
expect(normalizeErrors(stats.compilation.errors)).toMatchSnapshot('errors');
expect(
execute(readAsset('main.bundle.js', compiler, stats))
).toMatchSnapshot('result');
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});
});
62 changes: 42 additions & 20 deletions templates/test/validate-options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,48 @@ describe('validate options', () => {
it(`should ${
type === 'success' ? 'successfully validate' : 'throw an error on'
} the "${key}" option with "${stringifyValue(value)}" value`, async () => {
const compiler = getCompiler('simple.js', { [key]: value });

let stats;

try {
stats = await compile(compiler);
} finally {
if (type === 'success') {
expect(stats.hasErrors()).toBe(false);
} else if (type === 'failure') {
const {
compilation: { errors },
} = stats;

expect(errors).toHaveLength(1);
expect(() => {
throw new Error(errors[0].error.message);
}).toThrowErrorMatchingSnapshot();
}
}
// For loaders
// const compiler = getCompiler('simple.js', { [key]: value });
//
// let stats;
//
// try {
// stats = await compile(compiler);
// } finally {
// if (type === 'success') {
// expect(stats.hasErrors()).toBe(false);
// } else if (type === 'failure') {
// const {
// compilation: { errors },
// } = stats;
//
// expect(errors).toHaveLength(1);
// expect(() => {
// throw new Error(errors[0].error.message);
// }).toThrowErrorMatchingSnapshot();
// }
// }
// For plugins
// let error;
//
// try {
// // eslint-disable-next-line no-new
// new Plugin({ [key]: value });
// } catch (errorFromPlugin) {
// if (errorFromPlugin.name !== 'ValidationError') {
// throw errorFromPlugin;
// }
//
// error = errorFromPlugin;
// } finally {
// if (type === 'success') {
// expect(error).toBeUndefined();
// } else if (type === 'failure') {
// expect(() => {
// throw error;
// }).toThrowErrorMatchingSnapshot();
// }
// }
});
}

Expand Down

0 comments on commit fda4d2e

Please sign in to comment.