|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | +import { EmptyTree } from '@angular-devkit/schematics'; |
| 9 | +import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; |
| 10 | + |
| 11 | +describe('Migration to update Web Workers for Webpack 5', () => { |
| 12 | + const schematicRunner = new SchematicTestRunner( |
| 13 | + 'migrations', |
| 14 | + require.resolve('../migration-collection.json'), |
| 15 | + ); |
| 16 | + |
| 17 | + let tree: UnitTestTree; |
| 18 | + |
| 19 | + const workerConsumerPath = 'src/consumer.ts'; |
| 20 | + const workerConsumerContent = ` |
| 21 | + import { enableProdMode } from '@angular/core'; |
| 22 | + import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; |
| 23 | + import { AppModule } from './app/app.module'; |
| 24 | + import { environment } from './environments/environment'; |
| 25 | + if (environment.production) { enableProdMode(); } |
| 26 | + platformBrowserDynamic().bootstrapModule(AppModule).catch(err => console.error(err)); |
| 27 | +
|
| 28 | + const worker = new Worker('./app/app.worker', { type: 'module' }); |
| 29 | + worker.onmessage = ({ data }) => { |
| 30 | + console.log('page got message:', data); |
| 31 | + }; |
| 32 | + worker.postMessage('hello'); |
| 33 | + `; |
| 34 | + |
| 35 | + beforeEach(async () => { |
| 36 | + tree = new UnitTestTree(new EmptyTree()); |
| 37 | + tree.create('/package.json', JSON.stringify({})); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should replace the string path argument with a URL constructor', async () => { |
| 41 | + tree.create(workerConsumerPath, workerConsumerContent); |
| 42 | + |
| 43 | + await schematicRunner.runSchematicAsync('update-web-workers-webpack-5', {}, tree).toPromise(); |
| 44 | + await schematicRunner.engine.executePostTasks().toPromise(); |
| 45 | + |
| 46 | + const consumer = tree.readContent(workerConsumerPath); |
| 47 | + |
| 48 | + expect(consumer).not.toContain(`new Worker('./app/app.worker'`); |
| 49 | + expect(consumer).toContain( |
| 50 | + `new Worker(new URL('./app/app.worker', import.meta.url), { type: 'module' });`, |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should not replace the first argument if arguments types are invalid', async () => { |
| 55 | + tree.create(workerConsumerPath, workerConsumerContent.replace(`'./app/app.worker'`, '42')); |
| 56 | + |
| 57 | + await schematicRunner.runSchematicAsync('update-web-workers-webpack-5', {}, tree).toPromise(); |
| 58 | + await schematicRunner.engine.executePostTasks().toPromise(); |
| 59 | + |
| 60 | + const consumer = tree.readContent(workerConsumerPath); |
| 61 | + |
| 62 | + expect(consumer).toContain(`new Worker(42`); |
| 63 | + expect(consumer).not.toContain( |
| 64 | + `new Worker(new URL('42', import.meta.url), { type: 'module' });`, |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should not replace the first argument if type value is not "module"', async () => { |
| 69 | + tree.create(workerConsumerPath, workerConsumerContent.replace(`type: 'module'`, `type: 'xyz'`)); |
| 70 | + |
| 71 | + await schematicRunner.runSchematicAsync('update-web-workers-webpack-5', {}, tree).toPromise(); |
| 72 | + await schematicRunner.engine.executePostTasks().toPromise(); |
| 73 | + |
| 74 | + const consumer = tree.readContent(workerConsumerPath); |
| 75 | + |
| 76 | + expect(consumer).toContain(`new Worker('./app/app.worker'`); |
| 77 | + expect(consumer).not.toContain( |
| 78 | + `new Worker(new URL('42', import.meta.url), { type: 'xyz' });`, |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should replace the module path string when file has BOM', async () => { |
| 83 | + tree.create(workerConsumerPath, '\uFEFF' + workerConsumerContent); |
| 84 | + |
| 85 | + await schematicRunner.runSchematicAsync('update-web-workers-webpack-5', {}, tree).toPromise(); |
| 86 | + await schematicRunner.engine.executePostTasks().toPromise(); |
| 87 | + |
| 88 | + const consumer = tree.readContent(workerConsumerPath); |
| 89 | + |
| 90 | + expect(consumer).not.toContain(`new Worker('./app/app.worker'`); |
| 91 | + expect(consumer).toContain( |
| 92 | + `new Worker(new URL('./app/app.worker', import.meta.url), { type: 'module' });`, |
| 93 | + ); |
| 94 | + }); |
| 95 | +}); |
0 commit comments