From e5fe4e8048aa91b3f95af11841c609169c926e52 Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Wed, 19 Dec 2018 21:05:53 +0300 Subject: [PATCH] refactor: tests (#302) --- src/index.js | 3 +- test/Errors.test.js | 21 -- test/__snapshots__/Errors.test.js.snap | 14 -- .../__snapshots__/context-option.test.js.snap | 3 + .../emitFile-option.test.js.snap | 3 + test/__snapshots__/errors.test.js.snap | 8 + test/__snapshots__/loader.test.js.snap | 2 +- test/__snapshots__/name-option.test.js.snap | 5 + .../outputPath-option.test.js.snap} | 32 +-- .../publicPath-option.test.js.snap} | 12 +- .../useRelativePath-option.test.js.snap} | 8 +- test/context-option.test.js | 20 ++ test/emitFile-option.test.js | 38 ++++ test/errors.test.js | 14 ++ test/loader.test.js | 4 +- test/name-option.test.js | 39 ++++ .../__snapshots__/context.test.js.snap | 3 - .../__snapshots__/emitFile.test.js.snap | 3 - test/options/__snapshots__/name.test.js.snap | 5 - test/options/context.test.js | 22 -- test/options/emitFile.test.js | 42 ---- test/options/name.test.js | 44 ---- test/options/outputPath.test.js | 194 ------------------ test/options/publicPath.test.js | 75 ------- test/options/useRelativePath.test.js | 80 -------- test/outputPath-option.test.js | 192 +++++++++++++++++ test/publicPath-option.test.js | 73 +++++++ test/useRelativePath-option.test.js | 78 +++++++ 28 files changed, 503 insertions(+), 534 deletions(-) delete mode 100644 test/Errors.test.js delete mode 100644 test/__snapshots__/Errors.test.js.snap create mode 100644 test/__snapshots__/context-option.test.js.snap create mode 100644 test/__snapshots__/emitFile-option.test.js.snap create mode 100644 test/__snapshots__/errors.test.js.snap create mode 100644 test/__snapshots__/name-option.test.js.snap rename test/{options/__snapshots__/outputPath.test.js.snap => __snapshots__/outputPath-option.test.js.snap} (61%) rename test/{options/__snapshots__/publicPath.test.js.snap => __snapshots__/publicPath-option.test.js.snap} (63%) rename test/{options/__snapshots__/useRelativePath.test.js.snap => __snapshots__/useRelativePath-option.test.js.snap} (63%) create mode 100644 test/context-option.test.js create mode 100644 test/emitFile-option.test.js create mode 100644 test/errors.test.js create mode 100644 test/name-option.test.js delete mode 100644 test/options/__snapshots__/context.test.js.snap delete mode 100644 test/options/__snapshots__/emitFile.test.js.snap delete mode 100644 test/options/__snapshots__/name.test.js.snap delete mode 100644 test/options/context.test.js delete mode 100644 test/options/emitFile.test.js delete mode 100644 test/options/name.test.js delete mode 100644 test/options/outputPath.test.js delete mode 100644 test/options/publicPath.test.js delete mode 100644 test/options/useRelativePath.test.js create mode 100644 test/outputPath-option.test.js create mode 100644 test/publicPath-option.test.js create mode 100644 test/useRelativePath-option.test.js diff --git a/src/index.js b/src/index.js index 2ffe8c0..532ef2a 100644 --- a/src/index.js +++ b/src/index.js @@ -75,8 +75,7 @@ export default function loader(content) { publicPath = JSON.stringify(publicPath); } - // eslint-disable-next-line no-undefined - if (options.emitFile === undefined || options.emitFile) { + if (typeof options.emitFile === 'undefined' || options.emitFile) { this.emitFile(outputPath, content); } diff --git a/test/Errors.test.js b/test/Errors.test.js deleted file mode 100644 index 18eb5ba..0000000 --- a/test/Errors.test.js +++ /dev/null @@ -1,21 +0,0 @@ -import loader from '../src'; - -describe('Errors', () => { - test('Loader Error', () => { - const err = () => loader.call({ emitFile: false }); - - expect(err).toThrow(); - expect(err).toThrowErrorMatchingSnapshot(); - }); - - test('Validation Error', () => { - const err = () => - loader.call({ - query: { useRelativePath: 1 }, - emitFile: true, - }); - - expect(err).toThrow(); - expect(err).toThrowErrorMatchingSnapshot(); - }); -}); diff --git a/test/__snapshots__/Errors.test.js.snap b/test/__snapshots__/Errors.test.js.snap deleted file mode 100644 index 37e4a6c..0000000 --- a/test/__snapshots__/Errors.test.js.snap +++ /dev/null @@ -1,14 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Errors Loader Error 1`] = ` -"File Loader - -emitFile is required from module system" -`; - -exports[`Errors Validation Error 1`] = ` -"File Loader Invalid Options - -options.useRelativePath should be boolean -" -`; diff --git a/test/__snapshots__/context-option.test.js.snap b/test/__snapshots__/context-option.test.js.snap new file mode 100644 index 0000000..87e4e0c --- /dev/null +++ b/test/__snapshots__/context-option.test.js.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`when applied with \`context\` option matches snapshot for \`{String}\` value 1`] = `"module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`; diff --git a/test/__snapshots__/emitFile-option.test.js.snap b/test/__snapshots__/emitFile-option.test.js.snap new file mode 100644 index 0000000..aa6a97d --- /dev/null +++ b/test/__snapshots__/emitFile-option.test.js.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`when applied with \`emitFile\` option matches snapshot for \`true\` (\`{Boolean}\`) (default) 1`] = `"64eababb117f90535085779cc0325003.svg"`; diff --git a/test/__snapshots__/errors.test.js.snap b/test/__snapshots__/errors.test.js.snap new file mode 100644 index 0000000..872f9e8 --- /dev/null +++ b/test/__snapshots__/errors.test.js.snap @@ -0,0 +1,8 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`errors validation errors 1`] = ` +"File Loader Invalid Options + +options.useRelativePath should be boolean +" +`; diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap index 43c518b..087efa1 100644 --- a/test/__snapshots__/loader.test.js.snap +++ b/test/__snapshots__/loader.test.js.snap @@ -1,3 +1,3 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Loader Defaults 1`] = `"module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`; +exports[`loader should works without options 1`] = `"module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`; diff --git a/test/__snapshots__/name-option.test.js.snap b/test/__snapshots__/name-option.test.js.snap new file mode 100644 index 0000000..10e41a4 --- /dev/null +++ b/test/__snapshots__/name-option.test.js.snap @@ -0,0 +1,5 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`when applied with \`name\` option matches snapshot for \`{Function}\` value 1`] = `"module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`; + +exports[`when applied with \`name\` option matches snapshot for \`{String}\` value 1`] = `"module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`; diff --git a/test/options/__snapshots__/outputPath.test.js.snap b/test/__snapshots__/outputPath-option.test.js.snap similarity index 61% rename from test/options/__snapshots__/outputPath.test.js.snap rename to test/__snapshots__/outputPath-option.test.js.snap index d8d2d41..4328ebe 100644 --- a/test/options/__snapshots__/outputPath.test.js.snap +++ b/test/__snapshots__/outputPath-option.test.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Options outputPath {Function} 1`] = ` +exports[`when applied with \`outputPath\` option matches snapshot for \`{Function}\` value 1`] = ` Object { "assets": Array [ "output_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png", @@ -9,7 +9,7 @@ Object { } `; -exports[`Options outputPath {Function} with \`options.name\` 1`] = ` +exports[`when applied with \`outputPath\` option matches snapshot for \`{Function}\` value with \`options.name\` 1`] = ` Object { "assets": Array [ "output_path_func/file.png", @@ -18,7 +18,7 @@ Object { } `; -exports[`Options outputPath {Function} with \`publicPath\` {Function} 1`] = ` +exports[`when applied with \`outputPath\` option matches snapshot for \`{Function}\` value with \`publicPath\` (\`{Function}\`) 1`] = ` Object { "assets": Array [ "output_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png", @@ -27,7 +27,7 @@ Object { } `; -exports[`Options outputPath {Function} with \`publicPath\` {String} 1`] = ` +exports[`when applied with \`outputPath\` option matches snapshot for \`{Function}\` value with \`publicPath\` (\`{String}\`) 1`] = ` Object { "assets": Array [ "output_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png", @@ -36,7 +36,7 @@ Object { } `; -exports[`Options outputPath {String} 1`] = ` +exports[`when applied with \`outputPath\` option matches snapshot for \`{String}\` value 1`] = ` Object { "assets": Array [ "output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png", @@ -45,25 +45,25 @@ Object { } `; -exports[`Options outputPath {String} with \`options.name\` 1`] = ` +exports[`when applied with \`outputPath\` option matches snapshot for \`{String}\` value with \`publicPath\` (\`{Function}\`) 1`] = ` Object { "assets": Array [ - "output_path/file.png", + "output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png", ], - "source": "module.exports = __webpack_public_path__ + \\"output_path/file.png\\";", + "source": "module.exports = \\"public_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", } `; -exports[`Options outputPath {String} with \`publicPath\` {Function} 1`] = ` +exports[`when applied with \`outputPath\` option matches snapshot for \`{String}\` value with \`publicPath\` (\`{String}\`) 1`] = ` Object { "assets": Array [ "output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png", ], - "source": "module.exports = \\"public_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", + "source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", } `; -exports[`Options outputPath {String} with \`publicPath\` {String} 1`] = ` +exports[`when applied with \`outputPath\` option matches snapshot for \`{String}\` value with \`publicPath\` (\`{String}\`) without trailing slash 1`] = ` Object { "assets": Array [ "output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png", @@ -72,20 +72,20 @@ Object { } `; -exports[`Options outputPath {String} with \`publicPath\` {String} without trailing slash 1`] = ` +exports[`when applied with \`outputPath\` option matches snapshot for \`{String}\` value without trailing slash 1`] = ` Object { "assets": Array [ "output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png", ], - "source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", + "source": "module.exports = __webpack_public_path__ + \\"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", } `; -exports[`Options outputPath {String} without trailing slash 1`] = ` +exports[`when applied with \`outputPath\` option matches snapshot for \`{String}\` with \`name\` option 1`] = ` Object { "assets": Array [ - "output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png", + "output_path/file.png", ], - "source": "module.exports = __webpack_public_path__ + \\"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", + "source": "module.exports = __webpack_public_path__ + \\"output_path/file.png\\";", } `; diff --git a/test/options/__snapshots__/publicPath.test.js.snap b/test/__snapshots__/publicPath-option.test.js.snap similarity index 63% rename from test/options/__snapshots__/publicPath.test.js.snap rename to test/__snapshots__/publicPath-option.test.js.snap index 72c97dd..52cc6d8 100644 --- a/test/options/__snapshots__/publicPath.test.js.snap +++ b/test/__snapshots__/publicPath-option.test.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Options publicPath {Function} 1`] = ` +exports[`when applied with \`publicPath\` option matches snapshot for \`{Function}\` value 1`] = ` Object { "assets": Array [ "9c87cbf3ba33126ffd25ae7f2f6bbafb.png", @@ -9,25 +9,25 @@ Object { } `; -exports[`Options publicPath {String} - URL 1`] = ` +exports[`when applied with \`publicPath\` option matches snapshot for \`{String}\` value 1`] = ` Object { "assets": Array [ "9c87cbf3ba33126ffd25ae7f2f6bbafb.png", ], - "source": "module.exports = \\"https://cdn.com/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", + "source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", } `; -exports[`Options publicPath {String} - Without trailing slash 1`] = ` +exports[`when applied with \`publicPath\` option matches snapshot for \`{String}\` value as URL 1`] = ` Object { "assets": Array [ "9c87cbf3ba33126ffd25ae7f2f6bbafb.png", ], - "source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", + "source": "module.exports = \\"https://cdn.com/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", } `; -exports[`Options publicPath {String} 1`] = ` +exports[`when applied with \`publicPath\` option matches snapshot for \`{String}\` value without trailing slash 1`] = ` Object { "assets": Array [ "9c87cbf3ba33126ffd25ae7f2f6bbafb.png", diff --git a/test/options/__snapshots__/useRelativePath.test.js.snap b/test/__snapshots__/useRelativePath-option.test.js.snap similarity index 63% rename from test/options/__snapshots__/useRelativePath.test.js.snap rename to test/__snapshots__/useRelativePath-option.test.js.snap index 698b5f9..d9bd4cf 100644 --- a/test/options/__snapshots__/useRelativePath.test.js.snap +++ b/test/__snapshots__/useRelativePath-option.test.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Options useRelativePath {Boolean} - \`false\` 1`] = ` +exports[`when applied with \`useRelativePath\` option matches snapshot for \`false\` value (\`{Boolean}\`) 1`] = ` Object { "assets": Array [ "9c87cbf3ba33126ffd25ae7f2f6bbafb.png", @@ -9,7 +9,7 @@ Object { } `; -exports[`Options useRelativePath {Boolean} - \`true\` 1`] = ` +exports[`when applied with \`useRelativePath\` option matches snapshot for \`true\` value (\`{Boolean}\`) 1`] = ` Object { "assets": Array [ "assets/9c87cbf3ba33126ffd25ae7f2f6bbafb.png", @@ -18,7 +18,7 @@ Object { } `; -exports[`Options useRelativePath {Boolean} - \`true\` with absolute \`context\` 1`] = ` +exports[`when applied with \`useRelativePath\` option matches snapshot for \`true\` value (\`{Boolean}\`) and with absolute \`context\` 1`] = ` Object { "assets": Array [ "file-loader/test/fixtures/nested/assets/9c87cbf3ba33126ffd25ae7f2f6bbafb.png", @@ -27,7 +27,7 @@ Object { } `; -exports[`Options useRelativePath {Boolean} - \`true\` with relative \`context\` 1`] = ` +exports[`when applied with \`useRelativePath\` option matches snapshot for \`true\` value (\`{Boolean}\`) and with relative \`context\` 1`] = ` Object { "assets": Array [ "assets/9c87cbf3ba33126ffd25ae7f2f6bbafb.png", diff --git a/test/context-option.test.js b/test/context-option.test.js new file mode 100644 index 0000000..64551db --- /dev/null +++ b/test/context-option.test.js @@ -0,0 +1,20 @@ +import webpack from './helpers/compiler'; + +describe('when applied with `context` option', () => { + it('matches snapshot for `{String}` value', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + context: `${__dirname}`, + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { source } = module; + + expect(source).toMatchSnapshot(); + }); +}); diff --git a/test/emitFile-option.test.js b/test/emitFile-option.test.js new file mode 100644 index 0000000..065c4df --- /dev/null +++ b/test/emitFile-option.test.js @@ -0,0 +1,38 @@ +import webpack from './helpers/compiler'; + +describe('when applied with `emitFile` option', () => { + it('matches snapshot for `true` (`{Boolean}`) (default)', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + emitFile: true, + }, + }, + }; + + const stats = await webpack('emitFile/fixture.js', config); + const [, module] = stats.toJson().modules; + const { assets } = module; + + expect(assets[0]).toMatchSnapshot(); + }); + + it('matches snapshot for `false` value (`{Boolean}`)', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + emitFile: false, + }, + }, + }; + + const stats = await webpack('emitFile/fixture.js', config); + const [module] = stats.toJson().modules; + const { assets } = module; + + // eslint-disable-next-line no-undefined + expect(assets[0]).toBe(undefined); + }); +}); diff --git a/test/errors.test.js b/test/errors.test.js new file mode 100644 index 0000000..fb9f879 --- /dev/null +++ b/test/errors.test.js @@ -0,0 +1,14 @@ +import loader from '../src'; + +describe('errors', () => { + it('validation errors', () => { + const err = () => + loader.call({ + query: { useRelativePath: 1 }, + emitFile: true, + }); + + expect(err).toThrow(); + expect(err).toThrowErrorMatchingSnapshot(); + }); +}); diff --git a/test/loader.test.js b/test/loader.test.js index 609d415..20ca40d 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -1,7 +1,7 @@ import webpack from './helpers/compiler'; -describe('Loader', () => { - test('Defaults', async () => { +describe('loader', () => { + it('should works without options', async () => { const config = { loader: { test: /(png|jpg|svg)/, diff --git a/test/name-option.test.js b/test/name-option.test.js new file mode 100644 index 0000000..14fd84e --- /dev/null +++ b/test/name-option.test.js @@ -0,0 +1,39 @@ +import webpack from './helpers/compiler'; + +describe('when applied with `name` option', () => { + it('matches snapshot for `{String}` value', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + name: '[hash].[ext]', + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { source } = module; + + expect(source).toMatchSnapshot(); + }); + + it('matches snapshot for `{Function}` value', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + name() { + return '[hash].[ext]'; + }, + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { source } = module; + + expect(source).toMatchSnapshot(); + }); +}); diff --git a/test/options/__snapshots__/context.test.js.snap b/test/options/__snapshots__/context.test.js.snap deleted file mode 100644 index f4c9a32..0000000 --- a/test/options/__snapshots__/context.test.js.snap +++ /dev/null @@ -1,3 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Options context {String} 1`] = `"module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`; diff --git a/test/options/__snapshots__/emitFile.test.js.snap b/test/options/__snapshots__/emitFile.test.js.snap deleted file mode 100644 index 7fca6ba..0000000 --- a/test/options/__snapshots__/emitFile.test.js.snap +++ /dev/null @@ -1,3 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Options emitFile {Boolean} True (Default) 1`] = `"64eababb117f90535085779cc0325003.svg"`; diff --git a/test/options/__snapshots__/name.test.js.snap b/test/options/__snapshots__/name.test.js.snap deleted file mode 100644 index f0fdb4a..0000000 --- a/test/options/__snapshots__/name.test.js.snap +++ /dev/null @@ -1,5 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Options name {Function} 1`] = `"module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`; - -exports[`Options name {String} 1`] = `"module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`; diff --git a/test/options/context.test.js b/test/options/context.test.js deleted file mode 100644 index 3fb8c1e..0000000 --- a/test/options/context.test.js +++ /dev/null @@ -1,22 +0,0 @@ -import webpack from '../helpers/compiler'; - -describe('Options', () => { - describe('context', () => { - test('{String}', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - context: `${__dirname}`, - }, - }, - }; - - const stats = await webpack('fixture.js', config); - const [module] = stats.toJson().modules; - const { source } = module; - - expect(source).toMatchSnapshot(); - }); - }); -}); diff --git a/test/options/emitFile.test.js b/test/options/emitFile.test.js deleted file mode 100644 index cba0a06..0000000 --- a/test/options/emitFile.test.js +++ /dev/null @@ -1,42 +0,0 @@ -import webpack from '../helpers/compiler'; - -describe('Options', () => { - describe('emitFile', () => { - describe('{Boolean}', () => { - test('True (Default)', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - emitFile: true, - }, - }, - }; - - const stats = await webpack('emitFile/fixture.js', config); - const [, module] = stats.toJson().modules; - const { assets } = module; - - expect(assets[0]).toMatchSnapshot(); - }); - - test('False', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - emitFile: false, - }, - }, - }; - - const stats = await webpack('emitFile/fixture.js', config); - const [module] = stats.toJson().modules; - const { assets } = module; - - // eslint-disable-next-line no-undefined - expect(assets[0]).toBe(undefined); - }); - }); - }); -}); diff --git a/test/options/name.test.js b/test/options/name.test.js deleted file mode 100644 index 8f9c20b..0000000 --- a/test/options/name.test.js +++ /dev/null @@ -1,44 +0,0 @@ -/* eslint-disable - prefer-destructuring, -*/ -import webpack from '../helpers/compiler'; - -describe('Options', () => { - describe('name', () => { - test('{String}', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - name: '[hash].[ext]', - }, - }, - }; - - const stats = await webpack('fixture.js', config); - const [module] = stats.toJson().modules; - const { source } = module; - - expect(source).toMatchSnapshot(); - }); - - test('{Function}', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - name() { - return '[hash].[ext]'; - }, - }, - }, - }; - - const stats = await webpack('fixture.js', config); - const [module] = stats.toJson().modules; - const { source } = module; - - expect(source).toMatchSnapshot(); - }); - }); -}); diff --git a/test/options/outputPath.test.js b/test/options/outputPath.test.js deleted file mode 100644 index aa10169..0000000 --- a/test/options/outputPath.test.js +++ /dev/null @@ -1,194 +0,0 @@ -import webpack from '../helpers/compiler'; - -describe('Options', () => { - describe('outputPath', () => { - test('{String}', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - outputPath: 'output_path/', - }, - }, - }; - - const stats = await webpack('fixture.js', config); - const [module] = stats.toJson().modules; - const { assets, source } = module; - - expect({ assets, source }).toMatchSnapshot(); - }); - - test('{String} without trailing slash', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - outputPath: 'output_path', - }, - }, - }; - - const stats = await webpack('fixture.js', config); - const [module] = stats.toJson().modules; - const { assets, source } = module; - - expect({ assets, source }).toMatchSnapshot(); - }); - - test('{String} with `options.name`', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - name: '[path][name].[ext]', - outputPath: 'output_path/', - }, - }, - }; - - const stats = await webpack('fixture.js', config); - const [module] = stats.toJson().modules; - const { assets, source } = module; - - expect({ assets, source }).toMatchSnapshot(); - }); - - test('{Function}', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - outputPath(url) { - return `output_path_func/${url}`; - }, - }, - }, - }; - - const stats = await webpack('fixture.js', config); - const [module] = stats.toJson().modules; - const { assets, source } = module; - - expect({ assets, source }).toMatchSnapshot(); - }); - - test('{Function} with `options.name`', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - name: '[name].[ext]', - outputPath(url) { - return `output_path_func/${url}`; - }, - }, - }, - }; - - const stats = await webpack('fixture.js', config); - const [module] = stats.toJson().modules; - const { assets, source } = module; - - expect({ assets, source }).toMatchSnapshot(); - }); - - test('{String} with `publicPath` {String}', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - outputPath: 'output_path/', - publicPath: 'public_path/', - }, - }, - }; - - const stats = await webpack('fixture.js', config); - const [module] = stats.toJson().modules; - const { assets, source } = module; - - expect({ assets, source }).toMatchSnapshot(); - }); - - test('{String} with `publicPath` {String} without trailing slash', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - outputPath: 'output_path', - publicPath: 'public_path', - }, - }, - }; - - const stats = await webpack('fixture.js', config); - const [module] = stats.toJson().modules; - const { assets, source } = module; - - expect({ assets, source }).toMatchSnapshot(); - }); - - test('{Function} with `publicPath` {String}', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - publicPath: 'public_path/', - outputPath(url) { - return `output_path_func/${url}`; - }, - }, - }, - }; - - const stats = await webpack('fixture.js', config); - const [module] = stats.toJson().modules; - const { assets, source } = module; - - expect({ assets, source }).toMatchSnapshot(); - }); - - test('{String} with `publicPath` {Function}', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - outputPath: 'output_path/', - publicPath(url) { - return `public_path_func/${url}`; - }, - }, - }, - }; - - const stats = await webpack('fixture.js', config); - const [module] = stats.toJson().modules; - const { assets, source } = module; - - expect({ assets, source }).toMatchSnapshot(); - }); - - test('{Function} with `publicPath` {Function}', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - outputPath(url) { - return `output_path_func/${url}`; - }, - publicPath(url) { - return `public_path_func/${url}`; - }, - }, - }, - }; - - const stats = await webpack('fixture.js', config); - const [module] = stats.toJson().modules; - const { assets, source } = module; - - expect({ assets, source }).toMatchSnapshot(); - }); - }); -}); diff --git a/test/options/publicPath.test.js b/test/options/publicPath.test.js deleted file mode 100644 index f6f5171..0000000 --- a/test/options/publicPath.test.js +++ /dev/null @@ -1,75 +0,0 @@ -import webpack from '../helpers/compiler'; - -describe('Options', () => { - describe('publicPath', () => { - test('{String}', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - publicPath: 'public_path/', - }, - }, - }; - - const stats = await webpack('fixture.js', config); - const [module] = stats.toJson().modules; - const { assets, source } = module; - - expect({ assets, source }).toMatchSnapshot(); - }); - - test('{String} - Without trailing slash', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - publicPath: 'public_path', - }, - }, - }; - - const stats = await webpack('fixture.js', config); - const [module] = stats.toJson().modules; - const { assets, source } = module; - - expect({ assets, source }).toMatchSnapshot(); - }); - - test('{String} - URL', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - publicPath: 'https://cdn.com/', - }, - }, - }; - - const stats = await webpack('fixture.js', config); - const [module] = stats.toJson().modules; - const { assets, source } = module; - - expect({ assets, source }).toMatchSnapshot(); - }); - - test('{Function}', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - publicPath(url) { - return `public_path/${url}`; - }, - }, - }, - }; - - const stats = await webpack('fixture.js', config); - const [module] = stats.toJson().modules; - const { assets, source } = module; - - expect({ assets, source }).toMatchSnapshot(); - }); - }); -}); diff --git a/test/options/useRelativePath.test.js b/test/options/useRelativePath.test.js deleted file mode 100644 index 4e75fe7..0000000 --- a/test/options/useRelativePath.test.js +++ /dev/null @@ -1,80 +0,0 @@ -import webpack from '../helpers/compiler'; - -describe('Options', () => { - describe('useRelativePath', () => { - test('{Boolean} - `false`', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - useRelativePath: false, - }, - }, - }; - - const stats = await webpack('nested/fixture.js', config); - const [module] = stats.toJson().modules; - const { assets, source } = module; - - expect({ assets, source }).toMatchSnapshot(); - }); - - test('{Boolean} - `true`', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - useRelativePath: true, - }, - }, - }; - - const stats = await webpack('nested/fixture.js', config); - const [module] = stats.toJson().modules; - const { assets, source } = module; - - expect({ assets, source }).toMatchSnapshot(); - }); - - test('{Boolean} - `true` with relative `context`', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - context: './test/fixtures/nested/', - useRelativePath: true, - }, - }, - }; - - const stats = await webpack('nested/fixture.js', config); - const [module] = stats.toJson().modules; - const { assets, source } = module; - - expect({ assets, source }).toMatchSnapshot(); - }); - - test('{Boolean} - `true` with absolute `context`', async () => { - const config = { - loader: { - test: /(png|jpg|svg)/, - options: { - context: '../nested/', - useRelativePath: true, - }, - }, - }; - - const stats = await webpack('nested/fixture.js', config); - const [module] = stats.toJson().modules; - let { assets, source } = module; - - if (process.env.CIRCLECI) { - assets = [assets[0].replace('project', 'file-loader')]; - source = source.replace('project', 'file-loader'); - } - - expect({ assets, source }).toMatchSnapshot(); - }); - }); -}); diff --git a/test/outputPath-option.test.js b/test/outputPath-option.test.js new file mode 100644 index 0000000..a6e565f --- /dev/null +++ b/test/outputPath-option.test.js @@ -0,0 +1,192 @@ +import webpack from './helpers/compiler'; + +describe('when applied with `outputPath` option', () => { + it('matches snapshot for `{String}` value', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + outputPath: 'output_path/', + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); + + it('matches snapshot for `{String}` value without trailing slash', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + outputPath: 'output_path', + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); + + it('matches snapshot for `{String}` with `name` option', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + name: '[path][name].[ext]', + outputPath: 'output_path/', + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); + + it('matches snapshot for `{Function}` value', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + outputPath(url) { + return `output_path_func/${url}`; + }, + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); + + it('matches snapshot for `{Function}` value with `options.name`', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + name: '[name].[ext]', + outputPath(url) { + return `output_path_func/${url}`; + }, + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); + + it('matches snapshot for `{String}` value with `publicPath` (`{String}`)', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + outputPath: 'output_path/', + publicPath: 'public_path/', + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); + + it('matches snapshot for `{String}` value with `publicPath` (`{String}`) without trailing slash', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + outputPath: 'output_path', + publicPath: 'public_path', + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); + + it('matches snapshot for `{Function}` value with `publicPath` (`{String}`)', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + publicPath: 'public_path/', + outputPath(url) { + return `output_path_func/${url}`; + }, + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); + + it('matches snapshot for `{String}` value with `publicPath` (`{Function}`)', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + outputPath: 'output_path/', + publicPath(url) { + return `public_path_func/${url}`; + }, + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); + + it('matches snapshot for `{Function}` value with `publicPath` (`{Function}`)', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + outputPath(url) { + return `output_path_func/${url}`; + }, + publicPath(url) { + return `public_path_func/${url}`; + }, + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); +}); diff --git a/test/publicPath-option.test.js b/test/publicPath-option.test.js new file mode 100644 index 0000000..70e7cdc --- /dev/null +++ b/test/publicPath-option.test.js @@ -0,0 +1,73 @@ +import webpack from './helpers/compiler'; + +describe('when applied with `publicPath` option', () => { + it('matches snapshot for `{String}` value', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + publicPath: 'public_path/', + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); + + it('matches snapshot for `{String}` value without trailing slash', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + publicPath: 'public_path', + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); + + it('matches snapshot for `{String}` value as URL', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + publicPath: 'https://cdn.com/', + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); + + it('matches snapshot for `{Function}` value', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + publicPath(url) { + return `public_path/${url}`; + }, + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); +}); diff --git a/test/useRelativePath-option.test.js b/test/useRelativePath-option.test.js new file mode 100644 index 0000000..f7e9dfb --- /dev/null +++ b/test/useRelativePath-option.test.js @@ -0,0 +1,78 @@ +import webpack from './helpers/compiler'; + +describe('when applied with `useRelativePath` option', () => { + it('matches snapshot for `false` value (`{Boolean}`)', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + useRelativePath: false, + }, + }, + }; + + const stats = await webpack('nested/fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); + + it('matches snapshot for `true` value (`{Boolean}`)', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + useRelativePath: true, + }, + }, + }; + + const stats = await webpack('nested/fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); + + it('matches snapshot for `true` value (`{Boolean}`) and with relative `context`', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + context: './test/fixtures/nested/', + useRelativePath: true, + }, + }, + }; + + const stats = await webpack('nested/fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); + + it('matches snapshot for `true` value (`{Boolean}`) and with absolute `context`', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + context: '../nested/', + useRelativePath: true, + }, + }, + }; + + const stats = await webpack('nested/fixture.js', config); + const [module] = stats.toJson().modules; + let { assets, source } = module; + + if (process.env.CIRCLECI) { + assets = [assets[0].replace('project', 'file-loader')]; + source = source.replace('project', 'file-loader'); + } + + expect({ assets, source }).toMatchSnapshot(); + }); +});