Skip to content

Commit

Permalink
chore(mocks): update mockConfig, mockValidatedConfig to accept any pr…
Browse files Browse the repository at this point in the history
…operty (#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
  • Loading branch information
rwaskiewicz authored Jul 22, 2022
1 parent 47c4ccb commit 8e03f45
Show file tree
Hide file tree
Showing 16 changed files with 166 additions and 187 deletions.
16 changes: 10 additions & 6 deletions src/cli/telemetry/test/telemetry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ describe('telemetryBuildFinishedAction', () => {

beforeEach(() => {
sys = createSystem();
config = mockValidatedConfig(sys);
config.outputTargets = [];
config.flags.args = [];
config = mockValidatedConfig({
flags: createConfigFlags({ task: 'build' }),
outputTargets: [],
sys,
});
});

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

beforeEach(() => {
sys = createSystem();
config = mockValidatedConfig(sys);
config.outputTargets = [];
config.flags.args = [];
config = mockValidatedConfig({
flags: createConfigFlags({ task: 'build' }),
outputTargets: [],
sys,
});
});

it('issues a network request when no async function is passed', async () => {
Expand Down
16 changes: 9 additions & 7 deletions src/cli/test/run.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import type * as d from '../../declarations';
import * as coreCompiler from '@stencil/core/compiler';
import { mockCompilerSystem, mockConfig, mockLogger as createMockLogger } from '@stencil/core/testing';
import {
mockCompilerSystem,
mockConfig,
mockLogger as createMockLogger,
mockValidatedConfig,
} from '@stencil/core/testing';
import * as ParseFlags from '../parse-flags';
import { run, runTask } from '../run';
import * as BuildTask from '../task-build';
Expand Down Expand Up @@ -132,11 +137,9 @@ describe('run', () => {
sys = mockCompilerSystem();
sys.exit = jest.fn();

unvalidatedConfig = mockConfig(sys);
unvalidatedConfig.outputTargets = null;
unvalidatedConfig = mockConfig({ outputTargets: null, sys });

validatedConfig = mockConfig(sys);
validatedConfig.outputTargets = [];
validatedConfig = mockValidatedConfig({ sys });

taskBuildSpy = jest.spyOn(BuildTask, 'taskBuild');
taskBuildSpy.mockResolvedValue();
Expand Down Expand Up @@ -256,8 +259,7 @@ describe('run', () => {
});

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

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

Expand Down
11 changes: 7 additions & 4 deletions src/cli/test/task-generate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as utils from '../../utils/validation';

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

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

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

const setup = async () => {
const sys = mockCompilerSystem();
const config: d.ValidatedConfig = mockValidatedConfig(sys);
config.configPath = '/testing-path';
config.srcDir = '/src';
const config: d.ValidatedConfig = mockValidatedConfig({
configPath: '/testing-path',
flags: createConfigFlags({ task: 'generate' }),
srcDir: '/src',
sys,
});

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

Expand Down
17 changes: 10 additions & 7 deletions src/compiler/output-targets/test/custom-elements-types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ import { join, relative } from 'path';

const setup = () => {
const sys = mockCompilerSystem();
const config: d.ValidatedConfig = mockValidatedConfig(sys);
const config: d.ValidatedConfig = mockValidatedConfig({
configPath: '/testing-path',
buildAppCore: true,
buildEs5: true,
namespace: 'TestApp',
outputTargets: [{ type: DIST_CUSTOM_ELEMENTS, dir: 'my-best-dir' }],
srcDir: '/src',
sys,
});
const compilerCtx = mockCompilerCtx(config);
const buildCtx = mockBuildCtx(config, compilerCtx);

const root = config.rootDir;
config.configPath = '/testing-path';
config.srcDir = '/src';
config.buildAppCore = true;
config.rootDir = path.join(root, 'User', 'testing', '/');
config.namespace = 'TestApp';
config.buildEs5 = true;
config.globalScript = path.join(root, 'User', 'testing', 'src', 'global.ts');
config.outputTargets = [{ type: DIST_CUSTOM_ELEMENTS, dir: 'my-best-dir' }];

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,21 @@ import { DIST_CUSTOM_ELEMENTS, DIST_CUSTOM_ELEMENTS_BUNDLE } from '../output-uti

const setup = () => {
const sys = mockCompilerSystem();
const config: d.ValidatedConfig = mockValidatedConfig(sys);
const config: d.ValidatedConfig = mockValidatedConfig({
buildAppCore: true,
buildEs5: true,
configPath: '/testing-path',
namespace: 'TestApp',
outputTargets: [{ type: DIST_CUSTOM_ELEMENTS }],
srcDir: '/src',
sys,
});
const compilerCtx = mockCompilerCtx(config);
const buildCtx = mockBuildCtx(config, compilerCtx);

const root = config.rootDir;
config.configPath = '/testing-path';
config.srcDir = '/src';
config.buildAppCore = true;
config.rootDir = path.join(root, 'User', 'testing', '/');
config.namespace = 'TestApp';
config.buildEs5 = true;
config.globalScript = path.join(root, 'User', 'testing', 'src', 'global.ts');
config.outputTargets = [{ type: DIST_CUSTOM_ELEMENTS }];

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

Expand Down
15 changes: 8 additions & 7 deletions src/compiler/output-targets/test/output-targets-dist.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ describe.skip('outputTarget, dist', () => {
const root = path.resolve('/');

it('default dist files', async () => {
config = mockConfig();
config.buildAppCore = true;
config.rootDir = path.join(root, 'User', 'testing', '/');
config.namespace = 'TestApp';
config.buildEs5 = true;
config.globalScript = path.join(root, 'User', 'testing', 'src', 'global.ts');
config.outputTargets = [{ type: 'dist' }];
config = mockConfig({
buildAppCore: true,
buildEs5: true,
globalScript: path.join(root, 'User', 'testing', 'src', 'global.ts'),
namespace: 'TestApp',
outputTargets: [{ type: 'dist' }],
rootDir: path.join(root, 'User', 'testing', '/'),
});

compiler = new Compiler(config);

Expand Down
47 changes: 24 additions & 23 deletions src/compiler/output-targets/test/output-targets-www-dist.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,30 @@ describe.skip('outputTarget, www / dist / docs', () => {
const root = path.resolve('/');

it('dist, www and readme files w/ custom paths', async () => {
config = mockConfig();
config.flags.docs = true;
config.buildAppCore = true;
config.rootDir = path.join(root, 'User', 'testing', '/');
config.namespace = 'TestApp';
config.outputTargets = [
{
type: 'www',
dir: 'custom-www',
buildDir: 'www-build',
indexHtml: 'custom-index.htm',
} as any as d.OutputTargetDist,
{
type: 'dist',
dir: 'custom-dist',
buildDir: 'dist-build',
collectionDir: 'dist-collection',
typesDir: 'custom-types',
},
{
type: 'docs',
} as d.OutputTargetDocsReadme,
];
config = mockConfig({
buildAppCore: true,
flags: { docs: true },
namespace: 'TestApp',
outputTargets: [
{
type: 'www',
dir: 'custom-www',
buildDir: 'www-build',
indexHtml: 'custom-index.htm',
} as any as d.OutputTargetDist,
{
type: 'dist',
dir: 'custom-dist',
buildDir: 'dist-build',
collectionDir: 'dist-collection',
typesDir: 'custom-types',
},
{
type: 'docs',
} as d.OutputTargetDocsReadme,
],
rootDir: path.join(root, 'User', 'testing', '/'),
});

compiler = new Compiler(config);

Expand Down
9 changes: 5 additions & 4 deletions src/compiler/output-targets/test/output-targets-www.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ describe.skip('outputTarget, www', () => {
const root = path.resolve('/');

it('default www files', async () => {
config = mockConfig();
config.namespace = 'App';
config.buildAppCore = true;
config.rootDir = path.join(root, 'User', 'testing', '/');
config = mockConfig({
buildAppCore: true,
namespace: 'App',
rootDir: path.join(root, 'User', 'testing', '/'),
});

compiler = new Compiler(config);

Expand Down
20 changes: 10 additions & 10 deletions src/compiler/service-worker/test/service-worker-util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ describe('generateServiceWorkerUrl', () => {
let outputTarget: d.OutputTargetWww;

it('sw url w/ baseUrl', () => {
userConfig = mockConfig();
userConfig.devMode = false;
userConfig.outputTargets = [
{
type: 'www',
baseUrl: '/docs',
} as d.OutputTargetWww,
];
userConfig = mockConfig({
devMode: false,
outputTargets: [
{
type: 'www',
baseUrl: '/docs',
} as d.OutputTargetWww,
],
});
const { config } = validateConfig(userConfig, mockLoadConfigInit());
outputTarget = config.outputTargets[0] as d.OutputTargetWww;
const swUrl = generateServiceWorkerUrl(outputTarget, outputTarget.serviceWorker as d.ServiceWorkerConfig);
expect(swUrl).toBe('/docs/sw.js');
});

it('default sw url', () => {
userConfig = mockConfig();
userConfig.devMode = false;
userConfig = mockConfig({ devMode: false });
const { config } = validateConfig(userConfig, mockLoadConfigInit());
outputTarget = config.outputTargets[0] as d.OutputTargetWww;
const swUrl = generateServiceWorkerUrl(outputTarget, outputTarget.serviceWorker as d.ServiceWorkerConfig);
Expand Down
23 changes: 12 additions & 11 deletions src/compiler/service-worker/test/service-worker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ describe.skip('service worker', () => {
const root = path.resolve('/');

it('dev service worker', async () => {
config = mockConfig();
config.devMode = true;
config.outputTargets = [
{
type: 'www',
serviceWorker: {
swSrc: path.join('src', 'sw.js'),
globPatterns: ['**/*.{html,js,css,json,ico,png}'],
},
} as d.OutputTargetWww,
];
config = mockConfig({
devMode: true,
outputTargets: [
{
type: 'www',
serviceWorker: {
swSrc: path.join('src', 'sw.js'),
globPatterns: ['**/*.{html,js,css,json,ico,png}'],
},
} as d.OutputTargetWww,
],
});

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

0 comments on commit 8e03f45

Please sign in to comment.