Skip to content

Commit

Permalink
fix(testing): respect user config
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Jun 25, 2019
1 parent 01f0a93 commit 70e711f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 96 deletions.
66 changes: 33 additions & 33 deletions src/cli/task-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,6 @@ import exit from 'exit';


export async function taskTest(config: d.Config) {
// always ensure we have jest modules installed
const ensureModuleIds = [
'@types/jest',
'jest',
'jest-cli'
];

if (config.flags && config.flags.e2e) {
// if it's an e2e test, also make sure we're got
// puppeteer modules installed and if browserExecutablePath is provided don't download Chromium use only puppeteer-core instead
const puppeteer = config.testing.browserExecutablePath ? 'puppeteer-core' : 'puppeteer';

ensureModuleIds.push(
'@types/puppeteer',
puppeteer
);

if (config.flags.screenshot) {
// ensure we've got pixelmatch for screenshots
config.logger.warn(config.logger.yellow(`EXPERIMENTAL: screenshot visual diff testing is currently under heavy development and has not reached a stable status. However, any assistance testing would be appreciated.`));
}
}

// ensure we've got the required modules installed
// jest and puppeteer are quite large, so this
// is an experiment to lazy install these
// modules only when you need them
await config.sys.lazyRequire.ensure(
config.logger,
config.rootDir,
ensureModuleIds
);

try {
const { Testing } = require('../testing/index.js');

Expand All @@ -44,6 +11,39 @@ export async function taskTest(config: d.Config) {
exit(1);
}

// always ensure we have jest modules installed
const ensureModuleIds = [
'@types/jest',
'jest',
'jest-cli'
];

if (config.flags.e2e) {
// if it's an e2e test, also make sure we're got
// puppeteer modules installed and if browserExecutablePath is provided don't download Chromium use only puppeteer-core instead
const puppeteer = config.testing.browserExecutablePath ? 'puppeteer-core' : 'puppeteer';

ensureModuleIds.push(
'@types/puppeteer',
puppeteer
);

if (config.flags.screenshot) {
// ensure we've got pixelmatch for screenshots
config.logger.warn(config.logger.yellow(`EXPERIMENTAL: screenshot visual diff testing is currently under heavy development and has not reached a stable status. However, any assistance testing would be appreciated.`));
}
}

// ensure we've got the required modules installed
// jest and puppeteer are quite large, so this
// is an experiment to lazy install these
// modules only when you need them
await config.sys.lazyRequire.ensure(
config.logger,
config.rootDir,
ensureModuleIds
);

const passed = await testing.runTests();
await testing.destroy();

Expand Down
76 changes: 22 additions & 54 deletions src/compiler/config/validate-testing.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as d from '../../declarations';
import { isOutputTargetDist, isOutputTargetWww } from '../output-targets/output-utils';
import { buildError } from '@utils';
import { buildError, buildWarn } from '@utils';


export function validateTesting(config: d.Config, diagnostics: d.Diagnostic[]) {
Expand Down Expand Up @@ -58,10 +58,6 @@ export function validateTesting(config: d.Config, diagnostics: d.Diagnostic[]) {
);
}

if (!Array.isArray(testing.moduleFileExtensions)) {
testing.moduleFileExtensions = DEFAULT_MODULE_FILE_EXTENSIONS;
}

if (!Array.isArray(testing.testPathIgnorePatterns)) {
testing.testPathIgnorePatterns = DEFAULT_IGNORE_PATTERNS.map(ignorePattern => {
return path.join(testing.rootDir, ignorePattern);
Expand All @@ -84,28 +80,26 @@ export function validateTesting(config: d.Config, diagnostics: d.Diagnostic[]) {
);
}

if (typeof testing.setupTestFrameworkScriptFile !== 'string') {
testing.setupTestFrameworkScriptFile = path.join(
config.sys.compiler.packageDir, 'testing', 'jest-setuptestframework.js'
);
if (!Array.isArray(testing.setupFilesAfterEnv)) {
testing.setupFilesAfterEnv = [];

} else if (!path.isAbsolute(testing.setupTestFrameworkScriptFile)) {
testing.setupTestFrameworkScriptFile = path.join(
config.configPath,
testing.setupTestFrameworkScriptFile
);
}

if (typeof testing.testEnvironment !== 'string') {
testing.testEnvironment = path.join(
config.sys.compiler.packageDir, 'testing', 'jest-environment.js'
);
testing.setupFilesAfterEnv.unshift(
path.join(config.sys.compiler.packageDir, 'testing', 'jest-setuptestframework.js')
);
if (testing.setupTestFrameworkScriptFile) {
const err = buildWarn(diagnostics);
err.messageText = `setupTestFrameworkScriptFile has been deprecated.`;
}

} else if (!path.isAbsolute(testing.testEnvironment)) {
testing.testEnvironment = path.join(
config.configPath,
testing.testEnvironment
);
if (typeof testing.testEnvironment === 'string') {
if (!path.isAbsolute(testing.testEnvironment)) {
testing.testEnvironment = path.join(
config.configPath,
testing.testEnvironment
);
}
}

if (typeof testing.allowableMismatchedPixels === 'number') {
Expand Down Expand Up @@ -141,8 +135,6 @@ export function validateTesting(config: d.Config, diagnostics: d.Diagnostic[]) {
} else if (typeof testing.testRegex === 'string') {
delete testing.testMatch;

} else {
testing.testRegex = '(/__tests__/.*|\\.?(test|spec|e2e))\\.(tsx?|ts?|jsx?|js?)$';
}

if (typeof testing.runner !== 'string') {
Expand Down Expand Up @@ -175,37 +167,8 @@ export function validateTesting(config: d.Config, diagnostics: d.Diagnostic[]) {
}
];
}

testing.transform = testing.transform || {};

if (typeof testing.transform[DEFAULT_TS_TRANSFORM] !== 'string') {
testing.transform[DEFAULT_TS_TRANSFORM] = path.join(
config.sys.compiler.packageDir, 'testing', 'jest-preprocessor.js'
);

} else if (!path.isAbsolute(testing.transform[DEFAULT_TS_TRANSFORM])) {
testing.transform[DEFAULT_TS_TRANSFORM] = path.join(
config.configPath,
testing.transform[DEFAULT_TS_TRANSFORM]
);
}

}

const DEFAULT_TS_TRANSFORM = '^.+\\.(ts|tsx)$';

const DEFAULT_MODULE_FILE_EXTENSIONS = [
'ts',
'tsx',
'js',
'json'
];

const DEFAULT_IGNORE_PATTERNS = [
'.vscode',
'.stencil',
'node_modules',
];

function addOption(setArray: string[], option: string) {
if (!setArray.includes(option)) {
Expand All @@ -216,3 +179,8 @@ function addOption(setArray: string[], option: string) {

const DEFAULT_ALLOWABLE_MISMATCHED_PIXELS = 100;
const DEFAULT_PIXEL_MATCH_THRESHOLD = 0.1;
const DEFAULT_IGNORE_PATTERNS = [
'.vscode',
'.stencil',
'node_modules',
];
5 changes: 5 additions & 0 deletions src/declarations/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ export interface JestConfig {
*/
setupFiles?: string[];

setupFilesAfterEnv?: string[];

/**
* @deprecated Use setupFilesAfterEnv instead.
*/
setupTestFrameworkScriptFile?: string;
snapshotSerializers?: any[];
testEnvironment?: string;
Expand Down
10 changes: 3 additions & 7 deletions src/testing/jest/jest-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,16 @@ export function createTestRunner(): any {
export function includeTestFile(testPath: string, env: d.E2EProcessEnv) {
testPath = testPath.toLowerCase().replace(/\\/g, '/');

if (!(testPath.endsWith('.ts') || testPath.endsWith('.tsx'))) {
return false;
}

if ((testPath.includes('.e2e.') || testPath.includes('/e2e.')) && env.__STENCIL_E2E_TESTS__ === 'true') {
const hasE2E = testPath.includes('.e2e.') || testPath.includes('/e2e.');
if (env.__STENCIL_E2E_TESTS__ === 'true' && hasE2E) {
// keep this test if it's an e2e file and we should be testing e2e
return true;
}

if ((testPath.includes('.spec.') || testPath.includes('/spec.')) && env.__STENCIL_SPEC_TESTS__ === 'true') {
if (env.__STENCIL_SPEC_TESTS__ === 'true' && !hasE2E) {
// keep this test if it's a spec file and we should be testing unit tests
return true;
}

return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/testing/jest/test/jest-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('jest-config', () => {

const jestArgv = buildJestArgv(config);
expect(jestArgv.ci).toBe(true);
expect(jestArgv.maxWorkers).toBeUndefined();
expect(jestArgv.maxWorkers).toBe(config.maxConcurrentWorkers);
});

it('pass test spec arg to jest', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/testing/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class Testing implements d.Testing {
const { Compiler } = require('../compiler/index.js');

this.compiler = new Compiler(setupTestingConfig(config));
config.maxConcurrentWorkers = Math.min(config.maxConcurrentWorkers, 6);
this.config = this.compiler.config;

this.isValid = this.compiler.isValid;
Expand Down Expand Up @@ -147,7 +148,6 @@ export class Testing implements d.Testing {
function setupTestingConfig(config: d.Config) {
config.buildEs5 = false;
config.devMode = true;
config.maxConcurrentWorkers = Math.min(config.maxConcurrentWorkers, 6);
config.validateTypes = false;
config._isTesting = true;

Expand Down

0 comments on commit 70e711f

Please sign in to comment.