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

Commit

Permalink
test(coverage): coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
code-forger committed Feb 1, 2024
1 parent b74fcb0 commit 23fa890
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ Object {
}
`;

exports[`extendWebpackConfig should use the correct trailing slash on windows 1`] = `
exports[`extendWebpackConfig should use the provided requiredExternals configured, resolved to the js file 1`] = `
Object {
"test": StringMatching /ajv\\\\\\\\\\$/,
"test": StringMatching /ajv\\.js\\$/,
"use": Array [
Object {
"loader": "@americanexpress/one-app-bundler/webpack/loaders/externals-loader",
Expand All @@ -77,9 +77,9 @@ Object {
}
`;

exports[`extendWebpackConfig should use the correct trailing slash on windows 2`] = `
exports[`extendWebpackConfig should use the provided requiredExternals configured, resolved to the js file 2`] = `
Object {
"test": StringMatching /lodash\\\\\\\\\\$/,
"test": StringMatching /lodash\\.js\\$/,
"use": Array [
Object {
"loader": "@americanexpress/one-app-bundler/webpack/loaders/externals-loader",
Expand All @@ -92,54 +92,7 @@ Object {
}
`;

exports[`extendWebpackConfig should use the correct trailing slash on windows 3`] = `
Object {
"test": "/path/src/index",
"use": Array [
Object {
"loader": "@americanexpress/one-app-bundler/webpack/loaders/validate-required-externals-loader",
"options": Object {
"requiredExternals": Array [
"ajv",
"lodash",
],
},
},
],
}
`;

exports[`extendWebpackConfig should use the provided requiredExternals configured 1`] = `
Object {
"test": StringMatching /ajv\\\\/\\$/,
"use": Array [
Object {
"loader": "@americanexpress/one-app-bundler/webpack/loaders/externals-loader",
"options": Object {
"bundleTarget": undefined,
"externalName": "ajv",
},
},
],
}
`;

exports[`extendWebpackConfig should use the provided requiredExternals configured 2`] = `
Object {
"test": StringMatching /lodash\\\\/\\$/,
"use": Array [
Object {
"loader": "@americanexpress/one-app-bundler/webpack/loaders/externals-loader",
"options": Object {
"bundleTarget": undefined,
"externalName": "lodash",
},
},
],
}
`;

exports[`extendWebpackConfig should use the provided requiredExternals configured 3`] = `
exports[`extendWebpackConfig should use the provided requiredExternals configured, resolved to the js file 3`] = `
Object {
"test": "/path/src/index",
"use": Array [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,22 @@ describe('extendWebpackConfig', () => {
expect(rules[rules.length - 1]).toMatchSnapshot();
});

it('should use the provided requiredExternals configured, resolved to the js file', async () => {
expect.assertions(4);
getConfigOptions.mockReturnValueOnce({ requiredExternals: ['ajv', 'lodash'] });
const result = await extendWebpackConfig(originalWebpackConfig);
const { rules } = result.module;

expect(rules).toHaveLength(originalWebpackConfig.module.rules.length + 3);
expect(rules[rules.length - 3]).toMatchSnapshot({
test: expect.stringMatching(/ajv.js$/),
});
expect(rules[rules.length - 2]).toMatchSnapshot({
test: expect.stringMatching(/lodash.js$/),
});
expect(rules[rules.length - 1]).toMatchSnapshot();
});

it('should enable missing external fallbacks', async () => {
expect.assertions(1);
getConfigOptions.mockReturnValueOnce({ enableUnlistedExternalFallbacks: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ exports[`Common webpack loaders babel-loader should return a config that extends
Object {
"loader": "babel-loader",
"options": Object {
"cacheDirectory": "/current/dir/.build-cache",
"cacheDirectory": "/mock/path/.build-cache",
"envName": "modern",
"extends": "/current/dir/.babelrc",
"extends": "/mock/path/.babelrc",
},
}
`;
Expand All @@ -15,149 +15,9 @@ exports[`Common webpack loaders babel-loader should return a config with the exp
Object {
"loader": "babel-loader",
"options": Object {
"cacheDirectory": "/current/dir/.build-cache",
"cacheDirectory": "/mock/path/.build-cache",
"envName": "legacy",
"extends": "/current/dir/.babelrc",
},
}
`;

exports[`Common webpack loaders css-loader should still return a good localIdentName when not given a chunk name 1`] = `
Object {
"loader": "css-loader",
"options": Object {
"importLoaders": 2,
"modules": Object {
"getLocalIdent": [Function],
"localIdentName": "[name]__[local]___[hash:base64:5]",
},
},
}
`;

exports[`Common webpack loaders css-loader should still return localName from getLocalIdent if resourcePath includes node_modules 1`] = `
Object {
"loader": "css-loader",
"options": Object {
"importLoaders": 2,
"modules": Object {
"getLocalIdent": [Function],
"localIdentName": "[name]__[local]___[hash:base64:5]",
},
},
}
`;

exports[`Common webpack loaders css-loader should still return null from getLocalIdent if resourcePath does not include node_modules 1`] = `
Object {
"loader": "css-loader",
"options": Object {
"importLoaders": 2,
"modules": Object {
"getLocalIdent": [Function],
"localIdentName": "[name]__[local]___[hash:base64:5]",
},
},
}
`;

exports[`Common webpack loaders css-loader should use the chunk name in the localIdentName when given 1`] = `
Object {
"loader": "css-loader",
"options": Object {
"importLoaders": 2,
"modules": Object {
"getLocalIdent": [Function],
"localIdentName": "my-chunk__[name]__[local]___[hash:base64:5]",
},
},
}
`;

exports[`Common webpack loaders css-loader should use the css-loader 1`] = `
Object {
"loader": "css-loader",
"options": Object {
"importLoaders": 2,
"modules": Object {
"getLocalIdent": [Function],
"localIdentName": "[name]__[local]___[hash:base64:5]",
},
},
}
`;

exports[`Common webpack loaders purgecss-loader should include any additional configured paths 1`] = `
Array [
Object {
"loader": "@americanexpress/purgecss-loader",
"options": Object {
"blocklist": Array [],
"extractors": Array [],
"fontFace": false,
"keyframes": false,
"paths": Array [
"/current/dir/src/**/*.{js,jsx,ts,tsx}",
"foo",
"bar",
],
"safelist": Object {
"deep": Array [
/:global\\$/,
],
"greedy": Array [],
"keyframes": false,
"standard": Array [],
"variables": false,
},
"variables": false,
},
},
]
`;

exports[`Common webpack loaders purgecss-loader should return a config that accounts for the src dir 1`] = `
Array [
Object {
"loader": "@americanexpress/purgecss-loader",
"options": Object {
"blocklist": Array [],
"extractors": Array [],
"fontFace": false,
"keyframes": false,
"paths": Array [
"/current/dir/src/**/*.{js,jsx,ts,tsx}",
],
"safelist": Object {
"deep": Array [
/:global\\$/,
],
"greedy": Array [],
"keyframes": false,
"standard": Array [],
"variables": false,
},
"variables": false,
},
},
]
`;

exports[`Common webpack loaders sass-loader should return a config using dart sass 1`] = `
Object {
"loader": "sass-loader",
"options": Object {
"implementation": [Function],
"sassOptions": Object {
"includePaths": Array [
"/current/dir/src",
"/current/dir/node_modules",
],
"loadPaths": Array [
"/current/dir/src",
"/current/dir/node_modules",
],
},
"extends": "/mock/path/.babelrc",
},
}
`;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`index-style-loader should add an ssrStyles export with a uuid placeholder 1`] = `
exports[`index-server-ssr-styles-placeholder-loader should add an ssrStyles export with a uuid placeholder 1`] = `
"
export default SomeModuleName;
;SomeModuleName.ssrStyles = '2574b873-59dc-4da6-8692-d767a2484dac'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ const {
} = require('../../../webpack/loaders/common');

jest.mock('sass', () => () => 0);

jest.spyOn(process, 'cwd');

describe('Common webpack loaders', () => {
beforeEach(() => {
process.cwd.mockImplementation(() => '/mock/path/');
});
describe('babel-loader', () => {
it('should return a config that extends the project\'s babelrc', () => {
expect(babelLoader('modern')).toMatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import indexServerSsrStylesPlaceholderLoader from '../../../webpack/loaders/index-server-ssr-styles-placeholder-loader';

describe('index-style-loader', () => {
describe('index-server-ssr-styles-placeholder-loader', () => {
it('should add an ssrStyles export with a uuid placeholder', () => {
expect(indexServerSsrStylesPlaceholderLoader('export default SomeModuleName;')).toMatchSnapshot();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2019 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations
* under the License.
*/

import { loadStyles } from '@americanexpress/one-app-dev-bundler';
import { BUNDLE_TYPES } from '@americanexpress/one-app-dev-bundler/esbuild/constants/enums.js';
import unboundStylesLoader from '../../../webpack/loaders/styles-loader';

jest.mock('@americanexpress/one-app-dev-bundler', () => ({
loadStyles: jest.fn(() => 'let mockJsContent = "helloMockContent"'),
}));

describe('styles-loader', () => {
let mockGetOptions;
let stylesLoader;
beforeEach(() => {
jest.clearAllMocks();
mockGetOptions = jest.fn(() => ({
cssModulesOptions: {},
bundleType: BUNDLE_TYPES.BROWSER,
}));

stylesLoader = unboundStylesLoader.bind({
resourcePath: 'style/path/mock.scss',
getOptions: mockGetOptions,
});
});
it('should call the loadStyles util with the correct params, for non-node_modules', () => {
expect(stylesLoader()).toBe('let mockJsContent = "helloMockContent"');
expect(loadStyles).toHaveBeenCalledTimes(1);
expect(loadStyles).toHaveBeenCalledWith({ bundleType: BUNDLE_TYPES.BROWSER, cssModulesOptions: { generateScopedName: '[local]_[hash:base64:5]' }, path: 'style/path/mock.scss' });
});
it('should call the loadStyles util with the correct params, for node_modules', () => {
stylesLoader = unboundStylesLoader.bind({
resourcePath: 'node_modules/style/path/mock.scss',
getOptions: mockGetOptions,
});

expect(stylesLoader()).toBe('let mockJsContent = "helloMockContent"');

expect(loadStyles).toHaveBeenCalledTimes(1);
expect(loadStyles).toHaveBeenCalledWith({ bundleType: BUNDLE_TYPES.BROWSER, cssModulesOptions: { generateScopedName: '[local]' }, path: 'node_modules/style/path/mock.scss' });
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ServerSsrStylesInjectorPlugin The registered function should inject the aggregated styles into the placeholder for .node.js files 1`] = `
"
// replaced
ssrStyles = {
aggregatedStyles: [{\\"css\\":\\"* {color: red}\\",\\"digest\\":\\"digest1Mock\\"},{\\"css\\":\\"* {color: green}\\",\\"digest\\":\\"digest2Mock\\"}],
getFullSheet: function getFullSheet() {
return this.aggregatedStyles.reduce((acc, { css }) => acc + css, '');
},
};
=====
// replaced
ssrStyles = {
aggregatedStyles: [{\\"css\\":\\"* {color: red}\\",\\"digest\\":\\"digest1Mock\\"},{\\"css\\":\\"* {color: green}\\",\\"digest\\":\\"digest2Mock\\"}],
getFullSheet: function getFullSheet() {
return this.aggregatedStyles.reduce((acc, { css }) => acc + css, '');
},
};
=====
// not replaced
ssrStyles = 2574b873-59dc-4da6-8692-d767a2484dac
"
`;
Loading

0 comments on commit 23fa890

Please sign in to comment.