@stylable/core-test-kit
Advanced tools
Comparing version 3.11.14 to 3.12.0
@@ -34,6 +34,7 @@ import { Diagnostics, FileProcessor, postProcessor, processNamespace, replaceValueHook, Stylable, StylableMeta, StylableResolver, StylableResults, StylableTransformer } from '@stylable/core'; | ||
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 generateStylableRoot(config: Config): postcss.Root; | ||
export declare function generateStylableExports(config: Config): import("../../core/src").StylableExports; | ||
export declare function generateStylableExports(config: Config): import("@stylable/core/src").StylableExports; | ||
export declare function createStylableInstance(config: Config): Stylable; | ||
//# sourceMappingURL=generate-test-util.d.ts.map |
@@ -22,6 +22,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createStylableInstance = exports.generateStylableExports = exports.generateStylableRoot = exports.generateStylableResult = exports.createTransform = exports.createProcess = exports.generateFromMock = exports.processSource = exports.createTransformer = exports.generateInfra = void 0; | ||
exports.createStylableInstance = exports.generateStylableExports = exports.generateStylableRoot = exports.generateStylableResult = exports.expectTransformOutput = exports.createTransform = exports.createProcess = exports.generateFromMock = 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()) { | ||
@@ -80,2 +81,8 @@ const { fs, requireModule } = core_1.createMinimalFS(config); | ||
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) { | ||
@@ -82,0 +89,0 @@ return generateFromMock(config); |
@@ -0,4 +1,6 @@ | ||
import { StylableResults } from '@stylable/core'; | ||
import * 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.matchAllRulesAndDeclarations = exports.matchRuleAndDeclaration = void 0; | ||
exports.matchFromSource = exports.matchAllRulesAndDeclarations = exports.matchRuleAndDeclaration = void 0; | ||
function matchRuleAndDeclaration(parent, selectorIndex, selector, decl, msg) { | ||
@@ -20,2 +20,30 @@ 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": "3.11.14", | ||
"version": "3.12.0", | ||
"description": "Stylable core test-kit", | ||
@@ -12,6 +12,6 @@ "main": "./cjs/index.js", | ||
"dependencies": { | ||
"@stylable/core": "^3.11.14", | ||
"chai": "^4.2.0", | ||
"@stylable/core": "^3.12.0", | ||
"chai": "^4.3.0", | ||
"flat": "^5.0.2", | ||
"postcss": "^8.2.4" | ||
"postcss": "^8.2.6" | ||
}, | ||
@@ -18,0 +18,0 @@ "files": [ |
@@ -7,5 +7,5 @@ # @stylable/core-test-kit | ||
### What's in this test-kit? | ||
## What's in this test-kit? | ||
#### Matchers | ||
### Matchers | ||
@@ -17,12 +17,40 @@ An assortment of `Chai` matchers used by Stylable. | ||
#### Diagnostics tooling | ||
### Diagnostics tooling | ||
A collection of tools aimed at testing Stylable diagnostics messages (warnings and errors). | ||
#### Testing infrastructure | ||
### Testing infrastructure | ||
Used for easily setting up Stylable instances (processor/transformer) and its infrastructure. | ||
#### Match rules | ||
Exposes `expectTransformOutput` utility for creating transformation tests. These are the most common core tests and the recommended way to test the core transform functionality. | ||
Currently only supports testing target selector but when needed the functionality can be expended here to support: | ||
* JavaScript exports output | ||
* Declaration values | ||
* Mixins output | ||
#### Example | ||
Using the `/* @expect selector */` comment to test the root class selector target | ||
```js | ||
expectTransformOutput( | ||
{ | ||
entry: `/style.st.css`, | ||
files: { | ||
'/style.st.css': { | ||
namespace: 'ns', | ||
content: ` | ||
/* @expect .ns__root */ | ||
.root {} | ||
` | ||
}, | ||
}, | ||
1 | ||
); | ||
``` | ||
### Match rules | ||
Exposes two utility functions (`matchRuleAndDeclaration` and `matchAllRulesAndDeclarations`) used for testing Stylable generated AST representing CSS rules and declarations. | ||
@@ -29,0 +57,0 @@ |
@@ -18,2 +18,3 @@ import { | ||
import * as postcss from 'postcss'; | ||
import { matchFromSource } from './match-rules'; | ||
@@ -135,2 +136,8 @@ export interface File { | ||
export function expectTransformOutput(config: Config, numOfAssertions: number) { | ||
const res = generateFromMock(config); | ||
matchFromSource(res, numOfAssertions); | ||
return res; | ||
} | ||
export function generateStylableResult(config: Config) { | ||
@@ -137,0 +144,0 @@ return generateFromMock(config); |
@@ -0,1 +1,2 @@ | ||
import { StylableResults } from '@stylable/core'; | ||
import * as postcss from 'postcss'; | ||
@@ -37,1 +38,30 @@ | ||
} | ||
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')); | ||
} | ||
} |
@@ -43,40 +43,43 @@ import { StylableResults } from '@stylable/core'; | ||
chai.Assertion.addMethod('styleRules', function ( | ||
styleRules: string[] | { [key: number]: string } | ||
) { | ||
const actual = flag(this, 'object') as StylableResults; | ||
if (!actual.meta || !actual.exports) { | ||
throw new Error( | ||
`expected Stylable result {meta, exports}, but got: {${Object.keys(actual).join( | ||
', ' | ||
)}}` | ||
); | ||
} | ||
chai.Assertion.addMethod( | ||
'styleRules', | ||
function (styleRules: string[] | { [key: number]: string }) { | ||
const actual = flag(this, 'object') as StylableResults; | ||
if (!actual.meta || !actual.exports) { | ||
throw new Error( | ||
`expected Stylable result {meta, exports}, but got: {${Object.keys(actual).join( | ||
', ' | ||
)}}` | ||
); | ||
} | ||
let scopeRule: postcss.Container | undefined = flag(this, 'actualRule'); | ||
if (!scopeRule) { | ||
const { outputAst } = actual.meta; | ||
if (!outputAst) { | ||
throw new Error(`expected result to be transfromed - missing outputAst on meta`); | ||
} else { | ||
scopeRule = outputAst; | ||
let scopeRule: postcss.Container | undefined = flag(this, 'actualRule'); | ||
if (!scopeRule) { | ||
const { outputAst } = actual.meta; | ||
if (!outputAst) { | ||
throw new Error( | ||
`expected result to be transfromed - missing outputAst on meta` | ||
); | ||
} else { | ||
scopeRule = outputAst; | ||
} | ||
} | ||
} | ||
if (Array.isArray(styleRules)) { | ||
scopeRule.walkRules((rule, index) => { | ||
const nextExpectedRule = styleRules.shift(); | ||
const actualRule = rule.toString(); | ||
expect(actualRule, `rule #${index}`).to.equal(nextExpectedRule); | ||
}); | ||
} else { | ||
const nodes = scopeRule.nodes; | ||
for (const expectedIndex in styleRules) { | ||
expect(nodes, `rules exist`).to.not.equal(undefined); | ||
expect(nodes && nodes[expectedIndex].toString()).to.equal( | ||
styleRules[expectedIndex] | ||
); | ||
if (Array.isArray(styleRules)) { | ||
scopeRule.walkRules((rule, index) => { | ||
const nextExpectedRule = styleRules.shift(); | ||
const actualRule = rule.toString(); | ||
expect(actualRule, `rule #${index}`).to.equal(nextExpectedRule); | ||
}); | ||
} else { | ||
const nodes = scopeRule.nodes; | ||
for (const expectedIndex in styleRules) { | ||
expect(nodes, `rules exist`).to.not.equal(undefined); | ||
expect(nodes && nodes[expectedIndex].toString()).to.equal( | ||
styleRules[expectedIndex] | ||
); | ||
} | ||
} | ||
} | ||
}); | ||
); | ||
} |
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
57804
907
59
Updated@stylable/core@^3.12.0
Updatedchai@^4.3.0
Updatedpostcss@^8.2.6