Skip to content

Commit

Permalink
fix validate-testing tests
Browse files Browse the repository at this point in the history
this commit updates the tests for `validate-testing`. it replaces
instances of `path.join` in assertions with stencil's own `join`
function. existing instances of `path.join` have been left as-is if they
pertain to user input. this allows us to exercise a user using either
path separator in CI (which runs in windows and linux).

the screenshot connector is now normalized if it is absolute. otherwise,
provided connector file path in `stencil.config.ts` would never get
normalized
  • Loading branch information
rwaskiewicz committed Nov 9, 2023
1 parent 4c3f03d commit a7c6b49
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
54 changes: 38 additions & 16 deletions src/compiler/config/test/validate-testing.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import type * as d from '@stencil/core/declarations';
import { mockCompilerSystem, mockLoadConfigInit, mockLogger } from '@stencil/core/testing';
import { join } from '@utils';
import path from 'path';

import { ConfigFlags, createConfigFlags } from '../../../cli/config-flags';
import { validateConfig } from '../validate-config';

describe('validateTesting', () => {
// use Node's resolve() here to simulate a user using either Win/Posix separators (depending on the platform these
// tests are run on)
const ROOT = path.resolve('/');
const sys = mockCompilerSystem();
const logger = mockLogger();
Expand All @@ -17,15 +20,21 @@ describe('validateTesting', () => {
userConfig = {
sys: sys as any,
logger: logger,
// use Node's join() here to simulate a user using either Win/Posix separators (depending on the platform these
// tests are run on) for their input
rootDir: path.join(ROOT, 'User', 'some', 'path'),
srcDir: path.join(ROOT, 'User', 'some', 'path', 'src'),
flags,
namespace: 'Testing',
// use Node's join() here to simulate a user using either Win/Posix separators (depending on the platform these
// tests are run on) for their input
configPath: path.join(ROOT, 'User', 'some', 'path', 'stencil.config.ts'),
};
userConfig.outputTargets = [
{
type: 'www',
// use Node's join() here to simulate a user using either Win/Posix separators (depending on the platform these
// tests are run on) for their input
dir: path.join(ROOT, 'www'),
} as any as d.OutputTargetStats,
];
Expand Down Expand Up @@ -195,21 +204,25 @@ describe('validateTesting', () => {

describe('screenshotConnector', () => {
it('assigns the screenshotConnector value from the provided flags', () => {
// use Node's join() here to simulate a user using either Win/Posix separators (depending on the platform these
// tests are run on) for their input
userConfig.flags = { ...flags, e2e: true, screenshotConnector: path.join(ROOT, 'mock', 'path') };
userConfig.testing = { screenshotConnector: path.join(ROOT, 'another', 'mock', 'path') };

const { config } = validateConfig(userConfig, mockLoadConfigInit());

expect(config.testing.screenshotConnector).toBe(path.join(ROOT, 'mock', 'path'));
expect(config.testing.screenshotConnector).toBe(join(ROOT, 'mock', 'path'));
});

it("uses the config's root dir to make the screenshotConnector path absolute", () => {
// use Node's join() here to simulate a user using either Win/Posix separators (depending on the platform these
// tests are run on) for their input
userConfig.flags = { ...flags, e2e: true, screenshotConnector: path.join('mock', 'path') };
userConfig.testing = { screenshotConnector: path.join('another', 'mock', 'path') };

const { config } = validateConfig(userConfig, mockLoadConfigInit());

expect(config.testing.screenshotConnector).toBe(path.join(ROOT, 'User', 'some', 'path', 'mock', 'path'));
expect(config.testing.screenshotConnector).toBe(join(ROOT, 'User', 'some', 'path', 'mock', 'path'));
});

it('sets screenshotConnector if a non-string is provided', () => {
Expand All @@ -219,14 +232,16 @@ describe('validateTesting', () => {

const { config } = validateConfig(userConfig, mockLoadConfigInit());

expect(config.testing.screenshotConnector).toBe(path.join('screenshot', 'local-connector.js'));
expect(config.testing.screenshotConnector).toBe(join('screenshot', 'local-connector.js'));
});
});

describe('testPathIgnorePatterns', () => {
it('does not alter a provided testPathIgnorePatterns', () => {
userConfig.flags = { ...flags, e2e: true };

// use Node's join() here to simulate a user using either Win/Posix separators (depending on the platform these
// tests are run on) for their input
const mockPath1 = path.join('this', 'is', 'a', 'mock', 'path');
const mockPath2 = path.join('this', 'is', 'another', 'mock', 'path');
userConfig.testing = { testPathIgnorePatterns: [mockPath1, mockPath2] };
Expand All @@ -236,15 +251,16 @@ describe('validateTesting', () => {
expect(config.testing.testPathIgnorePatterns).toEqual([mockPath1, mockPath2]);
});

it('sets the default testPathIgnorePatterns if not array is provided', () => {
it('sets the default testPathIgnorePatterns if no array is provided', () => {
userConfig.flags = { ...flags, e2e: true };

const { config } = validateConfig(userConfig, mockLoadConfigInit());

expect(config.testing.testPathIgnorePatterns).toEqual([
path.join(ROOT, 'User', 'some', 'path', '.vscode'),
path.join(ROOT, 'User', 'some', 'path', '.stencil'),
path.join(ROOT, 'User', 'some', 'path', 'node_modules'),
join(ROOT, 'User', 'some', 'path', '.vscode'),
join(ROOT, 'User', 'some', 'path', '.stencil'),
join(ROOT, 'User', 'some', 'path', 'node_modules'),
// use Node's join() here as the normalization process doesn't necessarily occur for this field
path.join(ROOT, 'www'),
]);
});
Expand All @@ -260,11 +276,11 @@ describe('validateTesting', () => {
const { config } = validateConfig(userConfig, mockLoadConfigInit());

expect(config.testing.testPathIgnorePatterns).toEqual([
path.join(ROOT, 'User', 'some', 'path', '.vscode'),
path.join(ROOT, 'User', 'some', 'path', '.stencil'),
path.join(ROOT, 'User', 'some', 'path', 'node_modules'),
path.join(ROOT, 'User', 'some', 'path', 'www-folder'),
path.join(ROOT, 'User', 'some', 'path', 'dist-folder'),
join(ROOT, 'User', 'some', 'path', '.vscode'),
join(ROOT, 'User', 'some', 'path', '.stencil'),
join(ROOT, 'User', 'some', 'path', 'node_modules'),
join(ROOT, 'User', 'some', 'path', 'www-folder'),
join(ROOT, 'User', 'some', 'path', 'dist-folder'),
]);
});
});
Expand All @@ -286,24 +302,30 @@ describe('validateTesting', () => {
it('forces a provided preset path to be absolute', () => {
userConfig.flags = { ...flags, e2e: true };
userConfig.testing = {
// use Node's join() here to simulate a user using either Win/Posix separators (depending on the platform these
// tests are run on) for their input
preset: path.join('mock', 'path'),
};

const { config } = validateConfig(userConfig, mockLoadConfigInit());

expect(config.testing.preset).toEqual(path.join(ROOT, 'User', 'some', 'path', 'mock', 'path'));
expect(config.testing.preset).toEqual(join(ROOT, 'User', 'some', 'path', 'mock', 'path'));
});

it('does not change an already absolute preset path', () => {
userConfig.flags = { ...flags, e2e: true };

// use Node's join() here to simulate a user using either Win/Posix separators (depending on the platform these
// tests are run on) for their input
const presetPath = path.join(ROOT, 'mock', 'path');
userConfig.testing = {
preset: presetPath,
};

const { config } = validateConfig(userConfig, mockLoadConfigInit());

// per the test name, we should not change an already absolute path - assert against the preset path that was
// generated using Node's join()
expect(config.testing.preset).toEqual(presetPath);
});
});
Expand All @@ -319,7 +341,7 @@ describe('validateTesting', () => {
const { config } = validateConfig(userConfig, mockLoadConfigInit());

// 'testing' is the internal directory where the default setup file can be found
expect(config.testing.setupFilesAfterEnv).toEqual([path.join('testing', 'jest-setuptestframework.js')]);
expect(config.testing.setupFilesAfterEnv).toEqual([join('testing', 'jest-setuptestframework.js')]);
});

it.each([[[]], [['mock-setup-file.js']]])(
Expand All @@ -334,7 +356,7 @@ describe('validateTesting', () => {

expect(config.testing.setupFilesAfterEnv).toEqual([
// 'testing' is the internal directory where the default setup file can be found
path.join('testing', 'jest-setuptestframework.js'),
join('testing', 'jest-setuptestframework.js'),
...setupFilesAfterEnv,
]);
},
Expand Down Expand Up @@ -672,7 +694,7 @@ describe('validateTesting', () => {
const { config } = validateConfig(userConfig, mockLoadConfigInit());

// 'testing' is the internal directory where the default runner file can be found
expect(config.testing.runner).toEqual(path.join('testing', 'jest-runner.js'));
expect(config.testing.runner).toEqual(join('testing', 'jest-runner.js'));
});
});

Expand Down
4 changes: 3 additions & 1 deletion src/compiler/config/validate-testing.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { buildError, isOutputTargetDist, isOutputTargetWww, isString, join } from '@utils';
import { buildError, isOutputTargetDist, isOutputTargetWww, isString, join, normalizePath } from '@utils';
import { basename, dirname, isAbsolute } from 'path';

import type * as d from '../../declarations';
Expand Down Expand Up @@ -59,6 +59,8 @@ export const validateTesting = (config: d.ValidatedConfig, diagnostics: d.Diagno
if (typeof testing.screenshotConnector === 'string') {
if (!isAbsolute(testing.screenshotConnector)) {
testing.screenshotConnector = join(config.rootDir!, testing.screenshotConnector);
} else {
testing.screenshotConnector = normalizePath(testing.screenshotConnector);
}
} else {
testing.screenshotConnector = join(
Expand Down

0 comments on commit a7c6b49

Please sign in to comment.