From 03667c37d95a90751f3a4a2b642784934d1a01f2 Mon Sep 17 00:00:00 2001 From: Ryan Waskiewicz Date: Thu, 21 Jul 2022 17:18:03 -0400 Subject: [PATCH 1/9] alphabetize mockConfig properties 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 --- src/testing/mocks.ts | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/testing/mocks.ts b/src/testing/mocks.ts index fda5a4a9b10..ea3da0c638a 100644 --- a/src/testing/mocks.ts +++ b/src/testing/mocks.ts @@ -57,35 +57,34 @@ export function mockConfig(sys?: CompilerSystem): UnvalidatedConfig { return { _isTesting: true, - - namespace: 'Testing', - rootDir: rootDir, - globalScript: null, - devMode: true, - enableCache: false, buildAppCore: false, buildDist: true, - flags: createConfigFlags(), - bundles: null, - outputTargets: null, buildEs5: false, + bundles: null, + devMode: true, + enableCache: false, + extras: {}, + flags: createConfigFlags(), + globalScript: null, hashFileNames: false, logger: new TestingLogger(), maxConcurrentWorkers: 0, minifyCss: false, minifyJs: false, - sys, - testing: null, - validateTypes: false, - extras: {}, + namespace: 'Testing', nodeResolve: { customResolveOptions: {}, }, - sourceMap: true, + outputTargets: null, rollupPlugins: { before: [], after: [], }, + rootDir, + sourceMap: true, + sys, + testing: null, + validateTypes: false, }; } From d4c730b7c35204911282084b023d37824cb304df Mon Sep 17 00:00:00 2001 From: Ryan Waskiewicz Date: Thu, 21 Jul 2022 17:23:00 -0400 Subject: [PATCH 2/9] add overrides parameter to mockValidatedConfig 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. --- src/testing/mocks.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/testing/mocks.ts b/src/testing/mocks.ts index ea3da0c638a..f10ca11d6d2 100644 --- a/src/testing/mocks.ts +++ b/src/testing/mocks.ts @@ -26,9 +26,11 @@ import { createConfigFlags } from '../cli/config-flags'; * Creates a mock instance of an internal, validated Stencil configuration object * @param sys an optional compiler system to associate with the config. If one is not provided, one will be created for * the caller + * @param overrides a partial implementation of `ValidatedConfig`. Any provided fields will override the defaults + * provided by this function. * @returns the mock Stencil configuration */ -export function mockValidatedConfig(sys?: CompilerSystem): ValidatedConfig { +export function mockValidatedConfig(sys?: CompilerSystem, overrides: Partial = {}): ValidatedConfig { const baseConfig = mockConfig(sys); return { @@ -36,6 +38,7 @@ export function mockValidatedConfig(sys?: CompilerSystem): ValidatedConfig { flags: createConfigFlags(), logger: mockLogger(), outputTargets: baseConfig.outputTargets ?? [], + ...overrides, }; } From a26d58c3d09fcaaf3c87c10304e05c0382adfd58 Mon Sep 17 00:00:00 2001 From: Ryan Waskiewicz Date: Thu, 21 Jul 2022 17:28:16 -0400 Subject: [PATCH 3/9] rm `sys` from `mockValidatedConfig` 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 --- src/cli/telemetry/test/telemetry.spec.ts | 4 ++-- src/cli/test/task-generate.spec.ts | 2 +- .../output-targets/test/custom-elements-types.spec.ts | 2 +- .../test/output-targets-dist-custom-elements.spec.ts | 2 +- src/testing/mocks.ts | 5 ++--- 5 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/cli/telemetry/test/telemetry.spec.ts b/src/cli/telemetry/test/telemetry.spec.ts index 8af188bdc94..eed13efe5db 100644 --- a/src/cli/telemetry/test/telemetry.spec.ts +++ b/src/cli/telemetry/test/telemetry.spec.ts @@ -14,7 +14,7 @@ describe('telemetryBuildFinishedAction', () => { beforeEach(() => { sys = createSystem(); - config = mockValidatedConfig(sys); + config = mockValidatedConfig({ sys }); config.outputTargets = []; config.flags.args = []; }); @@ -45,7 +45,7 @@ describe('telemetryAction', () => { beforeEach(() => { sys = createSystem(); - config = mockValidatedConfig(sys); + config = mockValidatedConfig({ sys }); config.outputTargets = []; config.flags.args = []; }); diff --git a/src/cli/test/task-generate.spec.ts b/src/cli/test/task-generate.spec.ts index b21002ed0af..c0626e15b02 100644 --- a/src/cli/test/task-generate.spec.ts +++ b/src/cli/test/task-generate.spec.ts @@ -14,7 +14,7 @@ jest.mock('prompts', () => ({ const setup = async () => { const sys = mockCompilerSystem(); - const config: d.ValidatedConfig = mockValidatedConfig(sys); + const config: d.ValidatedConfig = mockValidatedConfig({ sys }); config.configPath = '/testing-path'; config.srcDir = '/src'; diff --git a/src/compiler/output-targets/test/custom-elements-types.spec.ts b/src/compiler/output-targets/test/custom-elements-types.spec.ts index 2cf3a1fb951..56b6aa628c6 100644 --- a/src/compiler/output-targets/test/custom-elements-types.spec.ts +++ b/src/compiler/output-targets/test/custom-elements-types.spec.ts @@ -15,7 +15,7 @@ import { join, relative } from 'path'; const setup = () => { const sys = mockCompilerSystem(); - const config: d.ValidatedConfig = mockValidatedConfig(sys); + const config: d.ValidatedConfig = mockValidatedConfig({ sys }); const compilerCtx = mockCompilerCtx(config); const buildCtx = mockBuildCtx(config, compilerCtx); const root = config.rootDir; diff --git a/src/compiler/output-targets/test/output-targets-dist-custom-elements.spec.ts b/src/compiler/output-targets/test/output-targets-dist-custom-elements.spec.ts index 5e0a0881043..1857721fc10 100644 --- a/src/compiler/output-targets/test/output-targets-dist-custom-elements.spec.ts +++ b/src/compiler/output-targets/test/output-targets-dist-custom-elements.spec.ts @@ -22,7 +22,7 @@ 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({ sys }); const compilerCtx = mockCompilerCtx(config); const buildCtx = mockBuildCtx(config, compilerCtx); const root = config.rootDir; diff --git a/src/testing/mocks.ts b/src/testing/mocks.ts index f10ca11d6d2..5acec7f18bf 100644 --- a/src/testing/mocks.ts +++ b/src/testing/mocks.ts @@ -24,14 +24,13 @@ import { createConfigFlags } from '../cli/config-flags'; // TODO(STENCIL-486): Update `mockValidatedConfig` to accept any property found on `ValidatedConfig` /** * Creates a mock instance of an internal, validated Stencil configuration object - * @param sys an optional compiler system to associate with the config. If one is not provided, one will be created for * the caller * @param overrides a partial implementation of `ValidatedConfig`. Any provided fields will override the defaults * provided by this function. * @returns the mock Stencil configuration */ -export function mockValidatedConfig(sys?: CompilerSystem, overrides: Partial = {}): ValidatedConfig { - const baseConfig = mockConfig(sys); +export function mockValidatedConfig(overrides: Partial = {}): ValidatedConfig { + const baseConfig = mockConfig(overrides.sys); return { ...baseConfig, From c111e28cf2f2bba4ea47e9ee9ba422bc40e5675b Mon Sep 17 00:00:00 2001 From: Ryan Waskiewicz Date: Thu, 21 Jul 2022 17:31:39 -0400 Subject: [PATCH 4/9] add `overrides` parameter to `mockConfig` 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 --- src/testing/mocks.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/testing/mocks.ts b/src/testing/mocks.ts index 5acec7f18bf..693e24ce3c9 100644 --- a/src/testing/mocks.ts +++ b/src/testing/mocks.ts @@ -47,9 +47,11 @@ export function mockValidatedConfig(overrides: Partial = {}): V * types/validity of its data. * @param sys an optional compiler system to associate with the config. If one is not provided, one will be created for * the caller + * @param overrides a partial implementation of `UnvalidatedConfig`. Any provided fields will override the defaults + * provided by this function. * @returns the mock Stencil configuration */ -export function mockConfig(sys?: CompilerSystem): UnvalidatedConfig { +export function mockConfig(sys?: CompilerSystem, overrides: Partial = {}): UnvalidatedConfig { const rootDir = path.resolve('/'); if (!sys) { @@ -87,6 +89,7 @@ export function mockConfig(sys?: CompilerSystem): UnvalidatedConfig { sys, testing: null, validateTypes: false, + ...overrides, }; } From affeeebfd374e966adba10bbc30027a357c0c216 Mon Sep 17 00:00:00 2001 From: Ryan Waskiewicz Date: Thu, 21 Jul 2022 17:38:47 -0400 Subject: [PATCH 5/9] remove `sys` from `mockConfig` remove the `sys` arg from `mockConfig`. this value is now derived from the provided `overrides`, falling back to a `TestingSystem` otherwise. --- src/cli/test/run.spec.ts | 14 +++++++++----- src/testing/mocks.ts | 8 +++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/cli/test/run.spec.ts b/src/cli/test/run.spec.ts index 0655b84ca81..93979fc147e 100644 --- a/src/cli/test/run.spec.ts +++ b/src/cli/test/run.spec.ts @@ -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'; @@ -132,11 +137,10 @@ describe('run', () => { sys = mockCompilerSystem(); sys.exit = jest.fn(); - unvalidatedConfig = mockConfig(sys); + unvalidatedConfig = mockConfig({ sys }); unvalidatedConfig.outputTargets = null; - validatedConfig = mockConfig(sys); - validatedConfig.outputTargets = []; + validatedConfig = mockValidatedConfig({ sys }); taskBuildSpy = jest.spyOn(BuildTask, 'taskBuild'); taskBuildSpy.mockResolvedValue(); @@ -256,7 +260,7 @@ describe('run', () => { }); it('defaults to the provided task if no flags exist on the provided config', async () => { - unvalidatedConfig = mockConfig(sys); + unvalidatedConfig = mockConfig({ sys }); unvalidatedConfig.flags = undefined; await runTask(coreCompiler, unvalidatedConfig, 'help', sys); diff --git a/src/testing/mocks.ts b/src/testing/mocks.ts index 693e24ce3c9..691b4b51bb9 100644 --- a/src/testing/mocks.ts +++ b/src/testing/mocks.ts @@ -2,7 +2,6 @@ import type { BuildCtx, Cache, CompilerCtx, - CompilerSystem, Config, LoadConfigInit, ValidatedConfig, @@ -30,7 +29,7 @@ import { createConfigFlags } from '../cli/config-flags'; * @returns the mock Stencil configuration */ export function mockValidatedConfig(overrides: Partial = {}): ValidatedConfig { - const baseConfig = mockConfig(overrides.sys); + const baseConfig = mockConfig(overrides); return { ...baseConfig, @@ -45,15 +44,14 @@ export function mockValidatedConfig(overrides: Partial = {}): V /** * Creates a mock instance of a Stencil configuration entity. The mocked configuration has no guarantees around the * types/validity of its data. - * @param sys an optional compiler system to associate with the config. If one is not provided, one will be created for - * the caller * @param overrides a partial implementation of `UnvalidatedConfig`. Any provided fields will override the defaults * provided by this function. * @returns the mock Stencil configuration */ -export function mockConfig(sys?: CompilerSystem, overrides: Partial = {}): UnvalidatedConfig { +export function mockConfig(overrides: Partial = {}): UnvalidatedConfig { const rootDir = path.resolve('/'); + let { sys } = overrides; if (!sys) { sys = createTestingSystem(); } From c07d2e4a1cf30363a647a03ed5583593429ece03 Mon Sep 17 00:00:00 2001 From: Ryan Waskiewicz Date: Thu, 21 Jul 2022 17:39:03 -0400 Subject: [PATCH 6/9] chore(): rm todo --- src/testing/mocks.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/testing/mocks.ts b/src/testing/mocks.ts index 691b4b51bb9..b40adfb6278 100644 --- a/src/testing/mocks.ts +++ b/src/testing/mocks.ts @@ -20,7 +20,6 @@ import { noop } from '@utils'; import { buildEvents } from '../compiler/events'; import { createConfigFlags } from '../cli/config-flags'; -// TODO(STENCIL-486): Update `mockValidatedConfig` to accept any property found on `ValidatedConfig` /** * Creates a mock instance of an internal, validated Stencil configuration object * the caller @@ -40,7 +39,6 @@ export function mockValidatedConfig(overrides: Partial = {}): V }; } -// TODO(STENCIL-486): Update `mockConfig` to accept any property found on `UnvalidatedConfig` /** * Creates a mock instance of a Stencil configuration entity. The mocked configuration has no guarantees around the * types/validity of its data. From 96cf150bf2f2d1ab4b9a4c85a853853243cb0a08 Mon Sep 17 00:00:00 2001 From: Ryan Waskiewicz Date: Thu, 21 Jul 2022 18:11:34 -0400 Subject: [PATCH 7/9] update mockValidatedConfig callsites update callsites to mockValidatedConfig to use new overrides fields --- src/cli/telemetry/test/telemetry.spec.ts | 16 ++++-- src/cli/test/task-generate.spec.ts | 10 ++-- .../test/custom-elements-types.spec.ts | 17 +++--- ...utput-targets-dist-custom-elements.spec.ts | 17 +++--- src/testing/jest/test/jest-config.spec.ts | 55 +++++++------------ 5 files changed, 56 insertions(+), 59 deletions(-) diff --git a/src/cli/telemetry/test/telemetry.spec.ts b/src/cli/telemetry/test/telemetry.spec.ts index eed13efe5db..6e9858e435e 100644 --- a/src/cli/telemetry/test/telemetry.spec.ts +++ b/src/cli/telemetry/test/telemetry.spec.ts @@ -14,9 +14,11 @@ describe('telemetryBuildFinishedAction', () => { beforeEach(() => { sys = createSystem(); - config = mockValidatedConfig({ sys }); - config.outputTargets = []; - config.flags.args = []; + config = mockValidatedConfig({ + flags: { args: [], knownArgs: [], task: 'build', unknownArgs: [] }, + outputTargets: [], + sys, + }); }); it('issues a network request when complete', async () => { @@ -45,9 +47,11 @@ describe('telemetryAction', () => { beforeEach(() => { sys = createSystem(); - config = mockValidatedConfig({ sys }); - config.outputTargets = []; - config.flags.args = []; + config = mockValidatedConfig({ + flags: { args: [], knownArgs: [], task: 'build', unknownArgs: [] }, + outputTargets: [], + sys, + }); }); it('issues a network request when no async function is passed', async () => { diff --git a/src/cli/test/task-generate.spec.ts b/src/cli/test/task-generate.spec.ts index c0626e15b02..6823ccdebaa 100644 --- a/src/cli/test/task-generate.spec.ts +++ b/src/cli/test/task-generate.spec.ts @@ -14,13 +14,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: { args: [], knownArgs: [], task: 'generate', unknownArgs: [] }, + 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); diff --git a/src/compiler/output-targets/test/custom-elements-types.spec.ts b/src/compiler/output-targets/test/custom-elements-types.spec.ts index 56b6aa628c6..b6c15c7cd71 100644 --- a/src/compiler/output-targets/test/custom-elements-types.spec.ts +++ b/src/compiler/output-targets/test/custom-elements-types.spec.ts @@ -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'); diff --git a/src/compiler/output-targets/test/output-targets-dist-custom-elements.spec.ts b/src/compiler/output-targets/test/output-targets-dist-custom-elements.spec.ts index 1857721fc10..0f4f8353b19 100644 --- a/src/compiler/output-targets/test/output-targets-dist-custom-elements.spec.ts +++ b/src/compiler/output-targets/test/output-targets-dist-custom-elements.spec.ts @@ -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'); diff --git a/src/testing/jest/test/jest-config.spec.ts b/src/testing/jest/test/jest-config.spec.ts index a9bfbe9715b..ee6e083a8eb 100644 --- a/src/testing/jest/test/jest-config.spec.ts +++ b/src/testing/jest/test/jest-config.spec.ts @@ -7,9 +7,8 @@ import path from 'path'; describe('jest-config', () => { it('pass --maxWorkers=2 arg when --max-workers=2', () => { const args = ['test', '--ci', '--max-workers=2']; - const config = mockValidatedConfig(); + const config = mockValidatedConfig({ testing: {} }); config.flags = parseFlags(args, config.sys); - config.testing = {}; expect(config.flags.args).toEqual(['--ci', '--max-workers=2']); expect(config.flags.unknownArgs).toEqual([]); @@ -21,8 +20,7 @@ describe('jest-config', () => { it('marks outputFile as a Jest argument', () => { const args = ['test', '--ci', '--outputFile=path/to/my-file']; - const config = mockValidatedConfig(); - config.testing = {}; + const config = mockValidatedConfig({ testing: {} }); config.flags = parseFlags(args, config.sys); expect(config.flags.args).toEqual(['--ci', '--outputFile=path/to/my-file']); expect(config.flags.unknownArgs).toEqual([]); @@ -32,9 +30,8 @@ describe('jest-config', () => { it('pass --maxWorkers=2 arg when e2e test and --ci', () => { const args = ['test', '--ci', '--e2e', '--max-workers=2']; - const config = mockValidatedConfig(); + const config = mockValidatedConfig({ testing: {} }); config.flags = parseFlags(args, config.sys); - config.testing = {}; expect(config.flags.args).toEqual(['--ci', '--e2e', '--max-workers=2']); expect(config.flags.unknownArgs).toEqual([]); @@ -46,9 +43,8 @@ describe('jest-config', () => { it('forces --maxWorkers=4 arg when e2e test and --ci', () => { const args = ['test', '--ci', '--e2e']; - const config = mockValidatedConfig(); + const config = mockValidatedConfig({ testing: {} }); config.flags = parseFlags(args, config.sys); - config.testing = {}; expect(config.flags.args).toEqual(['--ci', '--e2e']); expect(config.flags.unknownArgs).toEqual([]); @@ -60,9 +56,8 @@ describe('jest-config', () => { it('pass --maxWorkers=2 arg to jest', () => { const args = ['test', '--maxWorkers=2']; - const config = mockValidatedConfig(); + const config = mockValidatedConfig({ testing: {} }); config.flags = parseFlags(args, config.sys); - config.testing = {}; expect(config.flags.args).toEqual(['--maxWorkers=2']); expect(config.flags.unknownArgs).toEqual([]); @@ -73,9 +68,8 @@ describe('jest-config', () => { it('pass --ci arg to jest', () => { const args = ['test', '--ci']; - const config = mockValidatedConfig(); + const config = mockValidatedConfig({ testing: {} }); config.flags = parseFlags(args, config.sys); - config.testing = {}; expect(config.flags.args).toEqual(['--ci']); expect(config.flags.unknownArgs).toEqual([]); @@ -87,9 +81,8 @@ describe('jest-config', () => { it('sets legacy jest options', () => { const args = ['test']; - const config = mockValidatedConfig(); + const config = mockValidatedConfig({ testing: {} }); config.flags = parseFlags(args, config.sys); - config.testing = {}; const jestArgv = buildJestArgv(config); @@ -117,9 +110,8 @@ describe('jest-config', () => { it('pass test spec arg to jest', () => { const args = ['test', 'hello.spec.ts']; - const config = mockValidatedConfig(); + const config = mockValidatedConfig({ testing: {} }); config.flags = parseFlags(args, config.sys); - config.testing = {}; expect(config.flags.args).toEqual(['hello.spec.ts']); expect(config.flags.unknownArgs).toEqual(['hello.spec.ts']); @@ -130,11 +122,12 @@ describe('jest-config', () => { it('pass test config to jest', () => { const args = ['test']; - const config = mockValidatedConfig(); + const config = mockValidatedConfig({ + testing: { + testMatch: ['hello.spec.ts'], + }, + }); config.flags = parseFlags(args, config.sys); - config.testing = { - testMatch: ['hello.spec.ts'], - }; expect(config.flags.task).toBe('test'); @@ -146,12 +139,11 @@ describe('jest-config', () => { it('set jestArgv config reporters', () => { const rootDir = path.resolve('/'); const args = ['test']; - const config = mockValidatedConfig(); - config.rootDir = rootDir; + const config = mockValidatedConfig({ + rootDir, + testing: { reporters: ['default', ['jest-junit', { suiteName: 'jest tests' }]] }, + }); config.flags = parseFlags(args, config.sys); - config.testing = { - reporters: ['default', ['jest-junit', { suiteName: 'jest tests' }]], - }; const jestArgv = buildJestArgv(config); const parsedConfig = JSON.parse(jestArgv.config) as d.JestConfig; @@ -164,10 +156,8 @@ describe('jest-config', () => { it('set jestArgv config rootDir', () => { const rootDir = path.resolve('/'); const args = ['test']; - const config = mockValidatedConfig(); - config.rootDir = rootDir; + const config = mockValidatedConfig({ rootDir, testing: {} }); config.flags = parseFlags(args, config.sys); - config.testing = {}; const jestArgv = buildJestArgv(config); const parsedConfig = JSON.parse(jestArgv.config) as d.JestConfig; @@ -177,12 +167,8 @@ describe('jest-config', () => { it('set jestArgv config collectCoverageFrom', () => { const rootDir = path.resolve('/'); const args = ['test']; - const config = mockValidatedConfig(); - config.rootDir = rootDir; + const config = mockValidatedConfig({ rootDir, testing: { collectCoverageFrom: ['**/*.+(ts|tsx)'] } }); config.flags = parseFlags(args, config.sys); - config.testing = { - collectCoverageFrom: ['**/*.+(ts|tsx)'], - }; const jestArgv = buildJestArgv(config); const parsedConfig = JSON.parse(jestArgv.config) as d.JestConfig; @@ -192,9 +178,8 @@ describe('jest-config', () => { it('passed flags should be respected over defaults', () => { const args = ['test', '--spec', '--passWithNoTests']; - const config = mockValidatedConfig(); + const config = mockValidatedConfig({ testing: {} }); config.flags = parseFlags(args, config.sys); - config.testing = {}; expect(config.flags.args).toEqual(['--spec', '--passWithNoTests']); expect(config.flags.unknownArgs).toEqual([]); From 770ae08d620a49bdbea19932a68d1a33a117084d Mon Sep 17 00:00:00 2001 From: Ryan Waskiewicz Date: Thu, 21 Jul 2022 18:44:58 -0400 Subject: [PATCH 8/9] update callsites for mockConfig --- src/cli/test/run.spec.ts | 6 +-- .../test/output-targets-dist.spec.ts | 15 +++--- .../test/output-targets-www-dist.spec.ts | 47 ++++++++++--------- .../test/output-targets-www.spec.ts | 9 ++-- .../test/service-worker-util.spec.ts | 20 ++++---- .../test/service-worker.spec.ts | 23 ++++----- src/compiler/style/test/optimize-css.spec.ts | 24 +--------- src/compiler/style/test/style.spec.ts | 12 ++--- .../types/tests/validate-package-json.spec.ts | 13 +++-- src/utils/test/util.spec.ts | 12 ++--- 10 files changed, 79 insertions(+), 102 deletions(-) diff --git a/src/cli/test/run.spec.ts b/src/cli/test/run.spec.ts index 93979fc147e..42ffe501368 100644 --- a/src/cli/test/run.spec.ts +++ b/src/cli/test/run.spec.ts @@ -137,8 +137,7 @@ describe('run', () => { sys = mockCompilerSystem(); sys.exit = jest.fn(); - unvalidatedConfig = mockConfig({ sys }); - unvalidatedConfig.outputTargets = null; + unvalidatedConfig = mockConfig({ outputTargets: null, sys }); validatedConfig = mockValidatedConfig({ sys }); @@ -260,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); diff --git a/src/compiler/output-targets/test/output-targets-dist.spec.ts b/src/compiler/output-targets/test/output-targets-dist.spec.ts index 2f1716d27fa..d38058b64e0 100644 --- a/src/compiler/output-targets/test/output-targets-dist.spec.ts +++ b/src/compiler/output-targets/test/output-targets-dist.spec.ts @@ -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); diff --git a/src/compiler/output-targets/test/output-targets-www-dist.spec.ts b/src/compiler/output-targets/test/output-targets-www-dist.spec.ts index 6f778a72247..85dc79d7512 100644 --- a/src/compiler/output-targets/test/output-targets-www-dist.spec.ts +++ b/src/compiler/output-targets/test/output-targets-www-dist.spec.ts @@ -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); diff --git a/src/compiler/output-targets/test/output-targets-www.spec.ts b/src/compiler/output-targets/test/output-targets-www.spec.ts index 66f81a77f0c..cfd2a0d93c0 100644 --- a/src/compiler/output-targets/test/output-targets-www.spec.ts +++ b/src/compiler/output-targets/test/output-targets-www.spec.ts @@ -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); diff --git a/src/compiler/service-worker/test/service-worker-util.spec.ts b/src/compiler/service-worker/test/service-worker-util.spec.ts index 77903ea5a6c..27a726d6229 100644 --- a/src/compiler/service-worker/test/service-worker-util.spec.ts +++ b/src/compiler/service-worker/test/service-worker-util.spec.ts @@ -8,14 +8,15 @@ 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); @@ -23,8 +24,7 @@ describe('generateServiceWorkerUrl', () => { }); 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); diff --git a/src/compiler/service-worker/test/service-worker.spec.ts b/src/compiler/service-worker/test/service-worker.spec.ts index 60a0c8dab2d..4debd729fb0 100644 --- a/src/compiler/service-worker/test/service-worker.spec.ts +++ b/src/compiler/service-worker/test/service-worker.spec.ts @@ -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'), `/**/`); diff --git a/src/compiler/style/test/optimize-css.spec.ts b/src/compiler/style/test/optimize-css.spec.ts index 2173af31cd6..e05ead98d03 100644 --- a/src/compiler/style/test/optimize-css.spec.ts +++ b/src/compiler/style/test/optimize-css.spec.ts @@ -14,15 +14,13 @@ describe('optimizeCss', () => { jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000; beforeEach(() => { - config = mockConfig(); - config.maxConcurrentWorkers = 0; + config = mockConfig({ maxConcurrentWorkers: 0, minifyCss: true }); compilerCtx = mockCompilerCtx(config); diagnostics = []; }); it('handles error', async () => { const filePath = path.join(os.tmpdir(), 'my.css'); - config.minifyCss = true; const styleText = `/* css */ body color: #ff0000; }`; await optimizeCss(config, compilerCtx, diagnostics, styleText, filePath); @@ -30,7 +28,6 @@ describe('optimizeCss', () => { }); it('discard-comments', async () => { - config.minifyCss = true; const styleText = `/* css */ body { color: #ff0000; }`; const output = await optimizeCss(config, compilerCtx, diagnostics, styleText, null); @@ -40,7 +37,6 @@ describe('optimizeCss', () => { it('minify-gradients', async () => { config.autoprefixCss = false; - config.minifyCss = true; const styleText = ` h1 { background: linear-gradient(to bottom, #ffe500 0%, #ffe500 50%, #121 50%, #121 100%); @@ -53,7 +49,6 @@ describe('optimizeCss', () => { }); it('reduce-initial', async () => { - config.minifyCss = true; const styleText = ` h1 { min-width: initial; @@ -66,7 +61,6 @@ describe('optimizeCss', () => { }); it('normalize-display-values', async () => { - config.minifyCss = true; const styleText = ` h1 { display: inline flow-root; @@ -80,7 +74,6 @@ describe('optimizeCss', () => { it('reduce-transforms', async () => { config.autoprefixCss = false; - config.minifyCss = true; const styleText = ` h1 { transform: rotate3d(0, 0, 1, 20deg); @@ -93,7 +86,6 @@ describe('optimizeCss', () => { }); it('colormin', async () => { - config.minifyCss = true; const styleText = `body { color: #ff0000; }`; const output = await optimizeCss(config, compilerCtx, diagnostics, styleText, null); @@ -102,7 +94,6 @@ describe('optimizeCss', () => { }); it('convert-values', async () => { - config.minifyCss = true; const styleText = ` h1 { width: 0em; @@ -115,7 +106,6 @@ describe('optimizeCss', () => { }); it('ordered-values', async () => { - config.minifyCss = true; const styleText = ` h1 { border: red solid .5em; @@ -128,7 +118,6 @@ describe('optimizeCss', () => { }); it('minify-selectors', async () => { - config.minifyCss = true; const styleText = ` h1 + p, h2, h3, h2{color:red} `; @@ -139,7 +128,6 @@ describe('optimizeCss', () => { }); it('minify-params', async () => { - config.minifyCss = true; const styleText = ` @media only screen and ( min-width: 400px, min-height: 500px ) { h2 { @@ -154,7 +142,6 @@ describe('optimizeCss', () => { }); it('normalize-string', async () => { - config.minifyCss = true; const styleText = ` p:after { content: '\\'string\\' is intact'; @@ -167,7 +154,6 @@ describe('optimizeCss', () => { }); it('minify-font-values', async () => { - config.minifyCss = true; const styleText = ` p { font-family: "Helvetica Neue", Arial, sans-serif, Helvetica; @@ -181,7 +167,6 @@ describe('optimizeCss', () => { }); it('normalize-repeat-style', async () => { - config.minifyCss = true; const styleText = ` h1 { background: url(image.jpg) repeat no-repeat; @@ -194,7 +179,6 @@ describe('optimizeCss', () => { }); it('normalize-positions', async () => { - config.minifyCss = true; const styleText = ` h1 { background-position: bottom left; @@ -207,7 +191,6 @@ describe('optimizeCss', () => { }); it('normalize-whitespace', async () => { - config.minifyCss = true; const styleText = ` h1 { width: calc(10px - ( 100px / var(--test) )) ; @@ -220,7 +203,6 @@ describe('optimizeCss', () => { }); it('unique-selectors', async () => { - config.minifyCss = true; const styleText = ` h1, h3, h2, h1 { color: red; @@ -234,7 +216,6 @@ describe('optimizeCss', () => { it('prevent autoprefix with null', async () => { config.autoprefixCss = null; - config.minifyCss = true; const styleText = ` h1 { box-shadow: 1px; @@ -248,7 +229,6 @@ describe('optimizeCss', () => { it('prevent autoprefix with false', async () => { config.autoprefixCss = false; - config.minifyCss = true; const styleText = ` h1 { box-shadow: 1px; @@ -261,7 +241,6 @@ describe('optimizeCss', () => { }); it('autoprefix by default', async () => { - config.minifyCss = true; const styleText = ` h1 { box-shadow: 1px; @@ -275,7 +254,6 @@ describe('optimizeCss', () => { it('runs autoprefixerCss true config', async () => { config.autoprefixCss = true; - config.minifyCss = true; const styleText = ` h1 { box-shadow: 1px; diff --git a/src/compiler/style/test/style.spec.ts b/src/compiler/style/test/style.spec.ts index e23846304fe..55dc340ddbd 100644 --- a/src/compiler/style/test/style.spec.ts +++ b/src/compiler/style/test/style.spec.ts @@ -13,16 +13,17 @@ xdescribe('component-styles', () => { const root = path.resolve('/'); beforeEach(async () => { - config = mockConfig(); + config = mockConfig({ + minifyCss: true, + minifyJs: true, + hashFileNames: true, + }); compiler = new Compiler(config); await compiler.fs.writeFile(path.join(root, 'src', 'index.html'), ``); await compiler.fs.commit(); }); it('should add mode styles to hashed filename/minified builds', async () => { - compiler.config.minifyJs = true; - compiler.config.minifyCss = true; - compiler.config.hashFileNames = true; compiler.config.hashedFileNameLength = 2; await compiler.fs.writeFiles({ [path.join(root, 'src', 'cmp-a.tsx')]: `@Component({ @@ -59,9 +60,6 @@ xdescribe('component-styles', () => { }); it('should add default styles to hashed filename/minified builds', async () => { - compiler.config.minifyJs = true; - compiler.config.minifyCss = true; - compiler.config.hashFileNames = true; compiler.config.sys.generateContentHash = function () { return 'hashed'; }; diff --git a/src/compiler/types/tests/validate-package-json.spec.ts b/src/compiler/types/tests/validate-package-json.spec.ts index a4c1261aa9f..e9d4e3084a5 100644 --- a/src/compiler/types/tests/validate-package-json.spec.ts +++ b/src/compiler/types/tests/validate-package-json.spec.ts @@ -24,14 +24,17 @@ describe('validate-package-json', () => { dir: '/dist', typesDir: '/dist/types', }; - config = mockConfig(); - config.devMode = false; - config.namespace = 'SomeNamespace'; - config.fsNamespace = config.namespace.toLowerCase(); + + const namespace = 'SomeNamespace'; + config = mockConfig({ + devMode: false, + fsNamespace: namespace.toLowerCase(), + namespace, + packageJsonFilePath: path.join(root, 'package.json'), + }); compilerCtx = mockCompilerCtx(config); buildCtx = mockBuildCtx(config, compilerCtx); buildCtx.packageJson = {}; - config.packageJsonFilePath = path.join(root, 'package.json'); await compilerCtx.fs.writeFile(config.packageJsonFilePath, JSON.stringify(buildCtx.packageJson)); }); diff --git a/src/utils/test/util.spec.ts b/src/utils/test/util.spec.ts index 8d5c23f6edb..8fc12ddc6e2 100644 --- a/src/utils/test/util.spec.ts +++ b/src/utils/test/util.spec.ts @@ -6,8 +6,7 @@ import { stubDiagnostic } from '../../dev-server/test/Diagnostic.stub'; describe('util', () => { describe('generatePreamble', () => { it('generates a comment with a single line preamble', () => { - const testConfig = mockConfig(); - testConfig.preamble = 'I am Stencil'; + const testConfig = mockConfig({ preamble: 'I am Stencil' }); const result = util.generatePreamble(testConfig); @@ -17,8 +16,7 @@ describe('util', () => { }); it('generates a comment with a multi-line preamble', () => { - const testConfig = mockConfig(); - testConfig.preamble = 'I am Stencil\nHear me roar'; + const testConfig = mockConfig({ preamble: 'I am Stencil\nHear me roar' }); const result = util.generatePreamble(testConfig); @@ -37,8 +35,7 @@ describe('util', () => { }); it('returns an empty string a null preamble is provided', () => { - const testConfig = mockConfig(); - testConfig.preamble = null; + const testConfig = mockConfig({ preamble: null }); const result = util.generatePreamble(testConfig); @@ -46,8 +43,7 @@ describe('util', () => { }); it('returns an empty string if an empty preamble is provided', () => { - const testConfig = mockConfig(); - testConfig.preamble = ''; + const testConfig = mockConfig({ preamble: '' }); const result = util.generatePreamble(testConfig); From 57d6a3bb86e3568368c5d69e3285d4dd7b5e4a85 Mon Sep 17 00:00:00 2001 From: Ryan Waskiewicz Date: Fri, 22 Jul 2022 09:48:25 -0400 Subject: [PATCH 9/9] review(ap): use createConfigFlags --- src/cli/telemetry/test/telemetry.spec.ts | 4 ++-- src/cli/test/task-generate.spec.ts | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/cli/telemetry/test/telemetry.spec.ts b/src/cli/telemetry/test/telemetry.spec.ts index 6e9858e435e..9cf24ac2939 100644 --- a/src/cli/telemetry/test/telemetry.spec.ts +++ b/src/cli/telemetry/test/telemetry.spec.ts @@ -15,7 +15,7 @@ describe('telemetryBuildFinishedAction', () => { beforeEach(() => { sys = createSystem(); config = mockValidatedConfig({ - flags: { args: [], knownArgs: [], task: 'build', unknownArgs: [] }, + flags: createConfigFlags({ task: 'build' }), outputTargets: [], sys, }); @@ -48,7 +48,7 @@ describe('telemetryAction', () => { beforeEach(() => { sys = createSystem(); config = mockValidatedConfig({ - flags: { args: [], knownArgs: [], task: 'build', unknownArgs: [] }, + flags: createConfigFlags({ task: 'build' }), outputTargets: [], sys, }); diff --git a/src/cli/test/task-generate.spec.ts b/src/cli/test/task-generate.spec.ts index 6823ccdebaa..e6c522d4676 100644 --- a/src/cli/test/task-generate.spec.ts +++ b/src/cli/test/task-generate.spec.ts @@ -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'); @@ -16,7 +17,7 @@ const setup = async () => { const sys = mockCompilerSystem(); const config: d.ValidatedConfig = mockValidatedConfig({ configPath: '/testing-path', - flags: { args: [], knownArgs: [], task: 'generate', unknownArgs: [] }, + flags: createConfigFlags({ task: 'generate' }), srcDir: '/src', sys, });