Skip to content

Commit 8e03f45

Browse files
authored
chore(mocks): update mockConfig, mockValidatedConfig to accept any property (#3485)
this commit adds a new parameter to `mockValidatedConfig`, `overrides`. the argument is optional, and defaults to an empty object literal. it is spread over the returned object to override and defaults put in place by this method. the `sys` argument is also removed, as it now can be safely derived from the `overrides` argument (and still falls back to a new `TestingSystem` if `overrides.sys` is falsy this commit adds an `overrides` parameter to `mockConfig`. `overrides` is a partial instance of `UnvalidatedConfig`, which defaults to an empty object literal. its contents are spread over the returned object, overriding the values put in place by default. it also removes the `sys` arg. ' this value is now derived from the provided `overrides`, falling back to a `TestingSystem` otherwise. the properties on `mockConfig` are sorted in this commit, to make finding them (by a human) easier. it is assumed that this is the "best" ordering for them, as no other grouping seems "obvious" at this time STENCIL-486: Update mockConfig, mockValidatedConfig to accept any property
1 parent 47c4ccb commit 8e03f45

16 files changed

+166
-187
lines changed

src/cli/telemetry/test/telemetry.spec.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ describe('telemetryBuildFinishedAction', () => {
1414

1515
beforeEach(() => {
1616
sys = createSystem();
17-
config = mockValidatedConfig(sys);
18-
config.outputTargets = [];
19-
config.flags.args = [];
17+
config = mockValidatedConfig({
18+
flags: createConfigFlags({ task: 'build' }),
19+
outputTargets: [],
20+
sys,
21+
});
2022
});
2123

2224
it('issues a network request when complete', async () => {
@@ -45,9 +47,11 @@ describe('telemetryAction', () => {
4547

4648
beforeEach(() => {
4749
sys = createSystem();
48-
config = mockValidatedConfig(sys);
49-
config.outputTargets = [];
50-
config.flags.args = [];
50+
config = mockValidatedConfig({
51+
flags: createConfigFlags({ task: 'build' }),
52+
outputTargets: [],
53+
sys,
54+
});
5155
});
5256

5357
it('issues a network request when no async function is passed', async () => {

src/cli/test/run.spec.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import type * as d from '../../declarations';
22
import * as coreCompiler from '@stencil/core/compiler';
3-
import { mockCompilerSystem, mockConfig, mockLogger as createMockLogger } from '@stencil/core/testing';
3+
import {
4+
mockCompilerSystem,
5+
mockConfig,
6+
mockLogger as createMockLogger,
7+
mockValidatedConfig,
8+
} from '@stencil/core/testing';
49
import * as ParseFlags from '../parse-flags';
510
import { run, runTask } from '../run';
611
import * as BuildTask from '../task-build';
@@ -132,11 +137,9 @@ describe('run', () => {
132137
sys = mockCompilerSystem();
133138
sys.exit = jest.fn();
134139

135-
unvalidatedConfig = mockConfig(sys);
136-
unvalidatedConfig.outputTargets = null;
140+
unvalidatedConfig = mockConfig({ outputTargets: null, sys });
137141

138-
validatedConfig = mockConfig(sys);
139-
validatedConfig.outputTargets = [];
142+
validatedConfig = mockValidatedConfig({ sys });
140143

141144
taskBuildSpy = jest.spyOn(BuildTask, 'taskBuild');
142145
taskBuildSpy.mockResolvedValue();
@@ -256,8 +259,7 @@ describe('run', () => {
256259
});
257260

258261
it('defaults to the provided task if no flags exist on the provided config', async () => {
259-
unvalidatedConfig = mockConfig(sys);
260-
unvalidatedConfig.flags = undefined;
262+
unvalidatedConfig = mockConfig({ flags: undefined, sys });
261263

262264
await runTask(coreCompiler, unvalidatedConfig, 'help', sys);
263265

src/cli/test/task-generate.spec.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as utils from '../../utils/validation';
55

66
import * as coreCompiler from '@stencil/core/compiler';
77
import { CoreCompiler } from '../load-compiler';
8+
import { createConfigFlags } from '../config-flags';
89

910
const promptMock = jest.fn().mockResolvedValue('my-component');
1011

@@ -14,13 +15,15 @@ jest.mock('prompts', () => ({
1415

1516
const setup = async () => {
1617
const sys = mockCompilerSystem();
17-
const config: d.ValidatedConfig = mockValidatedConfig(sys);
18-
config.configPath = '/testing-path';
19-
config.srcDir = '/src';
18+
const config: d.ValidatedConfig = mockValidatedConfig({
19+
configPath: '/testing-path',
20+
flags: createConfigFlags({ task: 'generate' }),
21+
srcDir: '/src',
22+
sys,
23+
});
2024

2125
// set up some mocks / spies
2226
config.sys.exit = jest.fn();
23-
config.flags.unknownArgs = [];
2427
const errorSpy = jest.spyOn(config.logger, 'error');
2528
const validateTagSpy = jest.spyOn(utils, 'validateComponentTag').mockReturnValue(undefined);
2629

src/compiler/output-targets/test/custom-elements-types.spec.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,21 @@ import { join, relative } from 'path';
1515

1616
const setup = () => {
1717
const sys = mockCompilerSystem();
18-
const config: d.ValidatedConfig = mockValidatedConfig(sys);
18+
const config: d.ValidatedConfig = mockValidatedConfig({
19+
configPath: '/testing-path',
20+
buildAppCore: true,
21+
buildEs5: true,
22+
namespace: 'TestApp',
23+
outputTargets: [{ type: DIST_CUSTOM_ELEMENTS, dir: 'my-best-dir' }],
24+
srcDir: '/src',
25+
sys,
26+
});
1927
const compilerCtx = mockCompilerCtx(config);
2028
const buildCtx = mockBuildCtx(config, compilerCtx);
29+
2130
const root = config.rootDir;
22-
config.configPath = '/testing-path';
23-
config.srcDir = '/src';
24-
config.buildAppCore = true;
2531
config.rootDir = path.join(root, 'User', 'testing', '/');
26-
config.namespace = 'TestApp';
27-
config.buildEs5 = true;
2832
config.globalScript = path.join(root, 'User', 'testing', 'src', 'global.ts');
29-
config.outputTargets = [{ type: DIST_CUSTOM_ELEMENTS, dir: 'my-best-dir' }];
3033

3134
const bundleCustomElementsSpy = jest.spyOn(outputCustomElementsMod, 'bundleCustomElements');
3235

src/compiler/output-targets/test/output-targets-dist-custom-elements.spec.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,21 @@ import { DIST_CUSTOM_ELEMENTS, DIST_CUSTOM_ELEMENTS_BUNDLE } from '../output-uti
2222

2323
const setup = () => {
2424
const sys = mockCompilerSystem();
25-
const config: d.ValidatedConfig = mockValidatedConfig(sys);
25+
const config: d.ValidatedConfig = mockValidatedConfig({
26+
buildAppCore: true,
27+
buildEs5: true,
28+
configPath: '/testing-path',
29+
namespace: 'TestApp',
30+
outputTargets: [{ type: DIST_CUSTOM_ELEMENTS }],
31+
srcDir: '/src',
32+
sys,
33+
});
2634
const compilerCtx = mockCompilerCtx(config);
2735
const buildCtx = mockBuildCtx(config, compilerCtx);
36+
2837
const root = config.rootDir;
29-
config.configPath = '/testing-path';
30-
config.srcDir = '/src';
31-
config.buildAppCore = true;
3238
config.rootDir = path.join(root, 'User', 'testing', '/');
33-
config.namespace = 'TestApp';
34-
config.buildEs5 = true;
3539
config.globalScript = path.join(root, 'User', 'testing', 'src', 'global.ts');
36-
config.outputTargets = [{ type: DIST_CUSTOM_ELEMENTS }];
3740

3841
const bundleCustomElementsSpy = jest.spyOn(outputCustomElementsMod, 'bundleCustomElements');
3942

src/compiler/output-targets/test/output-targets-dist.spec.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ describe.skip('outputTarget, dist', () => {
1111
const root = path.resolve('/');
1212

1313
it('default dist files', async () => {
14-
config = mockConfig();
15-
config.buildAppCore = true;
16-
config.rootDir = path.join(root, 'User', 'testing', '/');
17-
config.namespace = 'TestApp';
18-
config.buildEs5 = true;
19-
config.globalScript = path.join(root, 'User', 'testing', 'src', 'global.ts');
20-
config.outputTargets = [{ type: 'dist' }];
14+
config = mockConfig({
15+
buildAppCore: true,
16+
buildEs5: true,
17+
globalScript: path.join(root, 'User', 'testing', 'src', 'global.ts'),
18+
namespace: 'TestApp',
19+
outputTargets: [{ type: 'dist' }],
20+
rootDir: path.join(root, 'User', 'testing', '/'),
21+
});
2122

2223
compiler = new Compiler(config);
2324

src/compiler/output-targets/test/output-targets-www-dist.spec.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,30 @@ describe.skip('outputTarget, www / dist / docs', () => {
1212
const root = path.resolve('/');
1313

1414
it('dist, www and readme files w/ custom paths', async () => {
15-
config = mockConfig();
16-
config.flags.docs = true;
17-
config.buildAppCore = true;
18-
config.rootDir = path.join(root, 'User', 'testing', '/');
19-
config.namespace = 'TestApp';
20-
config.outputTargets = [
21-
{
22-
type: 'www',
23-
dir: 'custom-www',
24-
buildDir: 'www-build',
25-
indexHtml: 'custom-index.htm',
26-
} as any as d.OutputTargetDist,
27-
{
28-
type: 'dist',
29-
dir: 'custom-dist',
30-
buildDir: 'dist-build',
31-
collectionDir: 'dist-collection',
32-
typesDir: 'custom-types',
33-
},
34-
{
35-
type: 'docs',
36-
} as d.OutputTargetDocsReadme,
37-
];
15+
config = mockConfig({
16+
buildAppCore: true,
17+
flags: { docs: true },
18+
namespace: 'TestApp',
19+
outputTargets: [
20+
{
21+
type: 'www',
22+
dir: 'custom-www',
23+
buildDir: 'www-build',
24+
indexHtml: 'custom-index.htm',
25+
} as any as d.OutputTargetDist,
26+
{
27+
type: 'dist',
28+
dir: 'custom-dist',
29+
buildDir: 'dist-build',
30+
collectionDir: 'dist-collection',
31+
typesDir: 'custom-types',
32+
},
33+
{
34+
type: 'docs',
35+
} as d.OutputTargetDocsReadme,
36+
],
37+
rootDir: path.join(root, 'User', 'testing', '/'),
38+
});
3839

3940
compiler = new Compiler(config);
4041

src/compiler/output-targets/test/output-targets-www.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ describe.skip('outputTarget, www', () => {
1111
const root = path.resolve('/');
1212

1313
it('default www files', async () => {
14-
config = mockConfig();
15-
config.namespace = 'App';
16-
config.buildAppCore = true;
17-
config.rootDir = path.join(root, 'User', 'testing', '/');
14+
config = mockConfig({
15+
buildAppCore: true,
16+
namespace: 'App',
17+
rootDir: path.join(root, 'User', 'testing', '/'),
18+
});
1819

1920
compiler = new Compiler(config);
2021

src/compiler/service-worker/test/service-worker-util.spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ describe('generateServiceWorkerUrl', () => {
88
let outputTarget: d.OutputTargetWww;
99

1010
it('sw url w/ baseUrl', () => {
11-
userConfig = mockConfig();
12-
userConfig.devMode = false;
13-
userConfig.outputTargets = [
14-
{
15-
type: 'www',
16-
baseUrl: '/docs',
17-
} as d.OutputTargetWww,
18-
];
11+
userConfig = mockConfig({
12+
devMode: false,
13+
outputTargets: [
14+
{
15+
type: 'www',
16+
baseUrl: '/docs',
17+
} as d.OutputTargetWww,
18+
],
19+
});
1920
const { config } = validateConfig(userConfig, mockLoadConfigInit());
2021
outputTarget = config.outputTargets[0] as d.OutputTargetWww;
2122
const swUrl = generateServiceWorkerUrl(outputTarget, outputTarget.serviceWorker as d.ServiceWorkerConfig);
2223
expect(swUrl).toBe('/docs/sw.js');
2324
});
2425

2526
it('default sw url', () => {
26-
userConfig = mockConfig();
27-
userConfig.devMode = false;
27+
userConfig = mockConfig({ devMode: false });
2828
const { config } = validateConfig(userConfig, mockLoadConfigInit());
2929
outputTarget = config.outputTargets[0] as d.OutputTargetWww;
3030
const swUrl = generateServiceWorkerUrl(outputTarget, outputTarget.serviceWorker as d.ServiceWorkerConfig);

src/compiler/service-worker/test/service-worker.spec.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ describe.skip('service worker', () => {
1313
const root = path.resolve('/');
1414

1515
it('dev service worker', async () => {
16-
config = mockConfig();
17-
config.devMode = true;
18-
config.outputTargets = [
19-
{
20-
type: 'www',
21-
serviceWorker: {
22-
swSrc: path.join('src', 'sw.js'),
23-
globPatterns: ['**/*.{html,js,css,json,ico,png}'],
24-
},
25-
} as d.OutputTargetWww,
26-
];
16+
config = mockConfig({
17+
devMode: true,
18+
outputTargets: [
19+
{
20+
type: 'www',
21+
serviceWorker: {
22+
swSrc: path.join('src', 'sw.js'),
23+
globPatterns: ['**/*.{html,js,css,json,ico,png}'],
24+
},
25+
} as d.OutputTargetWww,
26+
],
27+
});
2728

2829
compiler = new Compiler(config);
2930
await compiler.fs.writeFile(path.join(root, 'www', 'script.js'), `/**/`);

0 commit comments

Comments
 (0)