diff --git a/templates/test/helpers/getErrors.js b/templates/test/helpers/getErrors.js new file mode 100644 index 0000000..716fbbb --- /dev/null +++ b/templates/test/helpers/getErrors.js @@ -0,0 +1,5 @@ +import normalizeErrors from './normalizeErrors'; + +export default (stats) => { + return normalizeErrors(stats.compilation.errors); +}; diff --git a/templates/test/helpers/getWarnings.js b/templates/test/helpers/getWarnings.js new file mode 100644 index 0000000..c8a09d6 --- /dev/null +++ b/templates/test/helpers/getWarnings.js @@ -0,0 +1,5 @@ +import normalizeErrors from './normalizeErrors'; + +export default (stats) => { + return normalizeErrors(stats.compilation.warnings); +}; diff --git a/templates/test/helpers/index.js b/templates/test/helpers/index.js index 048ed5a..b8df497 100644 --- a/templates/test/helpers/index.js +++ b/templates/test/helpers/index.js @@ -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, +}; diff --git a/templates/test/helpers/readAsset.js b/templates/test/helpers/readAsset.js index 1a36027..8f4699f 100644 --- a/templates/test/helpers/readAsset.js +++ b/templates/test/helpers/readAsset.js @@ -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(); } diff --git a/templates/test/helpers/readAssets.js b/templates/test/helpers/readAssets.js new file mode 100644 index 0000000..a2fb783 --- /dev/null +++ b/templates/test/helpers/readAssets.js @@ -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; +} diff --git a/templates/test/loader.test.js b/templates/test/loader.test.js index 2ea5ef6..ccdcbc9 100644 --- a/templates/test/loader.test.js +++ b/templates/test/loader.test.js @@ -2,7 +2,8 @@ import { compile, execute, getCompiler, - normalizeErrors, + getErrors, + getWarnings, readAsset, } from './helpers'; @@ -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'); }); }); diff --git a/templates/test/name-option.test.js b/templates/test/name-option.test.js index 1aab10a..7497dfe 100644 --- a/templates/test/name-option.test.js +++ b/templates/test/name-option.test.js @@ -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 () => { @@ -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'); }); }); diff --git a/templates/test/validate-options.test.js b/templates/test/validate-options.test.js index ffa77e1..0642625 100644 --- a/templates/test/validate-options.test.js +++ b/templates/test/validate-options.test.js @@ -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(); + // } + // } }); }