(expectedBundleStructure, bundle)
| 13 | export const validateTypeOf = allIsFunctions.typeOf |
| 14 | |
| 15 | export function validateBundle (expectedBundleStructure, bundle) { |
| 16 | const originalWarn = console.warn |
| 17 | |
| 18 | console.warn = function (...args) { |
| 19 | if (args.join(' ').includes('is moved to') && args.join(' ').includes('Please use the new location instead')) { |
| 20 | // Ignore warnings like: |
| 21 | // Warning: math.type.isNumber is moved to math.isNumber in v6.0.0. Please use the new location instead. |
| 22 | return |
| 23 | } |
| 24 | |
| 25 | originalWarn.apply(console, args) |
| 26 | } |
| 27 | |
| 28 | try { |
| 29 | const issues = [] |
| 30 | |
| 31 | // see whether all expected functions and objects are there |
| 32 | traverse(expectedBundleStructure, (expectedType, path) => { |
| 33 | const actualValue = get(bundle, path) |
| 34 | const actualType = validateTypeOf(actualValue) |
| 35 | |
| 36 | const message = (actualType === 'undefined') |
| 37 | ? 'Missing entry in bundle. ' + |
| 38 | `Path: ${JSON.stringify(path)}, expected type: ${expectedType}, actual type: ${actualType}` |
| 39 | : 'Unexpected entry type in bundle. ' + |
| 40 | `Path: ${JSON.stringify(path)}, expected type: ${expectedType}, actual type: ${actualType}` |
| 41 | |
| 42 | if (actualType !== expectedType) { |
| 43 | issues.push({ actualType, expectedType, message }) |
| 44 | |
| 45 | console.warn(message) |
| 46 | } |
| 47 | }) |
| 48 | |
| 49 | // see whether there are any functions or objects that shouldn't be there |
| 50 | traverse(bundle, (actualValue, path) => { |
| 51 | const actualType = validateTypeOf(actualValue) |
| 52 | const expectedType = get(expectedBundleStructure, path) || 'undefined' |
| 53 | |
| 54 | // FIXME: ugly to have these special cases |
| 55 | if (path.join('.').includes('docs.')) { |
| 56 | // ignore the contents of docs |
| 57 | return |
| 58 | } |
| 59 | if (path.join('.').includes('all.')) { |
| 60 | // ignore the contents of all dependencies |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | const message = (expectedType === 'undefined') |
| 65 | ? 'Unknown entry in bundle. ' + |
| 66 | 'Is there a new function added which is missing in this snapshot test? ' + |
| 67 | `Path: ${JSON.stringify(path)}, expected type: ${expectedType}, actual type: ${actualType}` |
| 68 | : 'Unexpected entry type in bundle. ' + |
| 69 | `Path: ${JSON.stringify(path)}, expected type: ${expectedType}, actual type: ${actualType}` |
| 70 | |
| 71 | if (actualType !== expectedType) { |
| 72 | issues.push({ actualType, expectedType, message }) |
no test coverage detected
searching dependent graphs…