test(ivy): ngcc refactor mock file-systems to make each spec independent (#30591)

Previously each test relied on large shared mock file-systems, which
makes it difficult to reason about what is actually being tested.

This commit breaks up these big mock file-systems into smaller more
focused chunks.

PR Close #30591
This commit is contained in:
Pete Bacon Darwin 2019-05-22 17:34:09 +01:00 committed by Kara Erickson
parent abbbc69e64
commit 4004d15ba5
1 changed files with 68 additions and 70 deletions

View File

@ -19,7 +19,6 @@ runInEachFileSystem(() => {
let fs: FileSystem; let fs: FileSystem;
beforeEach(() => { beforeEach(() => {
setupMockFileSystem();
SOME_PACKAGE = absoluteFrom('/some_package'); SOME_PACKAGE = absoluteFrom('/some_package');
_ = absoluteFrom; _ = absoluteFrom;
fs = getFileSystem(); fs = getFileSystem();
@ -27,6 +26,16 @@ runInEachFileSystem(() => {
it('should return an object containing absolute paths to the formats of the specified entry-point', it('should return an object containing absolute paths to the formats of the specified entry-point',
() => { () => {
loadTestFiles([
{
name: _('/some_package/valid_entry_point/package.json'),
contents: createPackageJson('valid_entry_point')
},
{
name: _('/some_package/valid_entry_point/valid_entry_point.metadata.json'),
contents: 'some meta data'
},
]);
const entryPoint = getEntryPointInfo( const entryPoint = getEntryPointInfo(
fs, new MockLogger(), SOME_PACKAGE, _('/some_package/valid_entry_point')); fs, new MockLogger(), SOME_PACKAGE, _('/some_package/valid_entry_point'));
expect(entryPoint).toEqual({ expect(entryPoint).toEqual({
@ -40,12 +49,28 @@ runInEachFileSystem(() => {
}); });
it('should return null if there is no package.json at the entry-point path', () => { it('should return null if there is no package.json at the entry-point path', () => {
loadTestFiles([
{
name: _('/some_package/missing_package_json/missing_package_json.metadata.json'),
contents: 'some meta data'
},
]);
const entryPoint = getEntryPointInfo( const entryPoint = getEntryPointInfo(
fs, new MockLogger(), SOME_PACKAGE, _('/some_package/missing_package_json')); fs, new MockLogger(), SOME_PACKAGE, _('/some_package/missing_package_json'));
expect(entryPoint).toBe(null); expect(entryPoint).toBe(null);
}); });
it('should return null if there is no typings or types field in the package.json', () => { it('should return null if there is no typings or types field in the package.json', () => {
loadTestFiles([
{
name: _('/some_package/missing_typings/package.json'),
contents: createPackageJson('missing_typings', {excludes: ['typings']})
},
{
name: _('/some_package/missing_typings/missing_typings.metadata.json'),
contents: 'some meta data'
},
]);
const entryPoint = const entryPoint =
getEntryPointInfo(fs, new MockLogger(), SOME_PACKAGE, _('/some_package/missing_typings')); getEntryPointInfo(fs, new MockLogger(), SOME_PACKAGE, _('/some_package/missing_typings'));
expect(entryPoint).toBe(null); expect(entryPoint).toBe(null);
@ -53,6 +78,12 @@ runInEachFileSystem(() => {
it('should return an object with `compiledByAngular` set to false if there is no metadata.json file next to the typing file', it('should return an object with `compiledByAngular` set to false if there is no metadata.json file next to the typing file',
() => { () => {
loadTestFiles([
{
name: _('/some_package/missing_metadata/package.json'),
contents: createPackageJson('missing_metadata')
},
]);
const entryPoint = getEntryPointInfo( const entryPoint = getEntryPointInfo(
fs, new MockLogger(), SOME_PACKAGE, _('/some_package/missing_metadata')); fs, new MockLogger(), SOME_PACKAGE, _('/some_package/missing_metadata'));
expect(entryPoint).toEqual({ expect(entryPoint).toEqual({
@ -66,6 +97,17 @@ runInEachFileSystem(() => {
}); });
it('should work if the typings field is named `types', () => { it('should work if the typings field is named `types', () => {
loadTestFiles([
{
name: _('/some_package/types_rather_than_typings/package.json'),
contents: createPackageJson('types_rather_than_typings', {}, 'types')
},
{
name:
_('/some_package/types_rather_than_typings/types_rather_than_typings.metadata.json'),
contents: 'some meta data'
},
]);
const entryPoint = getEntryPointInfo( const entryPoint = getEntryPointInfo(
fs, new MockLogger(), SOME_PACKAGE, _('/some_package/types_rather_than_typings')); fs, new MockLogger(), SOME_PACKAGE, _('/some_package/types_rather_than_typings'));
expect(entryPoint).toEqual({ expect(entryPoint).toEqual({
@ -79,70 +121,7 @@ runInEachFileSystem(() => {
}); });
it('should work with Angular Material style package.json', () => { it('should work with Angular Material style package.json', () => {
const entryPoint =
getEntryPointInfo(fs, new MockLogger(), SOME_PACKAGE, _('/some_package/material_style'));
expect(entryPoint).toEqual({
name: 'some_package/material_style',
package: SOME_PACKAGE,
path: _('/some_package/material_style'),
typings: _(`/some_package/material_style/material_style.d.ts`),
packageJson: loadPackageJson(fs, '/some_package/material_style'),
compiledByAngular: true,
});
});
it('should return null if the package.json is not valid JSON', () => {
const entryPoint = getEntryPointInfo(
fs, new MockLogger(), SOME_PACKAGE, _('/some_package/unexpected_symbols'));
expect(entryPoint).toBe(null);
});
});
function setupMockFileSystem(): void {
const _ = absoluteFrom;
loadTestFiles([ loadTestFiles([
{
name: _('/some_package/valid_entry_point/package.json'),
contents: createPackageJson('valid_entry_point')
},
{
name: _('/some_package/valid_entry_point/valid_entry_point.metadata.json'),
contents: 'some meta data'
},
// no package.json!
{
name: _('/some_package/missing_package_json/missing_package_json.metadata.json'),
contents: 'some meta data'
},
{
name: _('/some_package/missing_typings/package.json'),
contents: createPackageJson('missing_typings', {excludes: ['typings']})
},
{
name: _('/some_package/missing_typings/missing_typings.metadata.json'),
contents: 'some meta data'
},
{
name: _('/some_package/types_rather_than_typings/package.json'),
contents: createPackageJson('types_rather_than_typings', {}, 'types')
},
{
name: _('/some_package/types_rather_than_typings/types_rather_than_typings.metadata.json'),
contents: 'some meta data'
},
{
name: _('/some_package/missing_esm2015/package.json'),
contents: createPackageJson('missing_fesm2015', {excludes: ['esm2015', 'fesm2015']})
},
{
name: _('/some_package/missing_esm2015/missing_esm2015.metadata.json'),
contents: 'some meta data'
},
// no metadata.json!
{
name: _('/some_package/missing_metadata/package.json'),
contents: createPackageJson('missing_metadata')
},
{ {
name: _('/some_package/material_style/package.json'), name: _('/some_package/material_style/package.json'),
contents: `{ contents: `{
@ -157,6 +136,21 @@ runInEachFileSystem(() => {
name: _('/some_package/material_style/material_style.metadata.json'), name: _('/some_package/material_style/material_style.metadata.json'),
contents: 'some meta data' contents: 'some meta data'
}, },
]);
const entryPoint =
getEntryPointInfo(fs, new MockLogger(), SOME_PACKAGE, _('/some_package/material_style'));
expect(entryPoint).toEqual({
name: 'some_package/material_style',
package: SOME_PACKAGE,
path: _('/some_package/material_style'),
typings: _(`/some_package/material_style/material_style.d.ts`),
packageJson: loadPackageJson(fs, '/some_package/material_style'),
compiledByAngular: true,
});
});
it('should return null if the package.json is not valid JSON', () => {
loadTestFiles([
// package.json might not be a valid JSON // package.json might not be a valid JSON
// for example, @schematics/angular contains a package.json blueprint // for example, @schematics/angular contains a package.json blueprint
// with unexpected symbols // with unexpected symbols
@ -165,7 +159,11 @@ runInEachFileSystem(() => {
contents: '{"devDependencies": {<% if (!minimal) { %>"@types/jasmine": "~2.8.8" <% } %>}}' contents: '{"devDependencies": {<% if (!minimal) { %>"@types/jasmine": "~2.8.8" <% } %>}}'
}, },
]); ]);
} const entryPoint = getEntryPointInfo(
fs, new MockLogger(), SOME_PACKAGE, _('/some_package/unexpected_symbols'));
expect(entryPoint).toBe(null);
});
});
function createPackageJson( function createPackageJson(
packageName: string, {excludes}: {excludes?: string[]} = {}, packageName: string, {excludes}: {excludes?: string[]} = {},