|
| 1 | +import { readFixture } from './sandbox/Fixture'; |
| 2 | +import { join } from 'path'; |
| 3 | +import { createSandbox, FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION, Sandbox } from './sandbox/Sandbox'; |
| 4 | +import { WEBPACK_CLI_VERSION, WEBPACK_DEV_SERVER_VERSION } from './sandbox/WebpackDevServerDriver'; |
| 5 | +import { extractWebpackErrors } from './sandbox/WebpackErrorsExtractor'; |
| 6 | +import stripAnsi from 'strip-ansi'; |
| 7 | + |
| 8 | +describe('Webpack Production Build', () => { |
| 9 | + let sandbox: Sandbox; |
| 10 | + |
| 11 | + beforeAll(async () => { |
| 12 | + sandbox = await createSandbox(); |
| 13 | + }); |
| 14 | + |
| 15 | + beforeEach(async () => { |
| 16 | + await sandbox.reset(); |
| 17 | + }); |
| 18 | + |
| 19 | + afterAll(async () => { |
| 20 | + await sandbox.cleanup(); |
| 21 | + }); |
| 22 | + |
| 23 | + it.each([{ webpack: '4.0.0' }, { webpack: '^4.0.0' }, { webpack: '^5.0.0-beta.16' }])( |
| 24 | + 'compiles the project successfully with %p', |
| 25 | + async ({ webpack }) => { |
| 26 | + await sandbox.load([ |
| 27 | + await readFixture(join(__dirname, 'fixtures/environment/typescript-basic.fixture'), { |
| 28 | + FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION: JSON.stringify( |
| 29 | + FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION |
| 30 | + ), |
| 31 | + TS_LOADER_VERSION: JSON.stringify('^5.0.0'), |
| 32 | + TYPESCRIPT_VERSION: JSON.stringify('~3.8.0'), |
| 33 | + WEBPACK_VERSION: JSON.stringify(webpack), |
| 34 | + WEBPACK_CLI_VERSION: JSON.stringify(WEBPACK_CLI_VERSION), |
| 35 | + WEBPACK_DEV_SERVER_VERSION: JSON.stringify(WEBPACK_DEV_SERVER_VERSION), |
| 36 | + ASYNC: JSON.stringify(false), |
| 37 | + }), |
| 38 | + await readFixture(join(__dirname, 'fixtures/implementation/typescript-basic.fixture')), |
| 39 | + ]); |
| 40 | + |
| 41 | + // lets remove the async option at all as the plugin should now how to set it by default |
| 42 | + await sandbox.patch( |
| 43 | + 'webpack.config.js', |
| 44 | + [ |
| 45 | + ' new ForkTsCheckerWebpackPlugin({', |
| 46 | + ' async: false,', |
| 47 | + ' logger: {', |
| 48 | + ' infrastructure: "console"', |
| 49 | + ' }', |
| 50 | + ' })', |
| 51 | + ].join('\n'), |
| 52 | + [ |
| 53 | + ' new ForkTsCheckerWebpackPlugin({', |
| 54 | + ' logger: {', |
| 55 | + ' infrastructure: "console"', |
| 56 | + ' }', |
| 57 | + ' })', |
| 58 | + ].join('\n') |
| 59 | + ); |
| 60 | + |
| 61 | + const result = await sandbox.exec('npm run webpack'); |
| 62 | + const errors = extractWebpackErrors(result); |
| 63 | + |
| 64 | + expect(errors).toEqual([]); |
| 65 | + } |
| 66 | + ); |
| 67 | + |
| 68 | + it.each([{ webpack: '4.0.0' }, { webpack: '^4.0.0' }, { webpack: '^5.0.0-beta.16' }])( |
| 69 | + 'exits with error on the project error with %p', |
| 70 | + async ({ webpack }) => { |
| 71 | + await sandbox.load([ |
| 72 | + await readFixture(join(__dirname, 'fixtures/environment/typescript-basic.fixture'), { |
| 73 | + FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION: JSON.stringify( |
| 74 | + FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION |
| 75 | + ), |
| 76 | + TS_LOADER_VERSION: JSON.stringify('^5.0.0'), |
| 77 | + TYPESCRIPT_VERSION: JSON.stringify('~3.8.0'), |
| 78 | + WEBPACK_VERSION: JSON.stringify(webpack), |
| 79 | + WEBPACK_CLI_VERSION: JSON.stringify(WEBPACK_CLI_VERSION), |
| 80 | + WEBPACK_DEV_SERVER_VERSION: JSON.stringify(WEBPACK_DEV_SERVER_VERSION), |
| 81 | + ASYNC: JSON.stringify(false), |
| 82 | + }), |
| 83 | + await readFixture(join(__dirname, 'fixtures/implementation/typescript-basic.fixture')), |
| 84 | + ]); |
| 85 | + |
| 86 | + // remove the async option at all as the plugin should now how to set it by default |
| 87 | + await sandbox.patch( |
| 88 | + 'webpack.config.js', |
| 89 | + [ |
| 90 | + ' new ForkTsCheckerWebpackPlugin({', |
| 91 | + ' async: false,', |
| 92 | + ' logger: {', |
| 93 | + ' infrastructure: "console"', |
| 94 | + ' }', |
| 95 | + ' })', |
| 96 | + ].join('\n'), |
| 97 | + [ |
| 98 | + ' new ForkTsCheckerWebpackPlugin({', |
| 99 | + ' logger: {', |
| 100 | + ' infrastructure: "console"', |
| 101 | + ' }', |
| 102 | + ' })', |
| 103 | + ].join('\n') |
| 104 | + ); |
| 105 | + |
| 106 | + // introduce an error in the project |
| 107 | + await sandbox.remove('src/model/User.ts'); |
| 108 | + |
| 109 | + try { |
| 110 | + await sandbox.exec('npm run webpack'); |
| 111 | + |
| 112 | + throw new Error('The webpack command should exit with an error code.'); |
| 113 | + } catch (error) { |
| 114 | + // remove npm related output |
| 115 | + const output = stripAnsi(String(error)).replace(/npm (ERR!|WARN).*/g, ''); |
| 116 | + // extract errors |
| 117 | + const errors = extractWebpackErrors(output); |
| 118 | + |
| 119 | + expect(errors).toEqual([ |
| 120 | + // first error is from the webpack module resolution |
| 121 | + expect.anything(), |
| 122 | + [ |
| 123 | + 'ERROR in src/authenticate.ts 1:22-36', |
| 124 | + "TS2307: Cannot find module './model/User'.", |
| 125 | + " > 1 | import { User } from './model/User';", |
| 126 | + ' | ^^^^^^^^^^^^^^', |
| 127 | + ' 2 | ', |
| 128 | + ' 3 | async function login(email: string, password: string): Promise<User> {', |
| 129 | + ' 4 | const response = await fetch(', |
| 130 | + ].join('\n'), |
| 131 | + [ |
| 132 | + 'ERROR in src/index.ts 2:29-43', |
| 133 | + "TS2307: Cannot find module './model/User'.", |
| 134 | + " 1 | import { login } from './authenticate';", |
| 135 | + " > 2 | import { getUserName } from './model/User';", |
| 136 | + ' | ^^^^^^^^^^^^^^', |
| 137 | + ' 3 | ', |
| 138 | + " 4 | const emailInput = document.getElementById('email');", |
| 139 | + " 5 | const passwordInput = document.getElementById('password');", |
| 140 | + ].join('\n'), |
| 141 | + ]); |
| 142 | + } |
| 143 | + } |
| 144 | + ); |
| 145 | +}); |
0 commit comments