|
| 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 { |
| 5 | + createWebpackDevServerDriver, |
| 6 | + WEBPACK_CLI_VERSION, |
| 7 | + WEBPACK_DEV_SERVER_VERSION, |
| 8 | +} from './sandbox/WebpackDevServerDriver'; |
| 9 | + |
| 10 | +describe('Webpack Issue Scope', () => { |
| 11 | + let sandbox: Sandbox; |
| 12 | + |
| 13 | + beforeAll(async () => { |
| 14 | + sandbox = await createSandbox(); |
| 15 | + }); |
| 16 | + |
| 17 | + beforeEach(async () => { |
| 18 | + await sandbox.reset(); |
| 19 | + }); |
| 20 | + |
| 21 | + afterAll(async () => { |
| 22 | + await sandbox.cleanup(); |
| 23 | + }); |
| 24 | + |
| 25 | + it.each([ |
| 26 | + { webpack: '4.0.0', async: true, scope: 'webpack' }, |
| 27 | + { webpack: '4.0.0', async: false, scope: 'all' }, |
| 28 | + { webpack: '^4.0.0', async: false, scope: 'webpack' }, |
| 29 | + { webpack: '^4.0.0', async: true, scope: 'all' }, |
| 30 | + { webpack: '^5.0.0-beta.16', async: true, scope: 'webpack' }, |
| 31 | + { webpack: '^5.0.0-beta.16', async: false, scope: 'all' }, |
| 32 | + ])( |
| 33 | + 'reports errors only related to the given scope with %p', |
| 34 | + async ({ webpack, async, scope }) => { |
| 35 | + await sandbox.load([ |
| 36 | + await readFixture(join(__dirname, 'fixtures/environment/typescript-basic.fixture'), { |
| 37 | + FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION: JSON.stringify( |
| 38 | + FORK_TS_CHECKER_WEBPACK_PLUGIN_VERSION |
| 39 | + ), |
| 40 | + TS_LOADER_VERSION: JSON.stringify('^5.0.0'), |
| 41 | + TYPESCRIPT_VERSION: JSON.stringify('~3.8.0'), |
| 42 | + WEBPACK_VERSION: JSON.stringify(webpack), |
| 43 | + WEBPACK_CLI_VERSION: JSON.stringify(WEBPACK_CLI_VERSION), |
| 44 | + WEBPACK_DEV_SERVER_VERSION: JSON.stringify(WEBPACK_DEV_SERVER_VERSION), |
| 45 | + ASYNC: JSON.stringify(async), |
| 46 | + }), |
| 47 | + await readFixture(join(__dirname, 'fixtures/implementation/typescript-basic.fixture')), |
| 48 | + ]); |
| 49 | + |
| 50 | + // add importsNotUsedAsValues which is supported from TypeScript 3.8.0+ |
| 51 | + // this option is required for proper watching of type-only files in the `transpileOnly: true` mode |
| 52 | + await sandbox.patch( |
| 53 | + './tsconfig.json', |
| 54 | + ' "outDir": "./dist"', |
| 55 | + [' "outDir": "./dist",', ' "importsNotUsedAsValues": "preserve"'].join('\n') |
| 56 | + ); |
| 57 | + |
| 58 | + // update configuration |
| 59 | + await sandbox.write( |
| 60 | + 'fork-ts-checker.config.js', |
| 61 | + `module.exports = { issue: { scope: ${JSON.stringify(scope)} } };` |
| 62 | + ); |
| 63 | + await sandbox.write('src/notUsedFile.ts', 'const x: number = "1";'); |
| 64 | + |
| 65 | + const driver = createWebpackDevServerDriver( |
| 66 | + sandbox.spawn('npm run webpack-dev-server'), |
| 67 | + async |
| 68 | + ); |
| 69 | + |
| 70 | + // first compilation should be successful only if we use "webpack" scope |
| 71 | + if (scope === 'webpack') { |
| 72 | + await driver.waitForNoErrors(); |
| 73 | + |
| 74 | + // add reference to the file to include it to the compilation |
| 75 | + await sandbox.patch( |
| 76 | + 'src/model/User.ts', |
| 77 | + "import { Role } from './Role';", |
| 78 | + "import { Role } from './Role';\nimport '../notUsedFile';" |
| 79 | + ); |
| 80 | + |
| 81 | + const errors = await driver.waitForErrors(); |
| 82 | + expect(errors).toEqual([ |
| 83 | + [ |
| 84 | + 'ERROR in src/notUsedFile.ts 1:7-8', |
| 85 | + "TS2322: Type '\"1\"' is not assignable to type 'number'.", |
| 86 | + ' > 1 | const x: number = "1";', |
| 87 | + ' | ^', |
| 88 | + ].join('\n'), |
| 89 | + ]); |
| 90 | + |
| 91 | + // remove reference to the file to exclude it from the compilation |
| 92 | + await sandbox.patch('src/model/User.ts', "import '../notUsedFile';", ''); |
| 93 | + |
| 94 | + await driver.waitForNoErrors(); |
| 95 | + } else { |
| 96 | + const errors = await driver.waitForErrors(); |
| 97 | + expect(errors).toEqual([ |
| 98 | + [ |
| 99 | + 'ERROR in src/notUsedFile.ts 1:7-8', |
| 100 | + "TS2322: Type '\"1\"' is not assignable to type 'number'.", |
| 101 | + ' > 1 | const x: number = "1";', |
| 102 | + ' | ^', |
| 103 | + ].join('\n'), |
| 104 | + ]); |
| 105 | + } |
| 106 | + } |
| 107 | + ); |
| 108 | +}); |
0 commit comments