Skip to content

Commit

Permalink
test: convert source-map core tests to use async/await instead of…
Browse files Browse the repository at this point in the history
… `fakeAsync` (angular#46888)

The source map tests rely on asynchronous logic from the `source-map`
package. The tests itself are written using `fakeAsync` but this
unnecessarily complicates the interaction with the asynchronous
source-map helpers/package.

To fix this, we just make the tests use async/await as we don't intend
to test fakeAsync in this `describe` block..

PR Close angular#46888
  • Loading branch information
devversion authored and AndrewKushnir committed Jul 19, 2022
1 parent 4b1204c commit 0dc754a
Showing 1 changed file with 29 additions and 33 deletions.
62 changes: 29 additions & 33 deletions packages/core/test/linker/source_map_integration_node_only_spec.ts
Expand Up @@ -65,30 +65,29 @@ describe('jit source mapping', () => {
function declareTests({ngUrl, templateDecorator}: TestConfig) {
const generatedUrl = 'ng:///MyComp.js';

it('should use the right source url in html parse errors', fakeAsync(() => {
const template = '<div>\n </error>';
@Component({...templateDecorator(template)})
class MyComp {
}
it('should use the right source url in html parse errors', async () => {
const template = '<div>\n </error>';
@Component({...templateDecorator(template)})
class MyComp {
}

expect(() => {
resolveCompileAndCreateComponent(MyComp, template);
}).toThrowError(new RegExp(`${escapeRegExp(ngUrl)}@1:2`));
}));
await expectAsync(resolveCompileAndCreateComponent(MyComp, template))
.toBeRejectedWithError(new RegExp(`${escapeRegExp(ngUrl)}@1:2`));
});

it('should create a sourceMap for templates', fakeAsync(() => {
const template = `Hello World!`;
it('should create a sourceMap for templates', async () => {
const template = `Hello World!`;

@Component({...templateDecorator(template)})
class MyComp {
}
@Component({...templateDecorator(template)})
class MyComp {
}

resolveCompileAndCreateComponent(MyComp, template);
await resolveCompileAndCreateComponent(MyComp, template);

const sourceMap = jitEvaluator.getSourceMap(generatedUrl);
expect(sourceMap.sources).toEqual([generatedUrl, ngUrl]);
expect(sourceMap.sourcesContent).toEqual([' ', template]);
}));
const sourceMap = jitEvaluator.getSourceMap(generatedUrl);
expect(sourceMap.sources).toEqual([generatedUrl, ngUrl]);
expect(sourceMap.sourcesContent).toEqual([' ', template]);
});


it('should report source location for di errors', async () => {
Expand All @@ -108,7 +107,7 @@ describe('jit source mapping', () => {
TestBed.configureTestingModule({declarations: [SomeDir]});
let error: any;
try {
resolveCompileAndCreateComponent(MyComp, template);
await resolveCompileAndCreateComponent(MyComp, template);
} catch (e) {
error = e;
}
Expand Down Expand Up @@ -139,7 +138,7 @@ describe('jit source mapping', () => {
TestBed.configureTestingModule({declarations: [SomeDir]});
let error: any;
try {
resolveCompileAndCreateComponent(MyComp, template);
await resolveCompileAndCreateComponent(MyComp, template);
} catch (e) {
error = e;
}
Expand All @@ -161,7 +160,7 @@ describe('jit source mapping', () => {
}
}

const comp = resolveCompileAndCreateComponent(MyComp, template);
const comp = await resolveCompileAndCreateComponent(MyComp, template);

let error: any;
try {
Expand All @@ -187,7 +186,7 @@ describe('jit source mapping', () => {
}
}

const comp = resolveCompileAndCreateComponent(MyComp, template);
const comp = await resolveCompileAndCreateComponent(MyComp, template);

let error: any;
const errorHandler = TestBed.inject(ErrorHandler);
Expand All @@ -208,28 +207,25 @@ describe('jit source mapping', () => {
}
});

function compileAndCreateComponent(comType: any) {
async function compileAndCreateComponent(comType: any) {
TestBed.configureTestingModule({declarations: [comType]});

let error: any;
TestBed.compileComponents().catch((e) => error = e);
await TestBed.compileComponents();

if (resourceLoader.hasPendingRequests()) {
resourceLoader.flush();
}
tick();
if (error) {
throw error;
}

return TestBed.createComponent(comType);
}

function createResolver(contents: string) {
return (_url: string) => Promise.resolve(contents);
}

function resolveCompileAndCreateComponent(comType: any, template: string) {
resolveComponentResources(createResolver(template));
return compileAndCreateComponent(comType);
async function resolveCompileAndCreateComponent(comType: any, template: string) {
await resolveComponentResources(createResolver(template));
return await compileAndCreateComponent(comType);
}

let ɵcompilerFacade: CompilerFacade;
Expand Down

0 comments on commit 0dc754a

Please sign in to comment.