@stylable/core-test-kit
Advanced tools
Comparing version 4.4.0 to 4.5.0
@@ -78,3 +78,3 @@ "use strict"; | ||
const diagnostics = new core_1.Diagnostics(); | ||
const result = generate_test_util_1.generateFromMock(config, diagnostics); | ||
const result = generate_test_util_1.generateStylableResult(config, diagnostics); | ||
const warningMessages = diagnostics.reports.map((d) => d.message); | ||
@@ -81,0 +81,0 @@ if (expectedWarnings.length === 0 && diagnostics.reports.length !== 0) { |
@@ -1,2 +0,2 @@ | ||
import { Diagnostics, FileProcessor, postProcessor, processNamespace, replaceValueHook, Stylable, StylableMeta, StylableResolver, StylableResults, StylableTransformer } from '@stylable/core'; | ||
import { Diagnostics, FileProcessor, postProcessor, processNamespace, replaceValueHook, StylableMeta, StylableResolver, StylableTransformer } from '@stylable/core'; | ||
import * as postcss from 'postcss'; | ||
@@ -31,10 +31,6 @@ export interface File { | ||
}, resolveNamespace?: typeof processNamespace): StylableMeta; | ||
export declare function generateFromMock(config: Config, diagnostics?: Diagnostics): StylableResults; | ||
export declare function createProcess(fileProcessor: FileProcessor<StylableMeta>): (path: string) => StylableMeta; | ||
export declare function createTransform(fileProcessor: FileProcessor<StylableMeta>, requireModule: RequireType): (meta: StylableMeta) => StylableMeta; | ||
export declare function expectTransformOutput(config: Config, numOfAssertions: number): StylableResults; | ||
export declare function generateStylableResult(config: Config): StylableResults; | ||
export declare function generateStylableResult(config: Config, diagnostics?: Diagnostics): import("@stylable/core").StylableResults; | ||
export declare function generateStylableRoot(config: Config): postcss.Root; | ||
export declare function generateStylableExports(config: Config): import("@stylable/core").StylableExports; | ||
export declare function createStylableInstance(config: Config): Stylable; | ||
//# sourceMappingURL=generate-test-util.d.ts.map |
@@ -22,7 +22,6 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createStylableInstance = exports.generateStylableExports = exports.generateStylableRoot = exports.generateStylableResult = exports.expectTransformOutput = exports.createTransform = exports.createProcess = exports.generateFromMock = exports.processSource = exports.createTransformer = exports.generateInfra = void 0; | ||
exports.generateStylableExports = exports.generateStylableRoot = exports.generateStylableResult = exports.createProcess = exports.processSource = exports.createTransformer = exports.generateInfra = void 0; | ||
const core_1 = require("@stylable/core"); | ||
const path_1 = require("path"); | ||
const postcss = __importStar(require("postcss")); | ||
const match_rules_1 = require("./match-rules"); | ||
function generateInfra(config, diagnostics = new core_1.Diagnostics()) { | ||
@@ -55,12 +54,2 @@ const { fs, requireModule } = core_1.createMinimalFS(config); | ||
exports.processSource = processSource; | ||
function generateFromMock(config, diagnostics = new core_1.Diagnostics()) { | ||
if (!path_1.isAbsolute(config.entry || '')) { | ||
throw new Error('entry must be absolute path: ' + config.entry); | ||
} | ||
const entry = config.entry; | ||
const t = createTransformer(config, diagnostics); | ||
const result = t.transform(t.fileProcessor.process(entry || '')); | ||
return result; | ||
} | ||
exports.generateFromMock = generateFromMock; | ||
function createProcess(fileProcessor) { | ||
@@ -70,42 +59,19 @@ return (path) => fileProcessor.process(path); | ||
exports.createProcess = createProcess; | ||
/* LEGACY */ | ||
function createTransform(fileProcessor, requireModule) { | ||
return (meta) => { | ||
return new core_1.StylableTransformer({ | ||
fileProcessor, | ||
requireModule, | ||
diagnostics: new core_1.Diagnostics(), | ||
keepValues: false, | ||
}).transform(meta).meta; | ||
}; | ||
function generateStylableResult(config, diagnostics = new core_1.Diagnostics()) { | ||
const { entry } = config; | ||
if (!path_1.isAbsolute(entry || '')) { | ||
throw new Error(`entry must be absolute path got: ${entry}`); | ||
} | ||
const transformer = createTransformer(config, diagnostics); | ||
return transformer.transform(transformer.fileProcessor.process(entry || '')); | ||
} | ||
exports.createTransform = createTransform; | ||
function expectTransformOutput(config, numOfAssertions) { | ||
const res = generateFromMock(config); | ||
match_rules_1.matchFromSource(res, numOfAssertions); | ||
return res; | ||
} | ||
exports.expectTransformOutput = expectTransformOutput; | ||
function generateStylableResult(config) { | ||
return generateFromMock(config); | ||
} | ||
exports.generateStylableResult = generateStylableResult; | ||
function generateStylableRoot(config) { | ||
return generateFromMock(config).meta.outputAst; | ||
return generateStylableResult(config).meta.outputAst; | ||
} | ||
exports.generateStylableRoot = generateStylableRoot; | ||
function generateStylableExports(config) { | ||
return generateFromMock(config).exports; | ||
return generateStylableResult(config).exports; | ||
} | ||
exports.generateStylableExports = generateStylableExports; | ||
function createStylableInstance(config) { | ||
config.trimWS = true; | ||
const { fs, requireModule } = core_1.createMinimalFS(config); | ||
const stylable = new core_1.Stylable('/', fs, requireModule, '__', (meta, path) => { | ||
meta.namespace = config.files[path].namespace || meta.namespace; | ||
return meta; | ||
}, undefined, undefined, config.resolve); | ||
return stylable; | ||
} | ||
exports.createStylableInstance = createStylableInstance; | ||
//# sourceMappingURL=generate-test-util.js.map |
@@ -1,6 +0,4 @@ | ||
import type { StylableResults } from '@stylable/core'; | ||
import type * as postcss from 'postcss'; | ||
export declare function matchRuleAndDeclaration(parent: postcss.Container, selectorIndex: number, selector: string, decl: string, msg?: string): void; | ||
export declare function matchAllRulesAndDeclarations(parent: postcss.Container, all: string[][], msg?: string, offset?: number): void; | ||
export declare function matchFromSource(results: StylableResults, numOfAssertions?: number): void; | ||
//# sourceMappingURL=match-rules.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.matchFromSource = exports.matchAllRulesAndDeclarations = exports.matchRuleAndDeclaration = void 0; | ||
exports.matchAllRulesAndDeclarations = exports.matchRuleAndDeclaration = void 0; | ||
function matchRuleAndDeclaration(parent, selectorIndex, selector, decl, msg) { | ||
@@ -20,30 +20,2 @@ const rule = parent.nodes[selectorIndex]; | ||
exports.matchAllRulesAndDeclarations = matchAllRulesAndDeclarations; | ||
function matchFromSource(results, numOfAssertions = 0) { | ||
let foundAssertions = 0; | ||
const errors = []; | ||
const root = results.meta.outputAst; | ||
if (!root) { | ||
throw new Error('expectCSS missing root ast'); | ||
} | ||
root.walkRules((rule) => { | ||
const prev = rule.prev(); | ||
if ((prev === null || prev === void 0 ? void 0 : prev.type) === 'comment') { | ||
const match = prev.text.trim().match(/@expect (.*)/); | ||
if (match) { | ||
foundAssertions++; | ||
const selector = match[1]; | ||
if (selector !== rule.selector) { | ||
errors.push(`\nactual: ${rule.selector}\nexpect: ${selector}`); | ||
} | ||
} | ||
} | ||
}); | ||
if (foundAssertions !== numOfAssertions) { | ||
errors.push(`expected ${numOfAssertions} @expect assertions found ${foundAssertions}`); | ||
} | ||
if (errors.length) { | ||
throw new Error(errors.join('\n')); | ||
} | ||
} | ||
exports.matchFromSource = matchFromSource; | ||
//# sourceMappingURL=match-rules.js.map |
{ | ||
"name": "@stylable/core-test-kit", | ||
"version": "4.4.0", | ||
"version": "4.5.0", | ||
"description": "Stylable core test-kit", | ||
@@ -10,3 +10,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@stylable/core": "^4.4.0", | ||
"@stylable/core": "^4.5.0", | ||
"chai": "^4.3.4", | ||
@@ -13,0 +13,0 @@ "flat": "^5.0.2", |
@@ -18,22 +18,80 @@ # @stylable/core-test-kit | ||
A collection of tools aimed at testing Stylable diagnostics messages (warnings and errors). | ||
A collection of tools used for testing Stylable diagnostics messages (warnings and errors). | ||
- `expectWarnings` - processes a Stylable input and checks for diagnostics during processing | ||
- `expectWarningsFromTransform` - checks for diagnostics after a full transformation | ||
- `shouldReportNoDiagnostics` - helper to check no diagnostics were reported | ||
### Testing infrastructure | ||
Used for easily setting up Stylable instances (processor/transformer) and its infrastructure. | ||
Used for setting up Stylable instances (`processor`/`transformer`) and their infrastructure: | ||
Exposes `expectTransformOutput` utility for creating transformation tests. These are the most common core tests and the recommended way to test the core transform functionality. | ||
- `generateInfra` - create Stylable basic in memory infrastructure (`resolver`, `requireModule`, `fileProcessor`) | ||
- `generateStylableResult` - genetare transformation results from in memory configuration | ||
- `generateStylableRoot` - helper over `generateStylableResult` that returns the `outputAst` | ||
- `generateStylableExports` - helper over `generateStylableResult` that returns the `exports` mapping | ||
Currently only supports testing target selector but when needed the functionality can be expended here to support: | ||
### `testInlineExpects` utility | ||
* JavaScript exports output | ||
* Declaration values | ||
* Mixins output | ||
Exposes `testInlineExpects` for testing transformed stylesheets that include inline expectation comments. These are the most common type of core tests and the recommended way of testing the core functionality. | ||
#### Supported checks: | ||
Rule checking (place just before rule) supporting multi-line declarations and multiple `@checks` statements | ||
##### Terminilogy | ||
- `LABEL: <string>` - label for the test expectation | ||
- `OFFEST: <number>` - offest for the tested rule after the `@check` | ||
- `SELECTOR: <string>` - output selector | ||
- `DECL: <string>` - declaration name | ||
- `VALUE: <string>` - declaration value | ||
Full options: | ||
```css | ||
/* @check(LABEL)[OFFEST] SELECTOR {DECL: VALUE} */ | ||
``` | ||
Basic - `@check SELECTOR` | ||
```css | ||
/* @check header::before */ | ||
header::before {} | ||
``` | ||
With declarations - ` @check SELECTOR {DECL1: VALUE1; DECL2: VALUE2;}` | ||
This will check full match and order. | ||
```css | ||
.my-mixin { | ||
color: red; | ||
} | ||
/* @check .entry__container {color: red;} */ | ||
.container { | ||
-st-mixin: my-mixin; | ||
} | ||
``` | ||
Target generated rules (mixin) - ` @check[OFFEST] SELECTOR` | ||
```css | ||
/* | ||
@check[1] .entry__container:hover {color: blue;} | ||
*/ | ||
.container { | ||
-st-mixin: my-mixin; | ||
} | ||
``` | ||
Support atrule params (anything between the @atrule and body or semicolon): | ||
```css | ||
/* @check screen and (min-width: 900px) */ | ||
@media value(smallScreen) {} | ||
``` | ||
#### Example | ||
Using the `/* @expect selector */` comment to test the root class selector target | ||
Here we are generating a Stylable AST which lncludes the `/* @check SELECTOR */` comment to test the root class selector target. | ||
```js | ||
expectTransformOutput( | ||
{ | ||
The `testInlineExpects` function performs that actual assertions to perform the test. | ||
```ts | ||
it('...', ()=>{ | ||
const root = generateStylableRoot({ | ||
entry: `/style.st.css`, | ||
@@ -44,9 +102,9 @@ files: { | ||
content: ` | ||
/* @expect .ns__root */ | ||
/* @check .ns__root */ | ||
.root {} | ||
` | ||
}, | ||
}, | ||
1 | ||
); | ||
}); | ||
testInlineExpects(root, 1); | ||
}) | ||
``` | ||
@@ -53,0 +111,0 @@ |
@@ -5,3 +5,3 @@ import { expect } from 'chai'; | ||
import { Diagnostics, process, safeParse, StylableMeta, StylableResults } from '@stylable/core'; | ||
import { Config, generateFromMock } from './generate-test-util'; | ||
import { Config, generateStylableResult } from './generate-test-util'; | ||
@@ -97,3 +97,3 @@ export interface Diagnostic { | ||
const diagnostics = new Diagnostics(); | ||
const result = generateFromMock(config, diagnostics); | ||
const result = generateStylableResult(config, diagnostics); | ||
const warningMessages = diagnostics.reports.map((d) => d.message); | ||
@@ -100,0 +100,0 @@ |
@@ -9,6 +9,4 @@ import { | ||
replaceValueHook, | ||
Stylable, | ||
StylableMeta, | ||
StylableResolver, | ||
StylableResults, | ||
StylableTransformer, | ||
@@ -19,3 +17,2 @@ createInfrastructure, | ||
import * as postcss from 'postcss'; | ||
import { matchFromSource } from './match-rules'; | ||
@@ -100,18 +97,2 @@ export interface File { | ||
export function generateFromMock( | ||
config: Config, | ||
diagnostics: Diagnostics = new Diagnostics() | ||
): StylableResults { | ||
if (!isAbsolute(config.entry || '')) { | ||
throw new Error('entry must be absolute path: ' + config.entry); | ||
} | ||
const entry = config.entry; | ||
const t = createTransformer(config, diagnostics); | ||
const result = t.transform(t.fileProcessor.process(entry || '')); | ||
return result; | ||
} | ||
export function createProcess( | ||
@@ -123,55 +104,20 @@ fileProcessor: FileProcessor<StylableMeta> | ||
/* LEGACY */ | ||
export function createTransform( | ||
fileProcessor: FileProcessor<StylableMeta>, | ||
requireModule: RequireType | ||
): (meta: StylableMeta) => StylableMeta { | ||
return (meta: StylableMeta) => { | ||
return new StylableTransformer({ | ||
fileProcessor, | ||
requireModule, | ||
diagnostics: new Diagnostics(), | ||
keepValues: false, | ||
}).transform(meta).meta; | ||
}; | ||
export function generateStylableResult( | ||
config: Config, | ||
diagnostics: Diagnostics = new Diagnostics() | ||
) { | ||
const { entry } = config; | ||
if (!isAbsolute(entry || '')) { | ||
throw new Error(`entry must be absolute path got: ${entry}`); | ||
} | ||
const transformer = createTransformer(config, diagnostics); | ||
return transformer.transform(transformer.fileProcessor.process(entry || '')); | ||
} | ||
export function expectTransformOutput(config: Config, numOfAssertions: number) { | ||
const res = generateFromMock(config); | ||
matchFromSource(res, numOfAssertions); | ||
return res; | ||
} | ||
export function generateStylableResult(config: Config) { | ||
return generateFromMock(config); | ||
} | ||
export function generateStylableRoot(config: Config) { | ||
return generateFromMock(config).meta.outputAst!; | ||
return generateStylableResult(config).meta.outputAst!; | ||
} | ||
export function generateStylableExports(config: Config) { | ||
return generateFromMock(config).exports; | ||
return generateStylableResult(config).exports; | ||
} | ||
export function createStylableInstance(config: Config) { | ||
config.trimWS = true; | ||
const { fs, requireModule } = createMinimalFS(config); | ||
const stylable = new Stylable( | ||
'/', | ||
fs, | ||
requireModule, | ||
'__', | ||
(meta, path) => { | ||
meta.namespace = config.files[path].namespace || meta.namespace; | ||
return meta; | ||
}, | ||
undefined, | ||
undefined, | ||
config.resolve | ||
); | ||
return stylable; | ||
} |
@@ -1,2 +0,1 @@ | ||
import type { StylableResults } from '@stylable/core'; | ||
import type * as postcss from 'postcss'; | ||
@@ -38,30 +37,1 @@ | ||
} | ||
export function matchFromSource(results: StylableResults, numOfAssertions = 0) { | ||
let foundAssertions = 0; | ||
const errors: string[] = []; | ||
const root = results.meta.outputAst; | ||
if (!root) { | ||
throw new Error('expectCSS missing root ast'); | ||
} | ||
root.walkRules((rule) => { | ||
const prev = rule.prev(); | ||
if (prev?.type === 'comment') { | ||
const match = prev.text.trim().match(/@expect (.*)/); | ||
if (match) { | ||
foundAssertions++; | ||
const selector = match[1]; | ||
if (selector !== rule.selector) { | ||
errors.push(`\nactual: ${rule.selector}\nexpect: ${selector}`); | ||
} | ||
} | ||
} | ||
}); | ||
if (foundAssertions !== numOfAssertions) { | ||
errors.push(`expected ${numOfAssertions} @expect assertions found ${foundAssertions}`); | ||
} | ||
if (errors.length) { | ||
throw new Error(errors.join('\n')); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
117
73989
1180
Updated@stylable/core@^4.5.0