From e82374ae2bac0443112ff365a5017b49ccc5cae5 Mon Sep 17 00:00:00 2001 From: Ben Heidemann Date: Sat, 10 Apr 2021 12:16:38 +0100 Subject: [PATCH] test: add spec for webpack plugin --- tests/index.js | 3 + .../webpack-plugin/webpack-plugin.spec.js | 530 ++++++++++++++++++ 2 files changed, 533 insertions(+) create mode 100644 tests/spec/webpack-plugin/webpack-plugin.spec.js diff --git a/tests/index.js b/tests/index.js index ae109ef..75f797e 100644 --- a/tests/index.js +++ b/tests/index.js @@ -7,6 +7,9 @@ require('./spec/shims/fs.spec'); require('./spec/shims/path.spec'); +// Webpack Plugin +require('./spec/webpack-plugin/webpack-plugin.spec'); + // Filer require('./spec/filer.spec'); require('./spec/filer.buffer.spec.js'); diff --git a/tests/spec/webpack-plugin/webpack-plugin.spec.js b/tests/spec/webpack-plugin/webpack-plugin.spec.js new file mode 100644 index 0000000..ac0eefd --- /dev/null +++ b/tests/spec/webpack-plugin/webpack-plugin.spec.js @@ -0,0 +1,530 @@ +'use strict'; +const expect = require('chai').expect; +const utils = require('../../lib/test-utils'); +const FilerWebpackPlugin = require('../../../src/webpack-plugin/plugin'); + +function createNMFResolveCompilerObject(resolveData) { + + const normalModuleFactory_resolve_tap = utils.createMockFn(function (name, callback) { + callback(resolveData); + }); + + const normalModuleFactory = { + hooks: { + resolve: { + tap: normalModuleFactory_resolve_tap + } + } + } + + const normalModuleFactory_tap = utils.createMockFn(function (name, callback) { + callback(normalModuleFactory); + }); + + const compiler = { + hooks: { + normalModuleFactory: { + tap: normalModuleFactory_tap, + } + } + } + + return { + compiler, + normalModuleFactory_tap, + normalModuleFactory_resolve_tap, + } +} + +describe.only('path shim', () => { + it('should instantiate the plugin with valid options', () => { + // Arrange + const options = { + filerDir: '/filer', + shimsDir: '/shims', + fsProviderDir: '/fsProvider', + shimFs: false, + shimPath: false, + fsProvider: 'custom', + }; + + // Act + const plugin = new FilerWebpackPlugin(options); + + // Assert + expect(plugin.options).to.not.be.undefined; + + expect(plugin.options.filerDir).to.equal(options.filerDir); + expect(plugin.options.shimsDir).to.equal(options.shimsDir); + expect(plugin.options.fsProviderDir).to.equal(options.fsProviderDir); + expect(plugin.options.shimFs).to.equal(options.shimFs); + expect(plugin.options.shimPath).to.equal(options.shimPath); + expect(plugin.options.fsProvider).to.equal(options.fsProvider); + }); + + it('should instantiate the plugin with default options', () => { + // Act + const plugin = new FilerWebpackPlugin(); + + // Assert + expect(plugin.options).to.not.be.undefined; + + expect(plugin.options.filerDir).to.equal('/node_modules/filer'); + expect(plugin.options.shimsDir).to.equal('/node_modules/filer/shims'); + expect(plugin.options.fsProviderDir).to.equal('/node_modules/filer/shims/providers'); + expect(plugin.options.shimFs).to.equal(true); + expect(plugin.options.shimPath).to.equal(true); + expect(plugin.options.fsProvider).to.equal('default'); + }); + + it('should instantiate the plugin with invalid options', () => { + // Arrange + const options = { + filerDir: 123, + shimsDir: 456, + fsProviderDir: 789, + shimFs: 'false', + shimPath: 'false', + fsProvider: false, + }; + + // Act // Assert + expect(() => { + new FilerWebpackPlugin(options); + }).to.throw(); + }); + + it('should instantiate the plugin with options and the tag should be replaced', () => { + // Arrange + const options = { + filerDir: '/filer', + shimsDir: '/shims', + fsProviderDir: '/fsProvider', + }; + + // Act + const plugin = new FilerWebpackPlugin(options); + + // Assert + expect(plugin.options).to.not.be.undefined; + + expect(plugin.options.filerDir).to.equal(options.filerDir.replace('', '')); + expect(plugin.options.shimsDir).to.equal(options.shimsDir.replace('', '')); + expect(plugin.options.fsProviderDir).to.equal(options.fsProviderDir.replace('', '')); + }); + + describe('should instantiate the plugin with valid options and invoke the apply method', () => { + it('should ignore an unrelated module', () => { + // Arrange + const options = { + filerDir: '/filer', + shimsDir: '/shims', + fsProviderDir: '/fsProvider', + shimFs: false, + shimPath: false, + fsProvider: 'custom', + }; + + const resolveData = { + request: 'aModuleWeDontCareAbout', + context: '/some/random/directory', + }; + const resolveDataIn = Object.create(resolveData); + + // Mocks + const { + compiler, + normalModuleFactory_tap, + normalModuleFactory_resolve_tap, + } = createNMFResolveCompilerObject(resolveDataIn); + + // Act + const plugin = new FilerWebpackPlugin(options); + plugin.apply(compiler); + + // Assert + expect(normalModuleFactory_tap.calls).to.have.length(1); + expect(normalModuleFactory_tap.calls[0].args[0]).to.equal('filer-webpack-plugin'); + + expect(normalModuleFactory_resolve_tap.calls).to.have.length(1); + expect(normalModuleFactory_resolve_tap.calls[0].args[0]).to.equal('filer-webpack-plugin'); + + expect(resolveDataIn.request).to.equal(resolveData.request); + }); + + it('should resolve fsProvider to /default.js when fsProvider options is default', () => { + // Arrange + const options = { + filerDir: '/filer', + shimsDir: '/shims', + fsProviderDir: '/fsProvider', + shimFs: false, + shimPath: false, + fsProvider: 'default', + }; + + const resolveData = { + request: 'fsProvider', + context: options.shimsDir, + }; + const resolveDataIn = Object.create(resolveData); + + // Mocks + const { + compiler, + normalModuleFactory_tap, + normalModuleFactory_resolve_tap, + } = createNMFResolveCompilerObject(resolveDataIn); + + // Act + const plugin = new FilerWebpackPlugin(options); + plugin.apply(compiler); + + // Assert + expect(normalModuleFactory_tap.calls).to.have.length(1); + expect(normalModuleFactory_tap.calls[0].args[0]).to.equal('filer-webpack-plugin'); + + expect(normalModuleFactory_resolve_tap.calls).to.have.length(1); + expect(normalModuleFactory_resolve_tap.calls[0].args[0]).to.equal('filer-webpack-plugin'); + + expect(resolveDataIn.request).to.equal(`${options.fsProviderDir}/default.js`); + }); + + it('should resolve fsProvider to /indexeddb.js when fsProvider options is indexeddb', () => { + // Arrange + const options = { + filerDir: '/filer', + shimsDir: '/shims', + fsProviderDir: '/fsProvider', + shimFs: false, + shimPath: false, + fsProvider: 'indexeddb', + }; + + const resolveData = { + request: 'fsProvider', + context: options.shimsDir, + }; + const resolveDataIn = Object.create(resolveData); + + // Mocks + const { + compiler, + normalModuleFactory_tap, + normalModuleFactory_resolve_tap, + } = createNMFResolveCompilerObject(resolveDataIn); + + // Act + const plugin = new FilerWebpackPlugin(options); + plugin.apply(compiler); + + // Assert + expect(normalModuleFactory_tap.calls).to.have.length(1); + expect(normalModuleFactory_tap.calls[0].args[0]).to.equal('filer-webpack-plugin'); + + expect(normalModuleFactory_resolve_tap.calls).to.have.length(1); + expect(normalModuleFactory_resolve_tap.calls[0].args[0]).to.equal('filer-webpack-plugin'); + + expect(resolveDataIn.request).to.equal(`${options.fsProviderDir}/indexeddb.js`); + }); + + it('should resolve fsProvider to /memory.js when fsProvider options is memory', () => { + // Arrange + const options = { + filerDir: '/filer', + shimsDir: '/shims', + fsProviderDir: '/fsProvider', + shimFs: false, + shimPath: false, + fsProvider: 'memory', + }; + + const resolveData = { + request: 'fsProvider', + context: options.shimsDir, + }; + const resolveDataIn = Object.create(resolveData); + + // Mocks + const { + compiler, + normalModuleFactory_tap, + normalModuleFactory_resolve_tap, + } = createNMFResolveCompilerObject(resolveDataIn); + + // Act + const plugin = new FilerWebpackPlugin(options); + plugin.apply(compiler); + + // Assert + expect(normalModuleFactory_tap.calls).to.have.length(1); + expect(normalModuleFactory_tap.calls[0].args[0]).to.equal('filer-webpack-plugin'); + + expect(normalModuleFactory_resolve_tap.calls).to.have.length(1); + expect(normalModuleFactory_resolve_tap.calls[0].args[0]).to.equal('filer-webpack-plugin'); + + expect(resolveDataIn.request).to.equal(`${options.fsProviderDir}/memory.js`); + }); + + it('should resolve fsProvider to /custom.js when fsProvider options is custom', () => { + // Arrange + const options = { + filerDir: '/filer', + shimsDir: '/shims', + fsProviderDir: '/fsProvider', + shimFs: false, + shimPath: false, + fsProvider: 'custom', + }; + + const resolveData = { + request: 'fsProvider', + context: options.shimsDir, + }; + const resolveDataIn = Object.create(resolveData); + + // Mocks + const { + compiler, + normalModuleFactory_tap, + normalModuleFactory_resolve_tap, + } = createNMFResolveCompilerObject(resolveDataIn); + + // Act + const plugin = new FilerWebpackPlugin(options); + plugin.apply(compiler); + + // Assert + expect(normalModuleFactory_tap.calls).to.have.length(1); + expect(normalModuleFactory_tap.calls[0].args[0]).to.equal('filer-webpack-plugin'); + + expect(normalModuleFactory_resolve_tap.calls).to.have.length(1); + expect(normalModuleFactory_resolve_tap.calls[0].args[0]).to.equal('filer-webpack-plugin'); + + expect(resolveDataIn.request).to.equal(`${options.fsProviderDir}/custom.js`); + }); + + it('should throw an error when an invalid options is provided for fsProvider', () => { + // Arrange + const options = { + filerDir: '/filer', + shimsDir: '/shims', + fsProviderDir: '/fsProvider', + shimFs: false, + shimPath: false, + fsProvider: 'invalid_fs_provider_option', + }; + + const resolveData = { + request: 'fsProvider', + context: options.shimsDir, + }; + const resolveDataIn = Object.create(resolveData); + + // Mocks + const { + compiler, + normalModuleFactory_tap, + normalModuleFactory_resolve_tap, + } = createNMFResolveCompilerObject(resolveDataIn); + + // Act + const plugin = new FilerWebpackPlugin(options); + + // Act // Assert + expect(() => { + plugin.apply(compiler); + }).to.throw(); + }); + + it('should ignore a request for fs when the context is in the filer directory', () => { + // Arrange + const options = { + filerDir: '/filer', + shimsDir: '/shims', + fsProviderDir: '/fsProvider', + shimFs: false, + shimPath: false, + fsProvider: 'custom', + }; + + const resolveData = { + request: 'fs', + context: options.filerDir + '/src', + }; + const resolveDataIn = Object.create(resolveData); + + // Mocks + const { + compiler, + normalModuleFactory_tap, + normalModuleFactory_resolve_tap, + } = createNMFResolveCompilerObject(resolveDataIn); + + // Act + const plugin = new FilerWebpackPlugin(options); + plugin.apply(compiler); + + // Assert + expect(normalModuleFactory_tap.calls).to.have.length(1); + expect(normalModuleFactory_tap.calls[0].args[0]).to.equal('filer-webpack-plugin'); + + expect(normalModuleFactory_resolve_tap.calls).to.have.length(1); + expect(normalModuleFactory_resolve_tap.calls[0].args[0]).to.equal('filer-webpack-plugin'); + + expect(resolveDataIn.request).to.equal(resolveData.request); + }); + + it('should resolve a request for fs to the fs shim when the context is not in the filer directory', () => { + // Arrange + const options = { + filerDir: '/filer', + shimsDir: '/shims', + fsProviderDir: '/fsProvider', + shimFs: true, + shimPath: true, + fsProvider: 'custom', + }; + + const resolveData = { + request: 'fs', + context: '/some/random/directory', + }; + const resolveDataIn = Object.create(resolveData); + + // Mocks + const { + compiler, + normalModuleFactory_tap, + normalModuleFactory_resolve_tap, + } = createNMFResolveCompilerObject(resolveDataIn); + + // Act + const plugin = new FilerWebpackPlugin(options); + plugin.apply(compiler); + + // Assert + expect(normalModuleFactory_tap.calls).to.have.length(1); + expect(normalModuleFactory_tap.calls[0].args[0]).to.equal('filer-webpack-plugin'); + + expect(normalModuleFactory_resolve_tap.calls).to.have.length(1); + expect(normalModuleFactory_resolve_tap.calls[0].args[0]).to.equal('filer-webpack-plugin'); + + expect(resolveDataIn.request).to.equal(`${options.shimsDir}/fs.js`); + }); + + it('should not resolve a request for fs to the fs shim when the context is not in the filer directory but shimFs is false', () => { + // Arrange + const options = { + filerDir: '/filer', + shimsDir: '/shims', + fsProviderDir: '/fsProvider', + shimFs: false, + shimPath: false, + fsProvider: 'custom', + }; + + const resolveData = { + request: 'fs', + context: '/some/random/directory', + }; + const resolveDataIn = Object.create(resolveData); + + // Mocks + const { + compiler, + normalModuleFactory_tap, + normalModuleFactory_resolve_tap, + } = createNMFResolveCompilerObject(resolveDataIn); + + // Act + const plugin = new FilerWebpackPlugin(options); + plugin.apply(compiler); + + // Assert + expect(normalModuleFactory_tap.calls).to.have.length(1); + expect(normalModuleFactory_tap.calls[0].args[0]).to.equal('filer-webpack-plugin'); + + expect(normalModuleFactory_resolve_tap.calls).to.have.length(1); + expect(normalModuleFactory_resolve_tap.calls[0].args[0]).to.equal('filer-webpack-plugin'); + + expect(resolveDataIn.request).to.equal(resolveData.request); + }); + + it('should resolve a request for path to the path shim when the context is not in the filer directory', () => { + // Arrange + const options = { + filerDir: '/filer', + shimsDir: '/shims', + fsProviderDir: '/fsProvider', + shimFs: true, + shimPath: true, + fsProvider: 'custom', + }; + + const resolveData = { + request: 'path', + context: '/some/random/directory', + }; + const resolveDataIn = Object.create(resolveData); + + // Mocks + const { + compiler, + normalModuleFactory_tap, + normalModuleFactory_resolve_tap, + } = createNMFResolveCompilerObject(resolveDataIn); + + // Act + const plugin = new FilerWebpackPlugin(options); + plugin.apply(compiler); + + // Assert + expect(normalModuleFactory_tap.calls).to.have.length(1); + expect(normalModuleFactory_tap.calls[0].args[0]).to.equal('filer-webpack-plugin'); + + expect(normalModuleFactory_resolve_tap.calls).to.have.length(1); + expect(normalModuleFactory_resolve_tap.calls[0].args[0]).to.equal('filer-webpack-plugin'); + + expect(resolveDataIn.request).to.equal(`${options.shimsDir}/path.js`); + }); + + it('should not resolve a request for path to the path shim when the context is not in the filer directory but shimPath is false', () => { + // Arrange + const options = { + filerDir: '/filer', + shimsDir: '/shims', + fsProviderDir: '/fsProvider', + shimFs: false, + shimPath: false, + fsProvider: 'custom', + }; + + const resolveData = { + request: 'path', + context: '/some/random/directory', + }; + const resolveDataIn = Object.create(resolveData); + + // Mocks + const { + compiler, + normalModuleFactory_tap, + normalModuleFactory_resolve_tap, + } = createNMFResolveCompilerObject(resolveDataIn); + + // Act + const plugin = new FilerWebpackPlugin(options); + plugin.apply(compiler); + + // Assert + expect(normalModuleFactory_tap.calls).to.have.length(1); + expect(normalModuleFactory_tap.calls[0].args[0]).to.equal('filer-webpack-plugin'); + + expect(normalModuleFactory_resolve_tap.calls).to.have.length(1); + expect(normalModuleFactory_resolve_tap.calls[0].args[0]).to.equal('filer-webpack-plugin'); + + expect(resolveDataIn.request).to.equal(resolveData.request); + }); + }); +});