|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | +const rimraf = require('rimraf'); |
| 6 | +const { run, runAndGetWatchProc, runPromptWithAnswers } = require('../../utils/test-utils'); |
| 7 | + |
| 8 | +const ENTER = '\x0D'; |
| 9 | +const outputDir = 'test-assets'; |
| 10 | +const outputPath = path.join(__dirname, outputDir); |
| 11 | +const outputFile = `${outputDir}/updated-webpack.config.js`; |
| 12 | +const outputFilePath = path.join(__dirname, outputFile); |
| 13 | + |
| 14 | +describe('migrate command', () => { |
| 15 | + beforeEach(() => { |
| 16 | + rimraf.sync(outputPath); |
| 17 | + fs.mkdirSync(outputPath); |
| 18 | + }); |
| 19 | + |
| 20 | + afterAll(() => { |
| 21 | + rimraf.sync(outputPath); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should warn if the source config file is not specified', () => { |
| 25 | + const { stderr } = run(__dirname, ['migrate'], false); |
| 26 | + expect(stderr).toContain('Please specify a path to your webpack config'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should prompt accordingly if an output path is not specified', () => { |
| 30 | + const { stdout } = run(__dirname, ['migrate', 'webpack.config.js'], false); |
| 31 | + expect(stdout).toContain('? Migration output path not specified'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should throw an error if the user refused to overwrite the source file and no output path is provided', async () => { |
| 35 | + const { stderr } = await runAndGetWatchProc(__dirname, ['migrate', 'webpack.config.js'], false, 'n'); |
| 36 | + expect(stderr).toBe('✖ ︎Migration aborted due to no output path'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should prompt for config validation when an output path is provided', async () => { |
| 40 | + const { stdout } = await runAndGetWatchProc(__dirname, ['migrate', 'webpack.config.js', outputFile], false, 'y'); |
| 41 | + // should show the diff of the config file |
| 42 | + expect(stdout).toContain('rules: ['); |
| 43 | + expect(stdout).toContain('? Do you want to validate your configuration?'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should generate an updated config file when an output path is provided', async () => { |
| 47 | + const { stdout, stderr } = await runPromptWithAnswers( |
| 48 | + __dirname, |
| 49 | + ['migrate', 'webpack.config.js', outputFile], |
| 50 | + [ENTER, ENTER], |
| 51 | + true, |
| 52 | + ); |
| 53 | + expect(stdout).toContain('? Do you want to validate your configuration?'); |
| 54 | + // should show the diff of the config file |
| 55 | + expect(stdout).toContain('rules: ['); |
| 56 | + expect(stderr).toBeFalsy(); |
| 57 | + |
| 58 | + expect(fs.existsSync(outputFilePath)).toBeTruthy(); |
| 59 | + // the output file should be a valid config file |
| 60 | + const config = require(outputFilePath); |
| 61 | + expect(config.module.rules).toEqual([ |
| 62 | + { |
| 63 | + test: /\.js$/, |
| 64 | + exclude: /node_modules/, |
| 65 | + |
| 66 | + use: [ |
| 67 | + { |
| 68 | + loader: 'babel-loader', |
| 69 | + |
| 70 | + options: { |
| 71 | + presets: ['@babel/preset-env'], |
| 72 | + }, |
| 73 | + }, |
| 74 | + ], |
| 75 | + }, |
| 76 | + ]); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should generate an updated config file and warn of an invalid webpack config', async () => { |
| 80 | + const { stdout, stderr } = await runPromptWithAnswers(__dirname, ['migrate', 'bad-webpack.config.js', outputFile], [ENTER, ENTER]); |
| 81 | + expect(stdout).toContain('? Do you want to validate your configuration?'); |
| 82 | + expect(stderr).toContain("configuration.output has an unknown property 'badOption'"); |
| 83 | + |
| 84 | + expect(fs.existsSync(outputFilePath)).toBeTruthy(); |
| 85 | + }); |
| 86 | +}); |
0 commit comments