|
| 1 | +import * as proxyquire from 'proxyquire'; |
| 2 | +import * as nsWebpackIndex from '../index'; |
| 3 | +// With noCallThru enabled, `proxyquire` will not fall back to requiring the real module to populate properties that are not mocked. |
| 4 | +// This allows us to mock packages that are not available in node_modules. |
| 5 | +// In case you want to enable fallback for a specific object, just add `'@noCallThru': false`. |
| 6 | +proxyquire.noCallThru(); |
| 7 | + |
| 8 | +class EmptyClass { }; |
| 9 | + |
| 10 | +const nativeScriptDevWebpack = { |
| 11 | + GenerateBundleStarterPlugin: EmptyClass, |
| 12 | + WatchStateLoggerPlugin: EmptyClass, |
| 13 | + PlatformFSPlugin: EmptyClass, |
| 14 | + getAppPath: () => 'app', |
| 15 | + getEntryModule: () => 'EntryModule', |
| 16 | + getResolver: () => null, |
| 17 | + getConvertedExternals: nsWebpackIndex.getConvertedExternals |
| 18 | +}; |
| 19 | + |
| 20 | +const emptyObject = {}; |
| 21 | + |
| 22 | +const webpackConfigAngular = proxyquire('./webpack.angular', { |
| 23 | + 'nativescript-dev-webpack': nativeScriptDevWebpack, |
| 24 | + 'nativescript-dev-webpack/nativescript-target': emptyObject, |
| 25 | + 'nativescript-dev-webpack/transformers/ns-replace-bootstrap': emptyObject, |
| 26 | + 'nativescript-dev-webpack/transformers/ns-replace-lazy-loader': emptyObject, |
| 27 | + 'nativescript-dev-webpack/utils/ast-utils': emptyObject, |
| 28 | + '@ngtools/webpack': { |
| 29 | + AngularCompilerPlugin: EmptyClass |
| 30 | + } |
| 31 | +}); |
| 32 | + |
| 33 | +const webpackConfigTypeScript = proxyquire('./webpack.typescript', { |
| 34 | + 'nativescript-dev-webpack': nativeScriptDevWebpack, |
| 35 | + 'nativescript-dev-webpack/nativescript-target': emptyObject, |
| 36 | +}); |
| 37 | + |
| 38 | +const webpackConfigJavaScript = proxyquire('./webpack.javascript', { |
| 39 | + 'nativescript-dev-webpack': nativeScriptDevWebpack, |
| 40 | + 'nativescript-dev-webpack/nativescript-target': emptyObject, |
| 41 | +}); |
| 42 | + |
| 43 | +const webpackConfigVue = proxyquire('./webpack.vue', { |
| 44 | + 'nativescript-dev-webpack': nativeScriptDevWebpack, |
| 45 | + 'nativescript-dev-webpack/nativescript-target': emptyObject, |
| 46 | + 'vue-loader/lib/plugin': EmptyClass, |
| 47 | + 'nativescript-vue-template-compiler': emptyObject |
| 48 | +}); |
| 49 | + |
| 50 | +describe('webpack.config.js', () => { |
| 51 | + [ |
| 52 | + { type: 'javascript', webpackConfig: webpackConfigJavaScript }, |
| 53 | + { type: 'typescript', webpackConfig: webpackConfigTypeScript }, |
| 54 | + { type: 'angular', webpackConfig: webpackConfigAngular }, |
| 55 | + { type: 'vue', webpackConfig: webpackConfigVue } |
| 56 | + ].forEach(element => { |
| 57 | + const { type, webpackConfig } = element; |
| 58 | + |
| 59 | + describe(`verify externals for webpack.${type}.js`, () => { |
| 60 | + const getInput = (platform: string, externals: string[]) => { |
| 61 | + const input: any = { externals }; |
| 62 | + input[platform] = true; |
| 63 | + return input; |
| 64 | + }; |
| 65 | + |
| 66 | + [ |
| 67 | + 'android', |
| 68 | + 'ios' |
| 69 | + ].forEach(platform => { |
| 70 | + describe(`for ${platform}`, () => { |
| 71 | + afterEach(() => { |
| 72 | + nativeScriptDevWebpack.getConvertedExternals = nsWebpackIndex.getConvertedExternals; |
| 73 | + }); |
| 74 | + |
| 75 | + it('returns empty array when externals are not passed', () => { |
| 76 | + const config = webpackConfig(getInput(platform, null)); |
| 77 | + expect(config.externals).toEqual([]); |
| 78 | + }); |
| 79 | + |
| 80 | + it('calls getConvertedExternals to convert externals', () => { |
| 81 | + let isCalled = false; |
| 82 | + nativeScriptDevWebpack.getConvertedExternals = () => { |
| 83 | + isCalled = true; |
| 84 | + return []; |
| 85 | + }; |
| 86 | + |
| 87 | + const input = getInput(platform, ['nativescript-vue']); |
| 88 | + webpackConfig(input); |
| 89 | + expect(isCalled).toBe(true, 'Webpack.config.js must use the getConvertedExternals method'); |
| 90 | + }); |
| 91 | + |
| 92 | + [ |
| 93 | + { |
| 94 | + input: ['nativescript-vue'], |
| 95 | + expectedOutput: [/^nativescript-vue((\/.*)|$)/] |
| 96 | + }, |
| 97 | + { |
| 98 | + input: ['nativescript-vue', 'nativescript-angular'], |
| 99 | + expectedOutput: [/^nativescript-vue((\/.*)|$)/, /^nativescript-angular((\/.*)|$)/] |
| 100 | + }, |
| 101 | + ].forEach(testCase => { |
| 102 | + const input = getInput(platform, testCase.input); |
| 103 | + |
| 104 | + it(`are correct regular expressions, for input ${testCase.input}`, () => { |
| 105 | + const config = webpackConfig(input); |
| 106 | + expect(config.externals).toEqual(testCase.expectedOutput); |
| 107 | + }); |
| 108 | + }); |
| 109 | + }); |
| 110 | + }); |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
0 commit comments