Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
chore: test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
giulianok committed Jun 29, 2023
1 parent 340f2f5 commit 0515fcd
Show file tree
Hide file tree
Showing 17 changed files with 598 additions and 92 deletions.
1 change: 1 addition & 0 deletions jest.esm.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = {
'!packages/*/test-results/**',
// Despite it not being in the root, coverage reports see this package
'!packages/one-app-locale-bundler/**',
'!packages/one-app-dev-bundler/index.js',
],
roots: [
'packages/one-app-dev-bundler',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Object {
"test": "/path/src/index",
"use": Array [
Object {
"loader": "@americanexpress/one-app-bundler/webpack/loaders/enable-missing-externals-loader",
"loader": "@americanexpress/one-app-bundler/webpack/loaders/enable-unlisted-external-fallbacks-loader",
"options": Object {
"enableUnlistedExternalFallbacks": true,
},
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`enable-unlisted-external-fallbacks-loader should append the enableUnlistedExternalFallbacks to the default export 1`] = `
"import MyComponent from './components/MyComponent';
export default MyComponent;
;
if (!global.BROWSER) {
MyComponent.appConfig = Object.assign({}, MyComponent.appConfig, {
enableUnlistedExternalFallbacks: \\"true\\",
});
}
"
`;

exports[`enable-unlisted-external-fallbacks-loader should throw an error when the wrong syntax is used - export default hoc() 1`] = `"@americanexpress/one-app-bundler: Module must use \`export default VariableName\` in index syntax to use app compatibility validation"`;

exports[`enable-unlisted-external-fallbacks-loader should throw an error when the wrong syntax is used - export from 1`] = `"@americanexpress/one-app-bundler: Module must use \`export default VariableName\` in index syntax to use app compatibility validation"`;

exports[`enable-unlisted-external-fallbacks-loader should throw an error when the wrong syntax is used - module.exports 1`] = `"@americanexpress/one-app-bundler: Module must use \`export default VariableName\` in index syntax to use app compatibility validation"`;
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,49 @@

exports[`externals-loader does not use fallback for server 1`] = `
"try {
const Holocron = require(\\"holocron\\");
const fallbackExternal = Holocron.getExternal({
name: 'lodash',
version: '4.17.21'
});
const rootModuleExternal = global.getTenantRootModule && global.getTenantRootModule().appConfig.providedExternals['lodash'];
if (rootModuleExternal && require('holocron').validateExternal({
providedVersion: rootModuleExternal.version,
requestedRange: '^1.0.0'
})) {
module.exports = rootModuleExternal.module;
} else {
This is CJS code
}
module.exports = fallbackExternal || (rootModuleExternal ? rootModuleExternal.module : () => {
throw new Error('[server][undefined] External not found: lodash');
})
} catch (error) {
const errorGettingExternal = new Error('Failed to get external lodash from root module on the server', error.message);
const errorGettingExternal = new Error([
'[server] Failed to get external fallback lodash',
error.message
].filter(Boolean).join(' :: '));
errorGettingExternal.shouldBlockModuleReload = false;
throw errorGettingExternal;
}
"
`;

exports[`externals-loader should ignore the content and get the dependency from the root module 1`] = `
"try {
const fallbackExternal = global.Holocron.getExternal({
const Holocron = global.Holocron;
const fallbackExternal = Holocron.getExternal({
name: 'lodash',
version: '4.17.21'
});
const rootModuleExternal = global.getTenantRootModule && global.getTenantRootModule().appConfig.providedExternals['lodash'];
module.exports = fallbackExternal || (rootModuleExternal ? rootModuleExternal.module : () => {
throw new Error('External not found: lodash');
});
throw new Error('[undefined][undefined] External not found: lodash');
})
} catch (error) {
const errorGettingExternal = new Error('Failed to get external fallback lodash');
const errorGettingExternal = new Error([
'[undefined] Failed to get external fallback lodash',
error.message
].filter(Boolean).join(' :: '));
errorGettingExternal.shouldBlockModuleReload = false;
throw errorGettingExternal;
}
"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ if (!global.BROWSER) {
SomeComponent.appConfig = Object.assign({}, SomeComponent.appConfig, {
requiredExternals: {
\\"ajv\\": {
\\"name\\": \\"ajv\\",
\\"version\\": \\"6.12.6\\",
\\"semanticRange\\": \\"^6.7.0\\",
\\"filename\\": \\"ajv.js\\"
\\"semanticRange\\": \\"^6.7.0\\"
},
\\"lodash\\": {
\\"name\\": \\"lodash\\",
\\"version\\": \\"4.17.21\\",
\\"semanticRange\\": \\"^4.17.20\\",
\\"filename\\": \\"lodash.js\\"
\\"semanticRange\\": \\"^4.17.20\\"
}
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* under the License.
*/

const fs = require('fs');
const readPkgUp = require('read-pkg-up');
const validateExternalsLoader = require('../../../webpack/loaders/validate-required-externals-loader');

Expand All @@ -23,17 +24,26 @@ jest.mock('read-pkg-up', () => ({
sync: jest.fn(),
}));

jest.mock('fs');

// eslint-disable-next-line global-require -- mocking readPkgUp needs us to require a json file
readPkgUp.sync.mockImplementation(() => ({ packageJson: require('../../../package.json') }));

fs.readFileSync = jest.fn(() => '{}');

describe('validate-required-externals-loader', () => {
beforeEach(() => {
// fs.readFileSync.mockClear();
});

it('should add versions for server side validation', () => {
const content = `\
import SomeComponent from './SomeComponent';
export default SomeComponent;
`;
expect(validateExternalsLoader(content)).toMatchSnapshot();
expect(fs.writeFileSync).toHaveBeenCalled();
});

it('should throw an error when the wrong syntax is used - export from', () => {
Expand Down
Loading

0 comments on commit 0515fcd

Please sign in to comment.